Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/test/webdriver/automation.cc

Issue 6614023: Convert ChromeDriver to use only the JSON automation interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address Pawel's additional comments Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/webdriver/automation.h ('k') | chrome/test/webdriver/commands/mouse_commands.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "chrome/test/webdriver/automation.h" 5 #include "chrome/test/webdriver/automation.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
11 #include "base/base_paths.h" 11 #include "base/base_paths.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/environment.h" 13 #include "base/environment.h"
14 #include "base/file_path.h" 14 #include "base/file_path.h"
15 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "base/values.h" 20 #include "base/values.h"
21 #include "chrome/common/automation_constants.h" 21 #include "chrome/common/automation_constants.h"
22 #include "chrome/common/chrome_constants.h" 22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_switches.h" 23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/url_constants.h" 24 #include "chrome/common/url_constants.h"
25 #include "chrome/test/automation/automation_json_requests.h" 25 #include "chrome/test/automation/automation_json_requests.h"
26 #include "chrome/test/automation/automation_proxy.h" 26 #include "chrome/test/automation/automation_proxy.h"
27 #include "chrome/test/automation/browser_proxy.h"
28 #include "chrome/test/automation/proxy_launcher.h" 27 #include "chrome/test/automation/proxy_launcher.h"
29 #include "chrome/test/automation/tab_proxy.h"
30 #include "googleurl/src/gurl.h" 28 #include "googleurl/src/gurl.h"
31 #include "ui/gfx/point.h" 29 #include "ui/gfx/point.h"
32 30
33 #if defined(OS_WIN) 31 #if defined(OS_WIN)
34 #include "base/win/registry.h" 32 #include "base/win/registry.h"
35 #include "base/win/windows_version.h" 33 #include "base/win/windows_version.h"
36 #endif 34 #endif
37 35
38 namespace { 36 namespace {
39 37
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 return true; 108 return true;
111 } 109 }
112 } 110 }
113 return false; 111 return false;
114 } 112 }
115 113
116 } // namespace 114 } // namespace
117 115
118 namespace webdriver { 116 namespace webdriver {
119 117
120 WebKeyEvent::WebKeyEvent(automation::KeyEventTypes type,
121 ui::KeyboardCode key_code,
122 const std::string& unmodified_text,
123 const std::string& modified_text,
124 int modifiers)
125 : type(type),
126 key_code(key_code),
127 unmodified_text(unmodified_text),
128 modified_text(modified_text),
129 modifiers(modifiers) {}
130
131 Automation::Automation() {} 118 Automation::Automation() {}
132 119
133 Automation::~Automation() {} 120 Automation::~Automation() {}
134 121
135 void Automation::Init(const FilePath& user_browser_dir, bool* success) { 122 void Automation::Init(const FilePath& user_browser_dir, bool* success) {
136 FilePath browser_dir = user_browser_dir; 123 FilePath browser_dir = user_browser_dir;
137 if (browser_dir.empty() && !GetDefaultChromeExeDir(&browser_dir)) { 124 if (browser_dir.empty() && !GetDefaultChromeExeDir(&browser_dir)) {
138 LOG(ERROR) << "Could not locate Chrome application directory"; 125 LOG(ERROR) << "Could not locate Chrome application directory";
139 *success = false; 126 *success = false;
140 return; 127 return;
(...skipping 25 matching lines...) Expand all
166 153
167 void Automation::Terminate() { 154 void Automation::Terminate() {
168 launcher_->QuitBrowser(); 155 launcher_->QuitBrowser();
169 } 156 }
170 157
171 void Automation::ExecuteScript(int tab_id, 158 void Automation::ExecuteScript(int tab_id,
172 const std::string& frame_xpath, 159 const std::string& frame_xpath,
173 const std::string& script, 160 const std::string& script,
174 std::string* result, 161 std::string* result,
175 bool* success) { 162 bool* success) {
176 TabProxy* tab = GetTabById(tab_id); 163 int windex = 0, tab_index = 0;
177 if (!tab) { 164 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
178 *success = false; 165 *success = false;
179 return; 166 return;
180 } 167 }
181 std::wstring wide_xpath = UTF8ToWide(frame_xpath); 168
182 std::wstring wide_script = UTF8ToWide(script); 169 Value* unscoped_value;
183 std::wstring wide_result; 170 if (!SendExecuteJavascriptJSONRequest(
184 *success = tab->ExecuteAndExtractString( 171 automation(), windex, tab_index, frame_xpath, script, &unscoped_value)) {
185 wide_xpath, wide_script, &wide_result); 172 *success = false;
186 if (*success) 173 return;
187 *result = WideToUTF8(wide_result); 174 }
175 scoped_ptr<Value> value(unscoped_value);
176 *success = value->GetAsString(result);
188 } 177 }
189 178
190 void Automation::MouseMove(int tab_id, 179 void Automation::MouseMove(int tab_id,
191 const gfx::Point& p, 180 const gfx::Point& p,
192 bool* success) { 181 bool* success) {
193 std::string reply; 182 int windex = 0, tab_index = 0;
194 DictionaryValue dict; 183 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
184 *success = false;
185 return;
186 }
195 187
196 dict.SetString("command", "WebkitMouseMove"); 188 *success = SendMouseMoveJSONRequest(
197 dict.SetInteger("x", p.x()); 189 automation(), windex, tab_index, p.x(), p.y());
198 dict.SetInteger("y", p.y());
199
200 *success = SendJSONRequest(tab_id, dict, &reply);
201 if (!*success) {
202 LOG(ERROR) << "Could not send mouse event. Reply: " << reply;
203 }
204 } 190 }
205 191
206 void Automation::MouseClick(int tab_id, 192 void Automation::MouseClick(int tab_id,
207 const gfx::Point& p, 193 const gfx::Point& p,
208 int flag, 194 automation::MouseButton button,
209 bool* success) { 195 bool* success) {
210 std::string reply; 196 int windex = 0, tab_index = 0;
211 DictionaryValue dict; 197 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
198 *success = false;
199 return;
200 }
212 201
213 dict.SetString("command", "WebkitMouseClick"); 202 *success = SendMouseClickJSONRequest(
214 dict.SetInteger("button_flags", flag); 203 automation(), windex, tab_index, button, p.x(), p.y());
215 dict.SetInteger("x", p.x());
216 dict.SetInteger("y", p.y());
217
218 *success = SendJSONRequest(tab_id, dict, &reply);
219 if (!*success) {
220 LOG(ERROR) << "Could not send mouse event. Reply: " << reply;
221 }
222 } 204 }
223 205
224 void Automation::MouseDrag(int tab_id, 206 void Automation::MouseDrag(int tab_id,
225 const gfx::Point& start, 207 const gfx::Point& start,
226 const gfx::Point& end, 208 const gfx::Point& end,
227 bool* success) { 209 bool* success) {
228 std::string reply; 210 int windex = 0, tab_index = 0;
229 DictionaryValue dict; 211 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
212 *success = false;
213 return;
214 }
230 215
231 dict.SetString("command", "WebkitMouseDrag"); 216 *success = SendMouseDragJSONRequest(
232 dict.SetInteger("start_x", start.x()); 217 automation(), windex, tab_index, start.x(), start.y(), end.x(), end.y());
233 dict.SetInteger("start_y", start.y());
234 dict.SetInteger("end_x", end.x());
235 dict.SetInteger("end_y", end.y());
236
237 *success = SendJSONRequest(tab_id, dict, &reply);
238 if (!*success) {
239 LOG(ERROR) << "Could not send mouse event. Reply: " << reply;
240 }
241 } 218 }
242 219
243 void Automation::SendWebKeyEvent(int tab_id, 220 void Automation::SendWebKeyEvent(int tab_id,
244 const WebKeyEvent& key_event, 221 const WebKeyEvent& key_event,
245 bool* success) { 222 bool* success) {
246 std::string reply; 223 int windex = 0, tab_index = 0;
247 DictionaryValue dict; 224 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
225 *success = false;
226 return;
227 }
248 228
249 dict.SetString("command", "SendKeyEventToActiveTab"); 229 *success = SendWebKeyEventJSONRequest(
250 dict.SetInteger("type", key_event.type); 230 automation(), windex, tab_index, key_event);
251 dict.SetInteger("nativeKeyCode", key_event.key_code);
252 dict.SetInteger("windowsKeyCode", key_event.key_code);
253 dict.SetString("unmodifiedText", key_event.unmodified_text);
254 dict.SetString("text", key_event.modified_text);
255 dict.SetInteger("modifiers", key_event.modifiers);
256 dict.SetBoolean("isSystemKey", false);
257
258 *success = SendJSONRequest(tab_id, dict, &reply);
259 if (!*success) {
260 LOG(ERROR) << "Could not send web key event. Reply: " << reply;
261 }
262 } 231 }
263 232
264 void Automation::NavigateToURL(int tab_id, 233 void Automation::NavigateToURL(int tab_id,
265 const std::string& url, 234 const std::string& url,
266 bool* success) { 235 bool* success) {
267 int browser_index = 0, tab_index = 0; 236 int browser_index = 0, tab_index = 0;
268 if (!GetIndicesForTab(tab_id, &browser_index, &tab_index)) { 237 if (!GetIndicesForTab(tab_id, &browser_index, &tab_index)) {
269 *success = false; 238 *success = false;
270 return; 239 return;
271 } 240 }
272 241
273 AutomationMsg_NavigationResponseValues navigate_response; 242 AutomationMsg_NavigationResponseValues navigate_response;
274 if (!SendNavigateToURLJSONRequest(automation(), browser_index, tab_index, 243 if (!SendNavigateToURLJSONRequest(automation(), browser_index, tab_index,
275 GURL(url), 1, &navigate_response)) { 244 GURL(url), 1, &navigate_response)) {
276 *success = false; 245 *success = false;
277 return; 246 return;
278 } 247 }
279 *success = navigate_response != AUTOMATION_MSG_NAVIGATION_ERROR; 248 *success = navigate_response != AUTOMATION_MSG_NAVIGATION_ERROR;
280 } 249 }
281 250
282 void Automation::GoForward(int tab_id, bool* success) { 251 void Automation::GoForward(int tab_id, bool* success) {
283 TabProxy* tab = GetTabById(tab_id); 252 int windex = 0, tab_index = 0;
284 if (!tab) { 253 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
285 *success = false; 254 *success = false;
286 return; 255 return;
287 } 256 }
288 *success = tab->GoForward(); 257
258 *success = SendGoForwardJSONRequest(automation(), windex, tab_index);
289 } 259 }
290 260
291 void Automation::GoBack(int tab_id, bool* success) { 261 void Automation::GoBack(int tab_id, bool* success) {
292 TabProxy* tab = GetTabById(tab_id); 262 int windex = 0, tab_index = 0;
293 if (!tab) { 263 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
294 *success = false; 264 *success = false;
295 return; 265 return;
296 } 266 }
297 *success = tab->GoBack(); 267
268 *success = SendGoBackJSONRequest(automation(), windex, tab_index);
298 } 269 }
299 270
300 void Automation::Reload(int tab_id, bool* success) { 271 void Automation::Reload(int tab_id, bool* success) {
301 TabProxy* tab = GetTabById(tab_id); 272 int windex = 0, tab_index = 0;
302 if (!tab) { 273 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
303 *success = false; 274 *success = false;
304 return; 275 return;
305 } 276 }
306 *success = tab->Reload(); 277
278 *success = SendReloadJSONRequest(automation(), windex, tab_index);
307 } 279 }
308 280
309 void Automation::GetURL(int tab_id, 281 void Automation::GetURL(int tab_id,
310 std::string* url, 282 std::string* url,
311 bool* success) { 283 bool* success) {
312 TabProxy* tab = GetTabById(tab_id); 284 int windex = 0, tab_index = 0;
313 if (!tab) { 285 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
314 *success = false; 286 *success = false;
315 return; 287 return;
316 } 288 }
317 GURL gurl; 289
318 *success = tab->GetCurrentURL(&gurl); 290 *success = SendGetTabURLJSONRequest(automation(), windex, tab_index, url);
319 if (*success)
320 *url = gurl.possibly_invalid_spec();
321 } 291 }
322 292
323 void Automation::GetGURL(int tab_id, 293 void Automation::GetGURL(int tab_id,
324 GURL* gurl, 294 GURL* gurl,
325 bool* success) { 295 bool* success) {
326 TabProxy* tab = GetTabById(tab_id); 296 std::string url;
327 if (!tab) { 297 GetURL(tab_id, &url, success);
328 *success = false; 298 if (*success)
329 return; 299 *gurl = GURL(url);
330 }
331 *success = tab->GetCurrentURL(gurl);
332 } 300 }
333 301
334 void Automation::GetTabTitle(int tab_id, 302 void Automation::GetTabTitle(int tab_id,
335 std::string* tab_title, 303 std::string* tab_title,
336 bool* success) { 304 bool* success) {
337 TabProxy* tab = GetTabById(tab_id); 305 int windex = 0, tab_index = 0;
338 if (!tab) { 306 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
339 *success = false; 307 *success = false;
340 return; 308 return;
341 } 309 }
342 std::wstring wide_title; 310
343 *success = tab->GetTabTitle(&wide_title); 311 *success = SendGetTabTitleJSONRequest(
344 if (*success) 312 automation(), windex, tab_index, tab_title);
345 *tab_title = WideToUTF8(wide_title);
346 } 313 }
347 314
348 void Automation::GetCookies(int tab_id, 315 void Automation::GetCookies(int tab_id,
349 const GURL& gurl, 316 const GURL& gurl,
350 std::string* cookies, 317 std::string* cookies,
351 bool* success) { 318 bool* success) {
352 TabProxy* tab = GetTabById(tab_id); 319 int windex = 0, tab_index = 0;
353 if (!tab) { 320 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
354 *success = false; 321 *success = false;
355 return; 322 return;
356 } 323 }
357 *success = tab->GetCookies(gurl, cookies);
358 }
359 324
360 void Automation::GetCookieByName(int tab_id, 325 *success = SendGetCookiesJSONRequest(
361 const GURL& gurl, 326 automation(), windex, gurl.possibly_invalid_spec(), cookies);
362 const std::string& cookie_name,
363 std::string* cookie,
364 bool* success) {
365 TabProxy* tab = GetTabById(tab_id);
366 if (!tab) {
367 *success = false;
368 return;
369 }
370 *success = tab->GetCookieByName(gurl, cookie_name, cookie);
371 } 327 }
372 328
373 void Automation::DeleteCookie(int tab_id, 329 void Automation::DeleteCookie(int tab_id,
374 const GURL& gurl, 330 const GURL& gurl,
375 const std::string& cookie_name, 331 const std::string& cookie_name,
376 bool* success) { 332 bool* success) {
377 TabProxy* tab = GetTabById(tab_id); 333 int windex = 0, tab_index = 0;
378 if (!tab) { 334 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
379 *success = false; 335 *success = false;
380 return; 336 return;
381 } 337 }
382 *success = tab->DeleteCookie(gurl, cookie_name); 338
339 *success = SendDeleteCookieJSONRequest(
340 automation(),
341 windex,
342 gurl.possibly_invalid_spec(),
343 cookie_name);
383 } 344 }
384 345
385 void Automation::SetCookie(int tab_id, 346 void Automation::SetCookie(int tab_id,
386 const GURL& gurl, 347 const GURL& gurl,
387 const std::string& cookie, 348 const std::string& cookie,
388 bool* success) { 349 bool* success) {
389 TabProxy* tab = GetTabById(tab_id); 350 int windex = 0, tab_index = 0;
390 if (!tab) { 351 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
391 *success = false; 352 *success = false;
392 return; 353 return;
393 } 354 }
394 *success = tab->SetCookie(gurl, cookie); 355
356 *success = SendSetCookieJSONRequest(
357 automation(),
358 windex,
359 gurl.possibly_invalid_spec(),
360 cookie);
395 } 361 }
396 362
397 void Automation::GetTabIds(std::vector<int>* tab_ids, 363 void Automation::GetTabIds(std::vector<int>* tab_ids,
398 bool* success) { 364 bool* success) {
399 *success = false; 365 *success = SendGetTabIdsJSONRequest(automation(), tab_ids);
400 int browser_count = 0;
401 if (!automation()->GetBrowserWindowCount(&browser_count)) {
402 LOG(ERROR) << "Failed to get browser window count";
403 return;
404 }
405 TabIdMap tab_id_map;
406 for (int browser_index = 0; browser_index < browser_count; ++browser_index) {
407 scoped_refptr<BrowserProxy> browser =
408 automation()->GetBrowserWindow(browser_index);
409 if (!browser.get())
410 continue;
411 int tab_count = 0;
412 if (!browser->GetTabCount(&tab_count))
413 continue;
414
415 for (int tab_index = 0; tab_index < tab_count; ++tab_index) {
416 scoped_refptr<TabProxy> tab = browser->GetTab(tab_index);
417 if (!tab.get())
418 continue;
419 tab_ids->push_back(tab->handle());
420 tab_id_map.insert(std::make_pair(tab->handle(), tab));
421 }
422 }
423
424 tab_id_map_ = tab_id_map;
425 *success = true;
426 } 366 }
427 367
428 void Automation::DoesTabExist(int tab_id, bool* does_exist) { 368 void Automation::DoesTabExist(int tab_id, bool* does_exist, bool* success) {
429 TabProxy* tab = GetTabById(tab_id); 369 *success = SendIsTabIdValidJSONRequest(automation(), tab_id, does_exist);
430 *does_exist = tab && tab->is_valid();
431 } 370 }
432 371
433 void Automation::CloseTab(int tab_id, bool* success) { 372 void Automation::CloseTab(int tab_id, bool* success) {
434 TabProxy* tab = GetTabById(tab_id); 373 int windex = 0, tab_index = 0;
435 if (!tab) { 374 if (!GetIndicesForTab(tab_id, &windex, &tab_index)) {
436 *success = false; 375 *success = false;
437 return; 376 return;
438 } 377 }
439 *success = tab->Close(true); 378
379 *success = SendCloseTabJSONRequest(automation(), windex, tab_index);
440 } 380 }
441 381
442 void Automation::GetVersion(std::string* version) { 382 void Automation::GetVersion(std::string* version) {
443 *version = automation()->server_version(); 383 *version = automation()->server_version();
444 } 384 }
445 385
446 void Automation::WaitForAllTabsToStopLoading(bool* success) { 386 void Automation::WaitForAllTabsToStopLoading(bool* success) {
447 DictionaryValue dict; 387 *success = SendWaitForAllTabsToStopLoadingJSONRequest(automation());
448 dict.SetString("command", "WaitForAllTabsToStopLoading");
449 std::string request, reply;
450 base::JSONWriter::Write(&dict, false, &request);
451 *success = automation()->SendJSONRequest(request, &reply);
452 }
453
454 TabProxy* Automation::GetTabById(int tab_id) {
455 TabIdMap::const_iterator iter = tab_id_map_.find(tab_id);
456 if (iter != tab_id_map_.end()) {
457 return iter->second.get();
458 }
459 return NULL;
460 } 388 }
461 389
462 AutomationProxy* Automation::automation() const { 390 AutomationProxy* Automation::automation() const {
463 return launcher_->automation(); 391 return launcher_->automation();
464 } 392 }
465 393
466 bool Automation::SendJSONRequest(int tab_id,
467 const DictionaryValue& dict,
468 std::string* reply) {
469 std::string request;
470
471 base::JSONWriter::Write(&dict, false, &request);
472 TabProxy* tab = GetTabById(tab_id);
473 if (!tab) {
474 LOG(ERROR) << "No such tab";
475 return false;
476 }
477
478 int tab_index = 0;
479 if (!tab->GetTabIndex(&tab_index)) {
480 LOG(ERROR) << "Could not get tab index";
481 return false;
482 }
483
484 scoped_refptr<BrowserProxy> browser = tab->GetParentBrowser();
485 if (!browser.get()) {
486 LOG(ERROR) << "Could not get parent browser of tab";
487 return false;
488 }
489
490 if (!browser->ActivateTab(tab_index)) {
491 LOG(ERROR) << "Could not activate tab";
492 return false;
493 }
494
495 return browser->SendJSONRequest(request, reply);
496 }
497
498 bool Automation::GetIndicesForTab( 394 bool Automation::GetIndicesForTab(
499 int tab_id, int* browser_index, int* tab_index) { 395 int tab_id, int* browser_index, int* tab_index) {
500 if (!SendGetIndicesFromTabJSONRequest(automation(), tab_id, 396 if (!SendGetIndicesFromTabIdJSONRequest(automation(), tab_id,
501 browser_index, tab_index)) { 397 browser_index, tab_index)) {
502 LOG(ERROR) << "Could not get browser and tab indices for WebDriver tab id"; 398 LOG(ERROR) << "Could not get browser and tab indices for WebDriver tab id";
503 return false; 399 return false;
504 } 400 }
505 return true; 401 return true;
506 } 402 }
507 403
508 } // namespace webdriver 404 } // namespace webdriver
OLDNEW
« no previous file with comments | « chrome/test/webdriver/automation.h ('k') | chrome/test/webdriver/commands/mouse_commands.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698