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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
// Copyright (c) 2013 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.
#include "chrome/test/chromedriver/window_commands.h"
#include <list>
#include <string>
#include "base/callback.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/test/chromedriver/basic_types.h"
#include "chrome/test/chromedriver/chrome/automation_extension.h"
#include "chrome/test/chromedriver/chrome/chrome.h"
#include "chrome/test/chromedriver/chrome/chrome_desktop_impl.h"
#include "chrome/test/chromedriver/chrome/devtools_client.h"
#include "chrome/test/chromedriver/chrome/geoposition.h"
#include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h"
#include "chrome/test/chromedriver/chrome/js.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/ui_events.h"
#include "chrome/test/chromedriver/chrome/web_view.h"
#include "chrome/test/chromedriver/element_util.h"
#include "chrome/test/chromedriver/session.h"
#include "chrome/test/chromedriver/util.h"
namespace {
Status GetMouseButton(const base::DictionaryValue& params,
MouseButton* button) {
int button_num;
if (!params.GetInteger("button", &button_num)) {
button_num = 0; // Default to left mouse button.
} else if (button_num < 0 || button_num > 2) {
return Status(kUnknownError,
base::StringPrintf("invalid button: %d", button_num));
}
*button = static_cast<MouseButton>(button_num);
return Status(kOk);
}
Status GetUrl(WebView* web_view, const std::string& frame, std::string* url) {
scoped_ptr<base::Value> value;
base::ListValue args;
Status status = web_view->CallFunction(
frame, "function() { return document.URL; }", args, &value);
if (status.IsError())
return status;
if (!value->GetAsString(url))
return Status(kUnknownError, "javascript failed to return the url");
return Status(kOk);
}
struct Cookie {
Cookie(const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
double expiry,
bool secure,
bool session)
: name(name), value(value), domain(domain), path(path), expiry(expiry),
secure(secure), session(session) {}
std::string name;
std::string value;
std::string domain;
std::string path;
double expiry;
bool secure;
bool session;
};
base::DictionaryValue* CreateDictionaryFrom(const Cookie& cookie) {
base::DictionaryValue* dict = new base::DictionaryValue();
dict->SetString("name", cookie.name);
dict->SetString("value", cookie.value);
if (!cookie.domain.empty())
dict->SetString("domain", cookie.domain);
if (!cookie.path.empty())
dict->SetString("path", cookie.path);
if (!cookie.session)
dict->SetDouble("expiry", cookie.expiry);
dict->SetBoolean("secure", cookie.secure);
return dict;
}
Status GetVisibleCookies(WebView* web_view,
std::list<Cookie>* cookies) {
scoped_ptr<base::ListValue> internal_cookies;
Status status = web_view->GetCookies(&internal_cookies);
if (status.IsError())
return status;
std::list<Cookie> cookies_tmp;
for (size_t i = 0; i < internal_cookies->GetSize(); ++i) {
base::DictionaryValue* cookie_dict;
if (!internal_cookies->GetDictionary(i, &cookie_dict))
return Status(kUnknownError, "DevTools returns a non-dictionary cookie");
std::string name;
cookie_dict->GetString("name", &name);
std::string value;
cookie_dict->GetString("value", &value);
std::string domain;
cookie_dict->GetString("domain", &domain);
std::string path;
cookie_dict->GetString("path", &path);
double expiry = 0;
cookie_dict->GetDouble("expires", &expiry);
expiry /= 1000; // Convert from millisecond to second.
bool session = false;
cookie_dict->GetBoolean("session", &session);
bool secure = false;
cookie_dict->GetBoolean("secure", &secure);
cookies_tmp.push_back(
Cookie(name, value, domain, path, expiry, secure, session));
}
cookies->swap(cookies_tmp);
return Status(kOk);
}
Status ScrollCoordinateInToView(
Session* session, WebView* web_view, int x, int y, int* offset_x,
int* offset_y) {
scoped_ptr<base::Value> value;
base::ListValue args;
args.AppendInteger(x);
args.AppendInteger(y);
Status status = web_view->CallFunction(
std::string(),
"function(x, y) {"
" if (x < window.pageXOffset ||"
" x >= window.pageXOffset + window.innerWidth ||"
" y < window.pageYOffset ||"
" y >= window.pageYOffset + window.innerHeight) {"
" window.scrollTo(x - window.innerWidth/2, y - window.innerHeight/2);"
" }"
" return {"
" view_x: Math.floor(window.pageXOffset),"
" view_y: Math.floor(window.pageYOffset),"
" view_width: Math.floor(window.innerWidth),"
" view_height: Math.floor(window.innerHeight)};"
"}",
args,
&value);
if (!status.IsOk())
return status;
base::DictionaryValue* view_attrib;
value->GetAsDictionary(&view_attrib);
int view_x, view_y, view_width, view_height;
view_attrib->GetInteger("view_x", &view_x);
view_attrib->GetInteger("view_y", &view_y);
view_attrib->GetInteger("view_width", &view_width);
view_attrib->GetInteger("view_height", &view_height);
*offset_x = x - view_x;
*offset_y = y - view_y;
if (*offset_x < 0 || *offset_x >= view_width || *offset_y < 0 ||
*offset_y >= view_height)
return Status(kUnknownError, "Failed to scroll coordinate into view");
return Status(kOk);
}
Status ExecuteTouchEvent(
Session* session, WebView* web_view, TouchEventType type,
const base::DictionaryValue& params) {
int x, y;
if (!params.GetInteger("x", &x))
return Status(kUnknownError, "'x' must be an integer");
if (!params.GetInteger("y", &y))
return Status(kUnknownError, "'y' must be an integer");
int relative_x = x;
int relative_y = y;
Status status = ScrollCoordinateInToView(
session, web_view, x, y, &relative_x, &relative_y);
if (!status.IsOk())
return status;
std::list<TouchEvent> events;
events.push_back(
TouchEvent(type, relative_x, relative_y));
return web_view->DispatchTouchEvents(events);
}
} // namespace
Status ExecuteWindowCommand(
const WindowCommand& command,
Session* session,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
WebView* web_view = NULL;
Status status = session->GetTargetWindow(&web_view);
if (status.IsError())
return status;
status = web_view->ConnectIfNecessary();
if (status.IsError())
return status;
status = web_view->HandleReceivedEvents();
if (status.IsError())
return status;
if (web_view->GetJavaScriptDialogManager()->IsDialogOpen())
return Status(kUnexpectedAlertOpen);
Status nav_status(kOk);
for (int attempt = 0; attempt < 2; attempt++) {
if (attempt == 1) {
if (status.code() == kNoSuchExecutionContext)
// Switch to main frame and retry command if subframe no longer exists.
session->SwitchToTopFrame();
else
break;
}
nav_status = web_view->WaitForPendingNavigations(
session->GetCurrentFrameId(), session->page_load_timeout, true);
if (nav_status.IsError())
return nav_status;
status = command.Run(session, web_view, params, value);
}
nav_status = web_view->WaitForPendingNavigations(
session->GetCurrentFrameId(), session->page_load_timeout, true);
if (status.IsOk() && nav_status.IsError() &&
nav_status.code() != kUnexpectedAlertOpen)
return nav_status;
if (status.code() == kUnexpectedAlertOpen)
return Status(kOk);
return status;
}
Status ExecuteGet(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string url;
if (!params.GetString("url", &url))
return Status(kUnknownError, "'url' must be a string");
return web_view->Load(url);
}
Status ExecuteExecuteScript(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string script;
if (!params.GetString("script", &script))
return Status(kUnknownError, "'script' must be a string");
if (script == ":takeHeapSnapshot") {
return web_view->TakeHeapSnapshot(value);
} else if (script == ":startProfile") {
return web_view->StartProfile();
} else if (script == ":endProfile") {
return web_view->EndProfile(value);
} else {
const base::ListValue* args;
if (!params.GetList("args", &args))
return Status(kUnknownError, "'args' must be a list");
return web_view->CallFunction(session->GetCurrentFrameId(),
"function(){" + script + "}", *args, value);
}
}
Status ExecuteExecuteAsyncScript(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string script;
if (!params.GetString("script", &script))
return Status(kUnknownError, "'script' must be a string");
const base::ListValue* args;
if (!params.GetList("args", &args))
return Status(kUnknownError, "'args' must be a list");
return web_view->CallUserAsyncFunction(
session->GetCurrentFrameId(), "function(){" + script + "}", *args,
session->script_timeout, value);
}
Status ExecuteSwitchToFrame(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const base::Value* id;
if (!params.Get("id", &id))
return Status(kUnknownError, "missing 'id'");
if (id->IsType(base::Value::TYPE_NULL)) {
session->SwitchToTopFrame();
return Status(kOk);
}
std::string script;
base::ListValue args;
const base::DictionaryValue* id_dict;
if (id->GetAsDictionary(&id_dict)) {
script = "function(elem) { return elem; }";
args.Append(id_dict->DeepCopy());
} else {
script =
"function(xpath) {"
" return document.evaluate(xpath, document, null, "
" XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
"}";
std::string xpath = "(/html/body//iframe|/html/frameset/frame)";
std::string id_string;
int id_int;
if (id->GetAsString(&id_string)) {
xpath += base::StringPrintf(
"[@name=\"%s\" or @id=\"%s\"]", id_string.c_str(), id_string.c_str());
} else if (id->GetAsInteger(&id_int)) {
xpath += base::StringPrintf("[%d]", id_int + 1);
} else {
return Status(kUnknownError, "invalid 'id'");
}
args.Append(new base::StringValue(xpath));
}
std::string frame;
Status status = web_view->GetFrameByFunction(
session->GetCurrentFrameId(), script, args, &frame);
if (status.IsError())
return status;
scoped_ptr<base::Value> result;
status = web_view->CallFunction(
session->GetCurrentFrameId(), script, args, &result);
if (status.IsError())
return status;
const base::DictionaryValue* element;
if (!result->GetAsDictionary(&element))
return Status(kUnknownError, "fail to locate the sub frame element");
std::string chrome_driver_id = GenerateId();
const char* kSetFrameIdentifier =
"function(frame, id) {"
" frame.setAttribute('cd_frame_id_', id);"
"}";
base::ListValue new_args;
new_args.Append(element->DeepCopy());
new_args.AppendString(chrome_driver_id);
result.reset(NULL);
status = web_view->CallFunction(
session->GetCurrentFrameId(), kSetFrameIdentifier, new_args, &result);
if (status.IsError())
return status;
session->SwitchToSubFrame(frame, chrome_driver_id);
return Status(kOk);
}
Status ExecuteSwitchToParentFrame(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
session->SwitchToParentFrame();
return Status(kOk);
}
Status ExecuteGetTitle(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const char* kGetTitleScript =
"function() {"
" if (document.title)"
" return document.title;"
" else"
" return document.URL;"
"}";
base::ListValue args;
return web_view->CallFunction(std::string(), kGetTitleScript, args, value);
}
Status ExecuteGetPageSource(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const char* kGetPageSource =
"function() {"
" return new XMLSerializer().serializeToString(document);"
"}";
base::ListValue args;
return web_view->CallFunction(
session->GetCurrentFrameId(), kGetPageSource, args, value);
}
Status ExecuteFindElement(
int interval_ms,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return FindElement(interval_ms, true, NULL, session, web_view, params, value);
}
Status ExecuteFindElements(
int interval_ms,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return FindElement(
interval_ms, false, NULL, session, web_view, params, value);
}
Status ExecuteGetCurrentUrl(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string url;
Status status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
if (status.IsError())
return status;
value->reset(new base::StringValue(url));
return Status(kOk);
}
Status ExecuteGoBack(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
std::string(), "window.history.back();", value);
}
Status ExecuteGoForward(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
std::string(), "window.history.forward();", value);
}
Status ExecuteRefresh(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->Reload();
}
Status ExecuteMouseMoveTo(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string element_id;
bool has_element = params.GetString("element", &element_id);
int x_offset = 0;
int y_offset = 0;
bool has_offset = params.GetInteger("xoffset", &x_offset) &&
params.GetInteger("yoffset", &y_offset);
if (!has_element && !has_offset)
return Status(kUnknownError, "at least an element or offset should be set");
WebPoint location;
if (has_element) {
Status status = ScrollElementIntoView(
session, web_view, element_id, &location);
if (status.IsError())
return status;
} else {
location = session->mouse_position;
}
if (has_offset) {
location.Offset(x_offset, y_offset);
} else {
WebSize size;
Status status = GetElementSize(session, web_view, element_id, &size);
if (status.IsError())
return status;
location.Offset(size.width / 2, size.height / 2);
}
std::list<MouseEvent> events;
events.push_back(
MouseEvent(kMovedMouseEventType, kNoneMouseButton,
location.x, location.y, session->sticky_modifiers, 0));
Status status =
web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
if (status.IsOk())
session->mouse_position = location;
return status;
}
Status ExecuteMouseClick(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
MouseButton button;
Status status = GetMouseButton(params, &button);
if (status.IsError())
return status;
std::list<MouseEvent> events;
events.push_back(
MouseEvent(kPressedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 1));
events.push_back(
MouseEvent(kReleasedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 1));
return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
}
Status ExecuteMouseButtonDown(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
MouseButton button;
Status status = GetMouseButton(params, &button);
if (status.IsError())
return status;
std::list<MouseEvent> events;
events.push_back(
MouseEvent(kPressedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 1));
return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
}
Status ExecuteMouseButtonUp(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
MouseButton button;
Status status = GetMouseButton(params, &button);
if (status.IsError())
return status;
std::list<MouseEvent> events;
events.push_back(
MouseEvent(kReleasedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 1));
return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
}
Status ExecuteMouseDoubleClick(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
MouseButton button;
Status status = GetMouseButton(params, &button);
if (status.IsError())
return status;
std::list<MouseEvent> events;
events.push_back(
MouseEvent(kPressedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 2));
events.push_back(
MouseEvent(kReleasedMouseEventType, button,
session->mouse_position.x, session->mouse_position.y,
session->sticky_modifiers, 2));
return web_view->DispatchMouseEvents(events, session->GetCurrentFrameId());
}
Status ExecuteTouchDown(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return ExecuteTouchEvent(session, web_view, kTouchStart, params);
}
Status ExecuteTouchUp(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return ExecuteTouchEvent(session, web_view, kTouchEnd, params);
}
Status ExecuteTouchMove(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return ExecuteTouchEvent(session, web_view, kTouchMove, params);
}
Status ExecuteGetActiveElement(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return GetActiveElement(session, web_view, value);
}
Status ExecuteSendKeysToActiveElement(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const base::ListValue* key_list;
if (!params.GetList("value", &key_list))
return Status(kUnknownError, "'value' must be a list");
return SendKeysOnWindow(
web_view, key_list, false, &session->sticky_modifiers);
}
Status ExecuteGetAppCacheStatus(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
session->GetCurrentFrameId(),
"applicationCache.status",
value);
}
Status ExecuteIsBrowserOnline(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
session->GetCurrentFrameId(),
"navigator.onLine",
value);
}
Status ExecuteGetStorageItem(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string key;
if (!params.GetString("key", &key))
return Status(kUnknownError, "'key' must be a string");
base::ListValue args;
args.Append(new base::StringValue(key));
return web_view->CallFunction(
session->GetCurrentFrameId(),
base::StringPrintf("function(key) { return %s[key]; }", storage),
args,
value);
}
Status ExecuteGetStorageKeys(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const char script[] =
"var keys = [];"
"for (var key in %s) {"
" keys.push(key);"
"}"
"keys";
return web_view->EvaluateScript(
session->GetCurrentFrameId(),
base::StringPrintf(script, storage),
value);
}
Status ExecuteSetStorageItem(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string key;
if (!params.GetString("key", &key))
return Status(kUnknownError, "'key' must be a string");
std::string storage_value;
if (!params.GetString("value", &storage_value))
return Status(kUnknownError, "'value' must be a string");
base::ListValue args;
args.Append(new base::StringValue(key));
args.Append(new base::StringValue(storage_value));
return web_view->CallFunction(
session->GetCurrentFrameId(),
base::StringPrintf("function(key, value) { %s[key] = value; }", storage),
args,
value);
}
Status ExecuteRemoveStorageItem(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string key;
if (!params.GetString("key", &key))
return Status(kUnknownError, "'key' must be a string");
base::ListValue args;
args.Append(new base::StringValue(key));
return web_view->CallFunction(
session->GetCurrentFrameId(),
base::StringPrintf("function(key) { %s.removeItem(key) }", storage),
args,
value);
}
Status ExecuteClearStorage(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
session->GetCurrentFrameId(),
base::StringPrintf("%s.clear()", storage),
value);
}
Status ExecuteGetStorageSize(
const char* storage,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->EvaluateScript(
session->GetCurrentFrameId(),
base::StringPrintf("%s.length", storage),
value);
}
Status ExecuteScreenshot(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
Status status = session->chrome->ActivateWebView(web_view->GetId());
if (status.IsError())
return status;
std::string screenshot;
if (session->chrome->GetAsDesktop() && !session->force_devtools_screenshot) {
AutomationExtension* extension = NULL;
status =
session->chrome->GetAsDesktop()->GetAutomationExtension(&extension);
if (status.IsError())
return status;
status = extension->CaptureScreenshot(&screenshot);
// If the screenshot was forbidden, fallback to DevTools.
if (status.code() == kForbidden)
status = web_view->CaptureScreenshot(&screenshot);
} else {
status = web_view->CaptureScreenshot(&screenshot);
}
if (status.IsError())
return status;
value->reset(new base::StringValue(screenshot));
return Status(kOk);
}
Status ExecuteGetCookies(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::list<Cookie> cookies;
Status status = GetVisibleCookies(web_view, &cookies);
if (status.IsError())
return status;
scoped_ptr<base::ListValue> cookie_list(new base::ListValue());
for (std::list<Cookie>::const_iterator it = cookies.begin();
it != cookies.end(); ++it) {
cookie_list->Append(CreateDictionaryFrom(*it));
}
value->reset(cookie_list.release());
return Status(kOk);
}
Status ExecuteAddCookie(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const base::DictionaryValue* cookie;
if (!params.GetDictionary("cookie", &cookie))
return Status(kUnknownError, "missing 'cookie'");
base::ListValue args;
args.Append(cookie->DeepCopy());
scoped_ptr<base::Value> result;
return web_view->CallFunction(
session->GetCurrentFrameId(), kAddCookieScript, args, &result);
}
Status ExecuteDeleteCookie(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string name;
if (!params.GetString("name", &name))
return Status(kUnknownError, "missing 'name'");
base::DictionaryValue params_url;
scoped_ptr<base::Value> value_url;
std::string url;
Status status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
if (status.IsError())
return status;
return web_view->DeleteCookie(name, url);
}
Status ExecuteDeleteAllCookies(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::list<Cookie> cookies;
Status status = GetVisibleCookies(web_view, &cookies);
if (status.IsError())
return status;
if (!cookies.empty()) {
base::DictionaryValue params_url;
scoped_ptr<base::Value> value_url;
std::string url;
status = GetUrl(web_view, session->GetCurrentFrameId(), &url);
if (status.IsError())
return status;
for (std::list<Cookie>::const_iterator it = cookies.begin();
it != cookies.end(); ++it) {
status = web_view->DeleteCookie(it->name, url);
if (status.IsError())
return status;
}
}
return Status(kOk);
}
Status ExecuteSetLocation(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
const base::DictionaryValue* location = NULL;
Geoposition geoposition;
if (!params.GetDictionary("location", &location) ||
!location->GetDouble("latitude", &geoposition.latitude) ||
!location->GetDouble("longitude", &geoposition.longitude))
return Status(kUnknownError, "missing or invalid 'location'");
if (location->HasKey("accuracy") &&
!location->GetDouble("accuracy", &geoposition.accuracy)) {
return Status(kUnknownError, "invalid 'accuracy'");
} else {
// |accuracy| is not part of the WebDriver spec yet, so if it is not given
// default to 100 meters accuracy.
geoposition.accuracy = 100;
}
Status status = web_view->OverrideGeolocation(geoposition);
if (status.IsOk())
session->overridden_geoposition.reset(new Geoposition(geoposition));
return status;
}
Status ExecuteTakeHeapSnapshot(
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
return web_view->TakeHeapSnapshot(value);
}
|