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 "chrome/test/webdriver/automation.h" | 13 #include "chrome/test/webdriver/automation.h" |
13 #include "chrome/test/webdriver/error_codes.h" | 14 #include "chrome/test/webdriver/error_codes.h" |
14 | 15 |
15 class GURL; | 16 class GURL; |
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(GURL* url); | 71 bool GetURL(GURL* url); |
60 bool GetURL(std::string* url); | 72 bool GetURL(std::string* url); |
61 bool GetTabTitle(std::string* tab_title); | 73 bool GetTabTitle(std::string* tab_title); |
62 bool GetCookies(const GURL& url, std::string* cookies); | 74 bool GetCookies(const GURL& url, std::string* cookies); |
63 bool GetCookieByName(const GURL& url, const std::string& cookie_name, | 75 bool GetCookieByName(const GURL& url, const std::string& cookie_name, |
64 std::string* cookie); | 76 std::string* cookie); |
65 bool DeleteCookie(const GURL& url, const std::string& cookie_name); | 77 bool DeleteCookie(const GURL& url, const std::string& cookie_name); |
66 bool SetCookie(const GURL& url, const std::string& cookie); | 78 bool SetCookie(const GURL& url, const std::string& cookie); |
67 | 79 |
| 80 // Gets all the currently existing window IDs. Returns true on success. |
| 81 bool GetWindowIds(std::vector<int>* window_ids); |
| 82 |
| 83 // Switches the window used by default. |name| is either an ID returned by |
| 84 // |GetWindowIds| or the name attribute of a DOM window. |
| 85 ErrorCode SwitchToWindow(const std::string& name); |
| 86 |
| 87 // Switches the frame used by default. |name_or_id| is either the name or id |
| 88 // of a frame element. |
| 89 ErrorCode SwitchToFrameWithNameOrId(const std::string& name_or_id); |
| 90 |
| 91 // Switches the frame used by default. |index| is the zero-based frame index. |
| 92 ErrorCode SwitchToFrameWithIndex(int index); |
| 93 |
| 94 // Closes the current window. Returns true on success. |
| 95 // Note: The session will be deleted if this closes the last window in the |
| 96 // session. |
| 97 bool CloseWindow(); |
| 98 |
68 inline const std::string& id() const { return id_; } | 99 inline const std::string& id() const { return id_; } |
69 | 100 |
70 inline int implicit_wait() { return implicit_wait_; } | 101 inline int implicit_wait() { return implicit_wait_; } |
71 inline void set_implicit_wait(const int& timeout) { | 102 inline void set_implicit_wait(const int& timeout) { |
72 implicit_wait_ = timeout > 0 ? timeout : 0; | 103 implicit_wait_ = timeout > 0 ? timeout : 0; |
73 } | 104 } |
74 | 105 |
75 enum Speed { kSlow, kMedium, kFast, kUnknown }; | 106 enum Speed { kSlow, kMedium, kFast, kUnknown }; |
76 inline Speed speed() { return speed_; } | 107 inline Speed speed() { return speed_; } |
77 inline void set_speed(Speed speed) { | 108 inline void set_speed(Speed speed) { |
78 speed_ = speed; | 109 speed_ = speed; |
79 } | 110 } |
80 | 111 |
81 inline const std::string& current_frame_xpath() const { | 112 inline const std::string& current_frame_xpath() const { |
82 return current_frame_xpath_; | 113 return current_frame_xpath_; |
83 } | 114 } |
84 | 115 |
85 inline void set_current_frame_xpath(const std::string& xpath) { | 116 inline void set_current_frame_xpath(const std::string& xpath) { |
86 current_frame_xpath_ = xpath; | 117 current_frame_xpath_ = xpath; |
87 } | 118 } |
88 | 119 |
| 120 inline int current_window_id() const { return current_window_id_; } |
| 121 |
89 private: | 122 private: |
90 void RunSessionTask(Task* task); | 123 void RunSessionTask(Task* task); |
91 void RunSessionTaskOnSessionThread( | 124 void RunSessionTaskOnSessionThread( |
92 Task* task, | 125 Task* task, |
93 base::WaitableEvent* done_event); | 126 base::WaitableEvent* done_event); |
94 void InitOnSessionThread(bool* success); | 127 void InitOnSessionThread(bool* success); |
95 void TerminateOnSessionThread(); | 128 void TerminateOnSessionThread(); |
96 void SendKeysOnSessionThread(const string16& keys, bool* success); | 129 void SendKeysOnSessionThread(const string16& keys, bool* success); |
| 130 ErrorCode SwitchToFrameWithJavaScriptLocatedFrame( |
| 131 const std::string& script, |
| 132 ListValue* args); |
| 133 |
| 134 const std::string id_; |
97 | 135 |
98 scoped_ptr<Automation> automation_; | 136 scoped_ptr<Automation> automation_; |
99 base::Thread thread_; | 137 base::Thread thread_; |
100 | 138 |
101 const std::string id_; | |
102 | |
103 int window_num_; | |
104 | |
105 int implicit_wait_; | 139 int implicit_wait_; |
106 Speed speed_; | 140 Speed speed_; |
107 | 141 |
108 // The XPath to the frame within this session's active tab which all | 142 // The XPath to the frame within this session's active tab which all |
109 // commands should be directed to. XPath strings can represent a frame deep | 143 // commands should be directed to. XPath strings can represent a frame deep |
110 // down the tree (across multiple frame DOMs). | 144 // down the tree (across multiple frame DOMs). |
111 // Example, /html/body/table/tbody/tr/td/iframe\n/frameset/frame[1] | 145 // Example, /html/body/table/tbody/tr/td/iframe\n/frameset/frame[1] |
112 // should break into 2 xpaths | 146 // should break into 2 xpaths |
113 // /html/body/table/tbody/tr/td/iframe & /frameset/frame[1]. | 147 // /html/body/table/tbody/tr/td/iframe & /frameset/frame[1]. |
114 std::string current_frame_xpath_; | 148 std::string current_frame_xpath_; |
| 149 int current_window_id_; |
115 | 150 |
116 DISALLOW_COPY_AND_ASSIGN(Session); | 151 DISALLOW_COPY_AND_ASSIGN(Session); |
117 }; | 152 }; |
118 | 153 |
119 } // namespace webdriver | 154 } // namespace webdriver |
120 | 155 |
121 DISABLE_RUNNABLE_METHOD_REFCOUNT(webdriver::Session); | 156 DISABLE_RUNNABLE_METHOD_REFCOUNT(webdriver::Session); |
122 | 157 |
123 #endif // CHROME_TEST_WEBDRIVER_SESSION_H_ | 158 #endif // CHROME_TEST_WEBDRIVER_SESSION_H_ |
OLD | NEW |