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
|
// 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/element_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.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/chrome.h"
#include "chrome/test/chromedriver/chrome/js.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/version.h"
#include "chrome/test/chromedriver/chrome/web_view.h"
#include "chrome/test/chromedriver/session.h"
#include "third_party/webdriver/atoms.h"
namespace {
const char kElementKey[] = "ELEMENT";
bool ParseFromValue(base::Value* value, WebPoint* point) {
base::DictionaryValue* dict_value;
if (!value->GetAsDictionary(&dict_value))
return false;
double x = 0;
double y = 0;
if (!dict_value->GetDouble("x", &x) ||
!dict_value->GetDouble("y", &y))
return false;
point->x = static_cast<int>(x);
point->y = static_cast<int>(y);
return true;
}
bool ParseFromValue(base::Value* value, WebSize* size) {
base::DictionaryValue* dict_value;
if (!value->GetAsDictionary(&dict_value))
return false;
double width = 0;
double height = 0;
if (!dict_value->GetDouble("width", &width) ||
!dict_value->GetDouble("height", &height))
return false;
size->width = static_cast<int>(width);
size->height = static_cast<int>(height);
return true;
}
bool ParseFromValue(base::Value* value, WebRect* rect) {
base::DictionaryValue* dict_value;
if (!value->GetAsDictionary(&dict_value))
return false;
double x = 0;
double y = 0;
double width = 0;
double height = 0;
if (!dict_value->GetDouble("left", &x) ||
!dict_value->GetDouble("top", &y) ||
!dict_value->GetDouble("width", &width) ||
!dict_value->GetDouble("height", &height))
return false;
rect->origin.x = static_cast<int>(x);
rect->origin.y = static_cast<int>(y);
rect->size.width = static_cast<int>(width);
rect->size.height = static_cast<int>(height);
return true;
}
base::Value* CreateValueFrom(const WebRect& rect) {
base::DictionaryValue* dict = new base::DictionaryValue();
dict->SetInteger("left", rect.X());
dict->SetInteger("top", rect.Y());
dict->SetInteger("width", rect.Width());
dict->SetInteger("height", rect.Height());
return dict;
}
Status CallAtomsJs(
const std::string& frame,
WebView* web_view,
const char* const* atom_function,
const base::ListValue& args,
scoped_ptr<base::Value>* result) {
return web_view->CallFunction(
frame, webdriver::atoms::asString(atom_function), args, result);
}
Status VerifyElementClickable(
const std::string& frame,
WebView* web_view,
const std::string& element_id,
const WebPoint& location) {
base::ListValue args;
args.Append(CreateElement(element_id));
args.Append(CreateValueFrom(location));
scoped_ptr<base::Value> result;
Status status = CallAtomsJs(
frame, web_view, webdriver::atoms::IS_ELEMENT_CLICKABLE,
args, &result);
if (status.IsError())
return status;
base::DictionaryValue* dict;
bool is_clickable = false;
if (!result->GetAsDictionary(&dict) ||
!dict->GetBoolean("clickable", &is_clickable)) {
return Status(kUnknownError,
"failed to parse value of IS_ELEMENT_CLICKABLE");
}
if (!is_clickable) {
std::string message;
if (!dict->GetString("message", &message))
message = "element is not clickable";
return Status(kUnknownError, message);
}
return Status(kOk);
}
Status ScrollElementRegionIntoViewHelper(
const std::string& frame,
WebView* web_view,
const std::string& element_id,
const WebRect& region,
bool center,
const std::string& clickable_element_id,
WebPoint* location) {
WebPoint tmp_location = *location;
base::ListValue args;
args.Append(CreateElement(element_id));
args.AppendBoolean(center);
args.Append(CreateValueFrom(region));
scoped_ptr<base::Value> result;
Status status = web_view->CallFunction(
frame, webdriver::atoms::asString(webdriver::atoms::GET_LOCATION_IN_VIEW),
args, &result);
if (status.IsError())
return status;
if (!ParseFromValue(result.get(), &tmp_location)) {
return Status(kUnknownError,
"failed to parse value of GET_LOCATION_IN_VIEW");
}
if (!clickable_element_id.empty()) {
WebPoint middle = tmp_location;
middle.Offset(region.Width() / 2, region.Height() / 2);
status = VerifyElementClickable(
frame, web_view, clickable_element_id, middle);
if (status.IsError())
return status;
}
*location = tmp_location;
return Status(kOk);
}
Status GetElementEffectiveStyle(
const std::string& frame,
WebView* web_view,
const std::string& element_id,
const std::string& property,
std::string* value) {
base::ListValue args;
args.Append(CreateElement(element_id));
args.AppendString(property);
scoped_ptr<base::Value> result;
Status status = web_view->CallFunction(
frame, webdriver::atoms::asString(webdriver::atoms::GET_EFFECTIVE_STYLE),
args, &result);
if (status.IsError())
return status;
if (!result->GetAsString(value)) {
return Status(kUnknownError,
"failed to parse value of GET_EFFECTIVE_STYLE");
}
return Status(kOk);
}
Status GetElementBorder(
const std::string& frame,
WebView* web_view,
const std::string& element_id,
int* border_left,
int* border_top) {
std::string border_left_str;
Status status = GetElementEffectiveStyle(
frame, web_view, element_id, "border-left-width", &border_left_str);
if (status.IsError())
return status;
std::string border_top_str;
status = GetElementEffectiveStyle(
frame, web_view, element_id, "border-top-width", &border_top_str);
if (status.IsError())
return status;
int border_left_tmp = -1;
int border_top_tmp = -1;
base::StringToInt(border_left_str, &border_left_tmp);
base::StringToInt(border_top_str, &border_top_tmp);
if (border_left_tmp == -1 || border_top_tmp == -1)
return Status(kUnknownError, "failed to get border width of element");
*border_left = border_left_tmp;
*border_top = border_top_tmp;
return Status(kOk);
}
} // namespace
base::DictionaryValue* CreateElement(const std::string& element_id) {
base::DictionaryValue* element = new base::DictionaryValue();
element->SetString(kElementKey, element_id);
return element;
}
base::Value* CreateValueFrom(const WebPoint& point) {
base::DictionaryValue* dict = new base::DictionaryValue();
dict->SetInteger("x", point.x);
dict->SetInteger("y", point.y);
return dict;
}
Status FindElement(
int interval_ms,
bool only_one,
const std::string* root_element_id,
Session* session,
WebView* web_view,
const base::DictionaryValue& params,
scoped_ptr<base::Value>* value) {
std::string strategy;
if (!params.GetString("using", &strategy))
return Status(kUnknownError, "'using' must be a string");
std::string target;
if (!params.GetString("value", &target))
return Status(kUnknownError, "'value' must be a string");
std::string script;
if (only_one)
script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT);
else
script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS);
scoped_ptr<base::DictionaryValue> locator(new base::DictionaryValue());
locator->SetString(strategy, target);
base::ListValue arguments;
arguments.Append(locator.release());
if (root_element_id)
arguments.Append(CreateElement(*root_element_id));
base::TimeTicks start_time = base::TimeTicks::Now();
while (true) {
scoped_ptr<base::Value> temp;
Status status = web_view->CallFunction(
session->GetCurrentFrameId(), script, arguments, &temp);
if (status.IsError())
return status;
if (!temp->IsType(base::Value::TYPE_NULL)) {
if (only_one) {
value->reset(temp.release());
return Status(kOk);
} else {
base::ListValue* result;
if (!temp->GetAsList(&result))
return Status(kUnknownError, "script returns unexpected result");
if (result->GetSize() > 0U) {
value->reset(temp.release());
return Status(kOk);
}
}
}
if (base::TimeTicks::Now() - start_time >= session->implicit_wait) {
if (only_one) {
return Status(kNoSuchElement);
} else {
value->reset(new base::ListValue());
return Status(kOk);
}
}
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(interval_ms));
}
return Status(kUnknownError);
}
Status GetActiveElement(
Session* session,
WebView* web_view,
scoped_ptr<base::Value>* value) {
base::ListValue args;
return web_view->CallFunction(
session->GetCurrentFrameId(),
"function() { return document.activeElement || document.body }",
args,
value);
}
Status IsElementFocused(
Session* session,
WebView* web_view,
const std::string& element_id,
bool* is_focused) {
scoped_ptr<base::Value> result;
Status status = GetActiveElement(session, web_view, &result);
if (status.IsError())
return status;
scoped_ptr<base::Value> element_dict(CreateElement(element_id));
*is_focused = result->Equals(element_dict.get());
return Status(kOk);
}
Status GetElementAttribute(
Session* session,
WebView* web_view,
const std::string& element_id,
const std::string& attribute_name,
scoped_ptr<base::Value>* value) {
base::ListValue args;
args.Append(CreateElement(element_id));
args.AppendString(attribute_name);
return CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::GET_ATTRIBUTE,
args, value);
}
Status IsElementAttributeEqualToIgnoreCase(
Session* session,
WebView* web_view,
const std::string& element_id,
const std::string& attribute_name,
const std::string& attribute_value,
bool* is_equal) {
scoped_ptr<base::Value> result;
Status status = GetElementAttribute(
session, web_view, element_id, attribute_name, &result);
if (status.IsError())
return status;
std::string actual_value;
if (result->GetAsString(&actual_value))
*is_equal = LowerCaseEqualsASCII(actual_value, attribute_value.c_str());
else
*is_equal = false;
return status;
}
Status GetElementClickableLocation(
Session* session,
WebView* web_view,
const std::string& element_id,
WebPoint* location) {
std::string tag_name;
Status status = GetElementTagName(session, web_view, element_id, &tag_name);
if (status.IsError())
return status;
std::string target_element_id = element_id;
if (tag_name == "area") {
// Scroll the image into view instead of the area.
const char* kGetImageElementForArea =
"function (element) {"
" var map = element.parentElement;"
" if (map.tagName.toLowerCase() != 'map')"
" throw new Error('the area is not within a map');"
" var mapName = map.getAttribute('name');"
" if (mapName == null)"
" throw new Error ('area\\'s parent map must have a name');"
" mapName = '#' + mapName.toLowerCase();"
" var images = document.getElementsByTagName('img');"
" for (var i = 0; i < images.length; i++) {"
" if (images[i].useMap.toLowerCase() == mapName)"
" return images[i];"
" }"
" throw new Error('no img is found for the area');"
"}";
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
status = web_view->CallFunction(
session->GetCurrentFrameId(), kGetImageElementForArea, args, &result);
if (status.IsError())
return status;
const base::DictionaryValue* element_dict;
if (!result->GetAsDictionary(&element_dict) ||
!element_dict->GetString(kElementKey, &target_element_id))
return Status(kUnknownError, "no element reference returned by script");
}
bool is_displayed = false;
status = IsElementDisplayed(
session, web_view, target_element_id, true, &is_displayed);
if (status.IsError())
return status;
if (!is_displayed)
return Status(kElementNotVisible);
WebRect rect;
status = GetElementRegion(session, web_view, element_id, &rect);
if (status.IsError())
return status;
std::string tmp_element_id = element_id;
int build_no = session->chrome->GetBrowserInfo()->build_no;
if (tag_name == "area" && build_no < 1799 && build_no >= 1666) {
// This is to skip clickable verification for <area>.
// The problem is caused by document.ElementFromPoint(crbug.com/338601).
// It was introduced by blink r159012, which rolled into chromium r227489.
// And it was fixed in blink r165426, which rolled into chromium r245994.
// TODO(stgao): Revert after 33 is not supported.
tmp_element_id = std::string();
}
status = ScrollElementRegionIntoView(
session, web_view, target_element_id, rect,
true /* center */, tmp_element_id, location);
if (status.IsError())
return status;
location->Offset(rect.Width() / 2, rect.Height() / 2);
return Status(kOk);
}
Status GetElementEffectiveStyle(
Session* session,
WebView* web_view,
const std::string& element_id,
const std::string& property_name,
std::string* property_value) {
return GetElementEffectiveStyle(session->GetCurrentFrameId(), web_view,
element_id, property_name, property_value);
}
Status GetElementRegion(
Session* session,
WebView* web_view,
const std::string& element_id,
WebRect* rect) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = web_view->CallFunction(
session->GetCurrentFrameId(), kGetElementRegionScript, args, &result);
if (status.IsError())
return status;
if (!ParseFromValue(result.get(), rect)) {
return Status(kUnknownError,
"failed to parse value of getElementRegion");
}
return Status(kOk);
}
Status GetElementTagName(
Session* session,
WebView* web_view,
const std::string& element_id,
std::string* name) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = web_view->CallFunction(
session->GetCurrentFrameId(),
"function(elem) { return elem.tagName.toLowerCase(); }",
args, &result);
if (status.IsError())
return status;
if (!result->GetAsString(name))
return Status(kUnknownError, "failed to get element tag name");
return Status(kOk);
}
Status GetElementSize(
Session* session,
WebView* web_view,
const std::string& element_id,
WebSize* size) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::GET_SIZE,
args, &result);
if (status.IsError())
return status;
if (!ParseFromValue(result.get(), size))
return Status(kUnknownError, "failed to parse value of GET_SIZE");
return Status(kOk);
}
Status IsElementDisplayed(
Session* session,
WebView* web_view,
const std::string& element_id,
bool ignore_opacity,
bool* is_displayed) {
base::ListValue args;
args.Append(CreateElement(element_id));
args.AppendBoolean(ignore_opacity);
scoped_ptr<base::Value> result;
Status status = CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::IS_DISPLAYED,
args, &result);
if (status.IsError())
return status;
if (!result->GetAsBoolean(is_displayed))
return Status(kUnknownError, "IS_DISPLAYED should return a boolean value");
return Status(kOk);
}
Status IsElementEnabled(
Session* session,
WebView* web_view,
const std::string& element_id,
bool* is_enabled) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::IS_ENABLED,
args, &result);
if (status.IsError())
return status;
if (!result->GetAsBoolean(is_enabled))
return Status(kUnknownError, "IS_ENABLED should return a boolean value");
return Status(kOk);
}
Status IsOptionElementSelected(
Session* session,
WebView* web_view,
const std::string& element_id,
bool* is_selected) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::IS_SELECTED,
args, &result);
if (status.IsError())
return status;
if (!result->GetAsBoolean(is_selected))
return Status(kUnknownError, "IS_SELECTED should return a boolean value");
return Status(kOk);
}
Status IsOptionElementTogglable(
Session* session,
WebView* web_view,
const std::string& element_id,
bool* is_togglable) {
base::ListValue args;
args.Append(CreateElement(element_id));
scoped_ptr<base::Value> result;
Status status = web_view->CallFunction(
session->GetCurrentFrameId(), kIsOptionElementToggleableScript,
args, &result);
if (status.IsError())
return status;
if (!result->GetAsBoolean(is_togglable))
return Status(kUnknownError, "failed check if option togglable or not");
return Status(kOk);
}
Status SetOptionElementSelected(
Session* session,
WebView* web_view,
const std::string& element_id,
bool selected) {
// TODO(171034): need to fix throwing error if an alert is triggered.
base::ListValue args;
args.Append(CreateElement(element_id));
args.AppendBoolean(selected);
scoped_ptr<base::Value> result;
return CallAtomsJs(
session->GetCurrentFrameId(), web_view, webdriver::atoms::CLICK,
args, &result);
}
Status ToggleOptionElement(
Session* session,
WebView* web_view,
const std::string& element_id) {
bool is_selected;
Status status = IsOptionElementSelected(
session, web_view, element_id, &is_selected);
if (status.IsError())
return status;
return SetOptionElementSelected(session, web_view, element_id, !is_selected);
}
Status ScrollElementIntoView(
Session* session,
WebView* web_view,
const std::string& id,
WebPoint* location) {
WebSize size;
Status status = GetElementSize(session, web_view, id, &size);
if (status.IsError())
return status;
return ScrollElementRegionIntoView(
session, web_view, id, WebRect(WebPoint(0, 0), size),
false /* center */, std::string(), location);
}
Status ScrollElementRegionIntoView(
Session* session,
WebView* web_view,
const std::string& element_id,
const WebRect& region,
bool center,
const std::string& clickable_element_id,
WebPoint* location) {
WebPoint region_offset = region.origin;
WebSize region_size = region.size;
Status status = ScrollElementRegionIntoViewHelper(
session->GetCurrentFrameId(), web_view, element_id, region,
center, clickable_element_id, ®ion_offset);
if (status.IsError())
return status;
const char* kFindSubFrameScript =
"function(xpath) {"
" return document.evaluate(xpath, document, null,"
" XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
"}";
for (std::list<FrameInfo>::reverse_iterator rit = session->frames.rbegin();
rit != session->frames.rend(); ++rit) {
base::ListValue args;
args.AppendString(
base::StringPrintf("//*[@cd_frame_id_ = '%s']",
rit->chromedriver_frame_id.c_str()));
scoped_ptr<base::Value> result;
status = web_view->CallFunction(
rit->parent_frame_id, kFindSubFrameScript, args, &result);
if (status.IsError())
return status;
const base::DictionaryValue* element_dict;
if (!result->GetAsDictionary(&element_dict))
return Status(kUnknownError, "no element reference returned by script");
std::string frame_element_id;
if (!element_dict->GetString(kElementKey, &frame_element_id))
return Status(kUnknownError, "failed to locate a sub frame");
// Modify |region_offset| by the frame's border.
int border_left = -1;
int border_top = -1;
status = GetElementBorder(
rit->parent_frame_id, web_view, frame_element_id,
&border_left, &border_top);
if (status.IsError())
return status;
region_offset.Offset(border_left, border_top);
status = ScrollElementRegionIntoViewHelper(
rit->parent_frame_id, web_view, frame_element_id,
WebRect(region_offset, region_size),
center, frame_element_id, ®ion_offset);
if (status.IsError())
return status;
}
*location = region_offset;
return Status(kOk);
}
|