| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/chromedriver/element_util.h" | 5 #include "chrome/test/chromedriver/element_util.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/test/chromedriver/basic_types.h" | 15 #include "chrome/test/chromedriver/basic_types.h" |
| 16 #include "chrome/test/chromedriver/chrome/chrome.h" | 16 #include "chrome/test/chromedriver/chrome/chrome.h" |
| 17 #include "chrome/test/chromedriver/chrome/js.h" | 17 #include "chrome/test/chromedriver/chrome/js.h" |
| 18 #include "chrome/test/chromedriver/chrome/status.h" | 18 #include "chrome/test/chromedriver/chrome/status.h" |
| 19 #include "chrome/test/chromedriver/chrome/web_view.h" | 19 #include "chrome/test/chromedriver/chrome/web_view.h" |
| 20 #include "chrome/test/chromedriver/session.h" | 20 #include "chrome/test/chromedriver/session.h" |
| 21 #include "third_party/webdriver/atoms.h" | 21 #include "third_party/webdriver/atoms.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kElementKey[] = "ELEMENT"; | 25 const char kElementKey[] = "ELEMENT"; |
| 26 const char kElementKeyW3C[] = "element-6066-11e4-a52e-4f735466cecf"; |
| 27 |
| 28 std::string GetElementKey() { |
| 29 Session* session = GetThreadLocalSession(); |
| 30 if (session && session->w3c_compliant) |
| 31 return kElementKeyW3C; |
| 32 else |
| 33 return kElementKey; |
| 34 } |
| 26 | 35 |
| 27 bool ParseFromValue(base::Value* value, WebPoint* point) { | 36 bool ParseFromValue(base::Value* value, WebPoint* point) { |
| 28 base::DictionaryValue* dict_value; | 37 base::DictionaryValue* dict_value; |
| 29 if (!value->GetAsDictionary(&dict_value)) | 38 if (!value->GetAsDictionary(&dict_value)) |
| 30 return false; | 39 return false; |
| 31 double x = 0; | 40 double x = 0; |
| 32 double y = 0; | 41 double y = 0; |
| 33 if (!dict_value->GetDouble("x", &x) || | 42 if (!dict_value->GetDouble("x", &x) || |
| 34 !dict_value->GetDouble("y", &y)) | 43 !dict_value->GetDouble("y", &y)) |
| 35 return false; | 44 return false; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 *border_left = border_left_tmp; | 212 *border_left = border_left_tmp; |
| 204 *border_top = border_top_tmp; | 213 *border_top = border_top_tmp; |
| 205 return Status(kOk); | 214 return Status(kOk); |
| 206 } | 215 } |
| 207 | 216 |
| 208 } // namespace | 217 } // namespace |
| 209 | 218 |
| 210 std::unique_ptr<base::DictionaryValue> CreateElement( | 219 std::unique_ptr<base::DictionaryValue> CreateElement( |
| 211 const std::string& element_id) { | 220 const std::string& element_id) { |
| 212 std::unique_ptr<base::DictionaryValue> element(new base::DictionaryValue()); | 221 std::unique_ptr<base::DictionaryValue> element(new base::DictionaryValue()); |
| 213 element->SetString(kElementKey, element_id); | 222 element->SetString(GetElementKey(), element_id); |
| 214 return element; | 223 return element; |
| 215 } | 224 } |
| 216 | 225 |
| 217 std::unique_ptr<base::DictionaryValue> CreateValueFrom(const WebPoint& point) { | 226 std::unique_ptr<base::DictionaryValue> CreateValueFrom(const WebPoint& point) { |
| 218 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 227 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 219 dict->SetInteger("x", point.x); | 228 dict->SetInteger("x", point.x); |
| 220 dict->SetInteger("y", point.y); | 229 dict->SetInteger("y", point.y); |
| 221 return dict; | 230 return dict; |
| 222 } | 231 } |
| 223 | 232 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 return status; | 265 return status; |
| 257 | 266 |
| 258 if (!temp->IsType(base::Value::TYPE_NULL)) { | 267 if (!temp->IsType(base::Value::TYPE_NULL)) { |
| 259 if (only_one) { | 268 if (only_one) { |
| 260 value->reset(temp.release()); | 269 value->reset(temp.release()); |
| 261 return Status(kOk); | 270 return Status(kOk); |
| 262 } else { | 271 } else { |
| 263 base::ListValue* result; | 272 base::ListValue* result; |
| 264 if (!temp->GetAsList(&result)) | 273 if (!temp->GetAsList(&result)) |
| 265 return Status(kUnknownError, "script returns unexpected result"); | 274 return Status(kUnknownError, "script returns unexpected result"); |
| 266 | |
| 267 if (result->GetSize() > 0U) { | 275 if (result->GetSize() > 0U) { |
| 268 value->reset(temp.release()); | 276 value->reset(temp.release()); |
| 269 return Status(kOk); | 277 return Status(kOk); |
| 270 } | 278 } |
| 271 } | 279 } |
| 272 } | 280 } |
| 273 | 281 |
| 274 if (base::TimeTicks::Now() - start_time >= session->implicit_wait) { | 282 if (base::TimeTicks::Now() - start_time >= session->implicit_wait) { |
| 275 if (only_one) { | 283 if (only_one) { |
| 276 return Status(kNoSuchElement, "Unable to locate element: {\"method\":\"" | 284 return Status(kNoSuchElement, "Unable to locate element: {\"method\":\"" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 "}"; | 384 "}"; |
| 377 base::ListValue args; | 385 base::ListValue args; |
| 378 args.Append(CreateElement(element_id)); | 386 args.Append(CreateElement(element_id)); |
| 379 std::unique_ptr<base::Value> result; | 387 std::unique_ptr<base::Value> result; |
| 380 status = web_view->CallFunction( | 388 status = web_view->CallFunction( |
| 381 session->GetCurrentFrameId(), kGetImageElementForArea, args, &result); | 389 session->GetCurrentFrameId(), kGetImageElementForArea, args, &result); |
| 382 if (status.IsError()) | 390 if (status.IsError()) |
| 383 return status; | 391 return status; |
| 384 const base::DictionaryValue* element_dict; | 392 const base::DictionaryValue* element_dict; |
| 385 if (!result->GetAsDictionary(&element_dict) || | 393 if (!result->GetAsDictionary(&element_dict) || |
| 386 !element_dict->GetString(kElementKey, &target_element_id)) | 394 !element_dict->GetString(GetElementKey(), &target_element_id)) |
| 387 return Status(kUnknownError, "no element reference returned by script"); | 395 return Status(kUnknownError, "no element reference returned by script"); |
| 388 } | 396 } |
| 389 bool is_displayed = false; | 397 bool is_displayed = false; |
| 390 status = IsElementDisplayed( | 398 status = IsElementDisplayed( |
| 391 session, web_view, target_element_id, true, &is_displayed); | 399 session, web_view, target_element_id, true, &is_displayed); |
| 392 if (status.IsError()) | 400 if (status.IsError()) |
| 393 return status; | 401 return status; |
| 394 if (!is_displayed) | 402 if (!is_displayed) |
| 395 return Status(kElementNotVisible); | 403 return Status(kElementNotVisible); |
| 396 | 404 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 rit->chromedriver_frame_id.c_str())); | 632 rit->chromedriver_frame_id.c_str())); |
| 625 std::unique_ptr<base::Value> result; | 633 std::unique_ptr<base::Value> result; |
| 626 status = web_view->CallFunction( | 634 status = web_view->CallFunction( |
| 627 rit->parent_frame_id, kFindSubFrameScript, args, &result); | 635 rit->parent_frame_id, kFindSubFrameScript, args, &result); |
| 628 if (status.IsError()) | 636 if (status.IsError()) |
| 629 return status; | 637 return status; |
| 630 const base::DictionaryValue* element_dict; | 638 const base::DictionaryValue* element_dict; |
| 631 if (!result->GetAsDictionary(&element_dict)) | 639 if (!result->GetAsDictionary(&element_dict)) |
| 632 return Status(kUnknownError, "no element reference returned by script"); | 640 return Status(kUnknownError, "no element reference returned by script"); |
| 633 std::string frame_element_id; | 641 std::string frame_element_id; |
| 634 if (!element_dict->GetString(kElementKey, &frame_element_id)) | 642 if (!element_dict->GetString(GetElementKey(), &frame_element_id)) |
| 635 return Status(kUnknownError, "failed to locate a sub frame"); | 643 return Status(kUnknownError, "failed to locate a sub frame"); |
| 636 | 644 |
| 637 // Modify |region_offset| by the frame's border. | 645 // Modify |region_offset| by the frame's border. |
| 638 int border_left = -1; | 646 int border_left = -1; |
| 639 int border_top = -1; | 647 int border_top = -1; |
| 640 status = GetElementBorder( | 648 status = GetElementBorder( |
| 641 rit->parent_frame_id, web_view, frame_element_id, | 649 rit->parent_frame_id, web_view, frame_element_id, |
| 642 &border_left, &border_top); | 650 &border_left, &border_top); |
| 643 if (status.IsError()) | 651 if (status.IsError()) |
| 644 return status; | 652 return status; |
| 645 region_offset.Offset(border_left, border_top); | 653 region_offset.Offset(border_left, border_top); |
| 646 | 654 |
| 647 status = ScrollElementRegionIntoViewHelper( | 655 status = ScrollElementRegionIntoViewHelper( |
| 648 rit->parent_frame_id, web_view, frame_element_id, | 656 rit->parent_frame_id, web_view, frame_element_id, |
| 649 WebRect(region_offset, region_size), | 657 WebRect(region_offset, region_size), |
| 650 center, frame_element_id, ®ion_offset); | 658 center, frame_element_id, ®ion_offset); |
| 651 if (status.IsError()) | 659 if (status.IsError()) |
| 652 return status; | 660 return status; |
| 653 } | 661 } |
| 654 *location = region_offset; | 662 *location = region_offset; |
| 655 return Status(kOk); | 663 return Status(kOk); |
| 656 } | 664 } |
| OLD | NEW |