1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_WEBDRIVER_SESSION_H_
#define CHROME_TEST_WEBDRIVER_SESSION_H_
#include <map>
#include <string>
#include <vector>
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/threading/thread.h"
#include "chrome/common/automation_constants.h"
#include "chrome/test/webdriver/automation.h"
#include "chrome/test/webdriver/error_codes.h"
class DictionaryValue;
class FilePath;
class GURL;
class ListValue;
class Value;
namespace base {
class WaitableEvent;
}
namespace gfx {
class Point;
}
namespace webdriver {
class WebElementId;
// Every connection made by WebDriver maps to a session object.
// This object creates the chrome instance and keeps track of the
// state necessary to control the chrome browser created.
// A session manages its own lifetime.
class Session {
public:
// Adds this |Session| to the |SessionManager|. The session manages its own
// lifetime. Do not call delete.
Session();
// Removes this |Session| from the |SessionManager|.
~Session();
// Starts the session thread and a new browser, using the exe found in
// |browser_dir|. If |browser_dir| is empty, it will search in all the default
// locations. Returns true on success. On failure, the session will delete
// itself and return false.
bool Init(const FilePath& browser_dir);
// Terminates this session and deletes itself.
void Terminate();
// Executes the given |script| in the context of the given window and frame.
// The |script| should be in the form of a function body
// (e.g. "return arguments[0]"), where |args| is the list of arguments to
// pass to the function. The caller is responsible for the script result
// |value|.
ErrorCode ExecuteScript(int window_id,
const std::string& frame_xpath,
const std::string& script,
const ListValue* const args,
Value** value);
// Same as above, but uses the currently targeted window and frame.
ErrorCode ExecuteScript(const std::string& script,
const ListValue* const args,
Value** value);
// Send the given keys to the given element dictionary. This function takes
// ownership of |element|.
ErrorCode SendKeys(const WebElementId& element, const string16& keys);
// Clicks the mouse at the given location using the given button.
void MouseClick(const gfx::Point& click, automation::MouseButton button);
bool MouseMove(const gfx::Point& location);
bool MouseDrag(const gfx::Point& start, const gfx::Point& end);
bool NavigateToURL(const std::string& url);
bool GoForward();
bool GoBack();
bool Reload();
bool GetURL(GURL* url);
bool GetURL(std::string* url);
bool GetTabTitle(std::string* tab_title);
bool GetCookies(const GURL& url, std::string* cookies);
bool GetCookieByName(const GURL& url, const std::string& cookie_name,
std::string* cookie);
bool DeleteCookie(const GURL& url, const std::string& cookie_name);
bool SetCookie(const GURL& url, const std::string& cookie);
// Gets all the currently existing window IDs. Returns true on success.
bool GetWindowIds(std::vector<int>* window_ids);
// Switches the window used by default. |name| is either an ID returned by
// |GetWindowIds| or the name attribute of a DOM window.
ErrorCode SwitchToWindow(const std::string& name);
// Switches the frame used by default. |name_or_id| is either the name or id
// of a frame element.
ErrorCode SwitchToFrameWithNameOrId(const std::string& name_or_id);
// Switches the frame used by default. |index| is the zero-based frame index.
ErrorCode SwitchToFrameWithIndex(int index);
// Closes the current window. Returns true on success.
// Note: The session will be deleted if this closes the last window in the
// session.
bool CloseWindow();
// Gets the version of the running browser.
std::string GetVersion();
// Finds a single element in the given window and frame, starting at the given
// |root_element|, using the given locator strategy. |locator| should be a
// constant from |LocatorType|. Returns an error code. If successful,
// |element| will be set as the found element.
ErrorCode FindElementInFrame(int window_id,
const std::string& frame_xpath,
const WebElementId& root_element,
const std::string& locator,
const std::string& query,
WebElementId* element);
// Same as above, but uses the current window and frame.
ErrorCode FindElement(const WebElementId& root_element,
const std::string& locator,
const std::string& query,
WebElementId* element);
// Same as above, but finds multiple elements.
ErrorCode FindElements(const WebElementId& root_element,
const std::string& locator,
const std::string& query,
std::vector<WebElementId>* elements);
// Scroll the element into view and get its location relative to the client's
// viewport.
ErrorCode GetElementLocationInView(
const WebElementId& element, int* x, int* y);
// Waits for all tabs to stop loading. Returns true on success.
bool WaitForAllTabsToStopLoading();
inline const std::string& id() const { return id_; }
inline int implicit_wait() const { return implicit_wait_; }
inline void set_implicit_wait(const int& timeout) {
implicit_wait_ = timeout > 0 ? timeout : 0;
}
enum Speed { kSlow, kMedium, kFast, kUnknown };
inline Speed speed() { return speed_; }
inline void set_speed(Speed speed) {
speed_ = speed;
}
inline const std::string& current_frame_xpath() const {
return current_frame_xpath_;
}
inline void set_current_frame_xpath(const std::string& xpath) {
current_frame_xpath_ = xpath;
}
inline int current_window_id() const { return current_window_id_; }
private:
void RunSessionTask(Task* task);
void RunSessionTaskOnSessionThread(
Task* task,
base::WaitableEvent* done_event);
void InitOnSessionThread(const FilePath& browser_dir, bool* success);
void TerminateOnSessionThread();
void SendKeysOnSessionThread(const string16& keys, bool* success);
ErrorCode SwitchToFrameWithJavaScriptLocatedFrame(
const std::string& script,
ListValue* args);
ErrorCode FindElementsHelper(int window_id,
const std::string& frame_xpath,
const WebElementId& root_element,
const std::string& locator,
const std::string& query,
bool find_one,
std::vector<WebElementId>* elements);
ErrorCode GetLocationInViewHelper(int window_id,
const std::string& frame_xpath,
const WebElementId& element,
int* offset_x,
int* offset_y);
const std::string id_;
scoped_ptr<Automation> automation_;
base::Thread thread_;
int implicit_wait_;
Speed speed_;
// The XPath to the frame within this session's active tab which all
// commands should be directed to. XPath strings can represent a frame deep
// down the tree (across multiple frame DOMs).
// Example, /html/body/table/tbody/tr/td/iframe\n/frameset/frame[1]
// should break into 2 xpaths
// /html/body/table/tbody/tr/td/iframe & /frameset/frame[1].
std::string current_frame_xpath_;
int current_window_id_;
DISALLOW_COPY_AND_ASSIGN(Session);
};
} // namespace webdriver
DISABLE_RUNNABLE_METHOD_REFCOUNT(webdriver::Session);
#endif // CHROME_TEST_WEBDRIVER_SESSION_H_
|