OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_TEST_WEBDRIVER_SESSION_H_ | 5 #ifndef CHROME_TEST_WEBDRIVER_SESSION_H_ |
6 #define CHROME_TEST_WEBDRIVER_SESSION_H_ | 6 #define CHROME_TEST_WEBDRIVER_SESSION_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
11 #include "base/string16.h" | 12 #include "base/string16.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/test/webdriver/automation.h" | 14 #include "chrome/test/webdriver/automation.h" |
14 #include "chrome/test/webdriver/error_codes.h" | 15 #include "chrome/test/webdriver/error_codes.h" |
15 | 16 |
16 class ListValue; | 17 class ListValue; |
17 class Value; | 18 class Value; |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class WaitableEvent; | 21 class WaitableEvent; |
21 } | 22 } |
22 | 23 |
23 namespace webdriver { | 24 namespace webdriver { |
24 | 25 |
25 // Every connection made by WebDriver maps to a session object. | 26 // Every connection made by WebDriver maps to a session object. |
26 // This object creates the chrome instance and keeps track of the | 27 // This object creates the chrome instance and keeps track of the |
27 // state necessary to control the chrome browser created. | 28 // state necessary to control the chrome browser created. |
| 29 // A session manages its own lifetime. |
28 // TODO(phajdan.jr): Abstract UITestBase classes, see: | 30 // TODO(phajdan.jr): Abstract UITestBase classes, see: |
29 // http://code.google.com/p/chromium/issues/detail?id=56865 | 31 // http://code.google.com/p/chromium/issues/detail?id=56865 |
30 class Session { | 32 class Session { |
31 public: | 33 public: |
32 explicit Session(const std::string& id); | 34 // Adds this |Session| to the |SessionManager|. The session manages its own |
| 35 // lifetime. Do not call delete. |
| 36 Session(); |
| 37 // Removes this |Session| from the |SessionManager|. |
33 ~Session(); | 38 ~Session(); |
34 | 39 |
35 // Creates a browser. | 40 // Starts the session thread and a new browser. Returns true on |
| 41 // success. On failure, the session will delete itself and return false. |
36 bool Init(); | 42 bool Init(); |
37 | 43 |
38 // Terminates this session and disconnects its automation proxy. After | 44 // Terminates this session and deletes itself. |
39 // invoking this method, the Session can safely be deleted. | |
40 void Terminate(); | 45 void Terminate(); |
41 | 46 |
42 // Executes the given |script| in the context of the frame that is currently | 47 // Executes the given |script| in the context of the given window and frame. |
43 // the focus of this session. The |script| should be in the form of a | 48 // The |script| should be in the form of a function body |
44 // function body (e.g. "return arguments[0]"), where \args| is the list of | 49 // (e.g. "return arguments[0]"), where |args| is the list of arguments to |
45 // arguments to pass to the function. The caller is responsible for the | 50 // pass to the function. The caller is responsible for the script result |
46 // script result |value|. | 51 // |value|. |
| 52 ErrorCode ExecuteScript(int window_id, |
| 53 const std::string& frame_xpath, |
| 54 const std::string& script, |
| 55 const ListValue* const args, |
| 56 Value** value); |
| 57 |
| 58 // Same as above, but uses the currently targeted window and frame. |
47 ErrorCode ExecuteScript(const std::string& script, | 59 ErrorCode ExecuteScript(const std::string& script, |
48 const ListValue* const args, | 60 const ListValue* const args, |
49 Value** value); | 61 Value** value); |
50 | 62 |
51 // Send the given keys to the given element dictionary. This function takes | 63 // Send the given keys to the given element dictionary. This function takes |
52 // ownership of |element|. | 64 // ownership of |element|. |
53 ErrorCode SendKeys(DictionaryValue* element, const string16& keys); | 65 ErrorCode SendKeys(DictionaryValue* element, const string16& keys); |
54 | 66 |
55 bool NavigateToURL(const std::string& url); | 67 bool NavigateToURL(const std::string& url); |
56 bool GoForward(); | 68 bool GoForward(); |
57 bool GoBack(); | 69 bool GoBack(); |
58 bool Reload(); | 70 bool Reload(); |
59 bool GetURL(std::string* url); | 71 bool GetURL(std::string* url); |
60 bool GetTabTitle(std::string* tab_title); | 72 bool GetTabTitle(std::string* tab_title); |
61 | 73 |
| 74 // Gets all the currently existing window IDs. Returns true on success. |
| 75 bool GetWindowIds(std::vector<int>* window_ids); |
| 76 |
| 77 // Switches the window used by default. |name| is either an ID returned by |
| 78 // |GetWindowIds| or the name attribute of a DOM window. |
| 79 ErrorCode SwitchToWindow(const std::string& name); |
| 80 |
| 81 // Switches the frame used by default. |name_or_id| is either the name or id |
| 82 // of a frame element. |
| 83 ErrorCode SwitchToFrameWithNameOrId(const std::string& name_or_id); |
| 84 |
| 85 // Switches the frame used by default. |index| is the zero-based frame index. |
| 86 ErrorCode SwitchToFrameWithIndex(int index); |
| 87 |
| 88 // Closes the current window. Returns true on success. |
| 89 // Note: The session will be deleted if this closes the last window in the |
| 90 // session. |
| 91 bool CloseWindow(); |
| 92 |
62 inline const std::string& id() const { return id_; } | 93 inline const std::string& id() const { return id_; } |
63 | 94 |
64 inline int implicit_wait() { return implicit_wait_; } | 95 inline int implicit_wait() { return implicit_wait_; } |
65 inline void set_implicit_wait(const int& timeout) { | 96 inline void set_implicit_wait(const int& timeout) { |
66 implicit_wait_ = timeout > 0 ? timeout : 0; | 97 implicit_wait_ = timeout > 0 ? timeout : 0; |
67 } | 98 } |
68 | 99 |
69 enum Speed { kSlow, kMedium, kFast, kUnknown }; | 100 enum Speed { kSlow, kMedium, kFast, kUnknown }; |
70 inline Speed speed() { return speed_; } | 101 inline Speed speed() { return speed_; } |
71 inline void set_speed(Speed speed) { | 102 inline void set_speed(Speed speed) { |
72 speed_ = speed; | 103 speed_ = speed; |
73 } | 104 } |
74 | 105 |
75 inline const std::string& current_frame_xpath() const { | 106 inline const std::string& current_frame_xpath() const { |
76 return current_frame_xpath_; | 107 return current_frame_xpath_; |
77 } | 108 } |
78 | 109 |
79 inline void set_current_frame_xpath(const std::string& xpath) { | 110 inline void set_current_frame_xpath(const std::string& xpath) { |
80 current_frame_xpath_ = xpath; | 111 current_frame_xpath_ = xpath; |
81 } | 112 } |
82 | 113 |
| 114 inline int current_window_id() const { return current_window_id_; } |
| 115 |
83 private: | 116 private: |
84 void RunSessionTask(Task* task); | 117 void RunSessionTask(Task* task); |
85 void RunSessionTaskOnSessionThread( | 118 void RunSessionTaskOnSessionThread( |
86 Task* task, | 119 Task* task, |
87 base::WaitableEvent* done_event); | 120 base::WaitableEvent* done_event); |
88 void InitOnSessionThread(bool* success); | 121 void InitOnSessionThread(bool* success); |
89 void TerminateOnSessionThread(); | 122 void TerminateOnSessionThread(); |
90 void SendKeysOnSessionThread(const string16& keys, bool* success); | 123 void SendKeysOnSessionThread(const string16& keys, bool* success); |
| 124 ErrorCode SwitchToFrameWithJavaScriptLocatedFrame( |
| 125 const std::string& script, |
| 126 ListValue* args); |
| 127 |
| 128 const std::string id_; |
91 | 129 |
92 scoped_ptr<Automation> automation_; | 130 scoped_ptr<Automation> automation_; |
93 base::Thread thread_; | 131 base::Thread thread_; |
94 | 132 |
95 const std::string id_; | |
96 | |
97 int window_num_; | |
98 | |
99 int implicit_wait_; | 133 int implicit_wait_; |
100 Speed speed_; | 134 Speed speed_; |
101 | 135 |
102 // The XPath to the frame within this session's active tab which all | 136 // The XPath to the frame within this session's active tab which all |
103 // commands should be directed to. XPath strings can represent a frame deep | 137 // commands should be directed to. XPath strings can represent a frame deep |
104 // down the tree (across multiple frame DOMs). | 138 // down the tree (across multiple frame DOMs). |
105 // Example, /html/body/table/tbody/tr/td/iframe\n/frameset/frame[1] | 139 // Example, /html/body/table/tbody/tr/td/iframe\n/frameset/frame[1] |
106 // should break into 2 xpaths | 140 // should break into 2 xpaths |
107 // /html/body/table/tbody/tr/td/iframe & /frameset/frame[1]. | 141 // /html/body/table/tbody/tr/td/iframe & /frameset/frame[1]. |
108 std::string current_frame_xpath_; | 142 std::string current_frame_xpath_; |
| 143 int current_window_id_; |
109 | 144 |
110 DISALLOW_COPY_AND_ASSIGN(Session); | 145 DISALLOW_COPY_AND_ASSIGN(Session); |
111 }; | 146 }; |
112 | 147 |
113 } // namespace webdriver | 148 } // namespace webdriver |
114 | 149 |
115 DISABLE_RUNNABLE_METHOD_REFCOUNT(webdriver::Session); | 150 DISABLE_RUNNABLE_METHOD_REFCOUNT(webdriver::Session); |
116 | 151 |
117 #endif // CHROME_TEST_WEBDRIVER_SESSION_H_ | 152 #endif // CHROME_TEST_WEBDRIVER_SESSION_H_ |
OLD | NEW |