| OLD | NEW |
| 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/session.h" | 5 #include "chrome/test/webdriver/session.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 "xpath += sub('[@name=\"$\" or @id=\"$\"]');" | 517 "xpath += sub('[@name=\"$\" or @id=\"$\"]');" |
| 518 "var frame = document.evaluate(xpath, document, null, " | 518 "var frame = document.evaluate(xpath, document, null, " |
| 519 " XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" | 519 " XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" |
| 520 "if (!frame) { return null; }" | 520 "if (!frame) { return null; }" |
| 521 "xpath = frame.tagName == 'IFRAME' ? '/html/body//iframe'" | 521 "xpath = frame.tagName == 'IFRAME' ? '/html/body//iframe'" |
| 522 " : '/html/frameset/frame';" | 522 " : '/html/frameset/frame';" |
| 523 "frame_xpath = xpath + " | 523 "frame_xpath = xpath + " |
| 524 " sub('[@' + (frame.id == arg ? 'id' : 'name') + '=\"$\"]');" | 524 " sub('[@' + (frame.id == arg ? 'id' : 'name') + '=\"$\"]');" |
| 525 "return [frame, frame_xpath];"; | 525 "return [frame, frame_xpath];"; |
| 526 ListValue args; | 526 ListValue args; |
| 527 args.Append(new StringValue(name_or_id)); | 527 args.Append(base::StringValue::New(name_or_id)); |
| 528 return SwitchToFrameWithJavaScriptLocatedFrame(script, &args); | 528 return SwitchToFrameWithJavaScriptLocatedFrame(script, &args); |
| 529 } | 529 } |
| 530 | 530 |
| 531 Error* Session::SwitchToFrameWithIndex(int index) { | 531 Error* Session::SwitchToFrameWithIndex(int index) { |
| 532 // We cannot simply index into window.frames because we need to know the | 532 // We cannot simply index into window.frames because we need to know the |
| 533 // tagName of the frameElement. If child frame N is from another domain, then | 533 // tagName of the frameElement. If child frame N is from another domain, then |
| 534 // the following will run afoul of the same origin policy: | 534 // the following will run afoul of the same origin policy: |
| 535 // window.frames[N].frameElement; | 535 // window.frames[N].frameElement; |
| 536 // Instead of indexing window.frames, we use a an XPath expression to index | 536 // Instead of indexing window.frames, we use a an XPath expression to index |
| 537 // into the list of all IFRAME and FRAME elements on the page - if we find | 537 // into the list of all IFRAME and FRAME elements on the page - if we find |
| 538 // something, then that XPath expression can be used as the new frame's XPath. | 538 // something, then that XPath expression can be used as the new frame's XPath. |
| 539 std::string script = | 539 std::string script = |
| 540 "var index = '[' + (arguments[0] + 1) + ']';" | 540 "var index = '[' + (arguments[0] + 1) + ']';" |
| 541 "var xpath = '(/html/body//iframe|/html/frameset/frame)' + " | 541 "var xpath = '(/html/body//iframe|/html/frameset/frame)' + " |
| 542 " index;" | 542 " index;" |
| 543 "console.info('searching for frame by xpath: ' + xpath);" | 543 "console.info('searching for frame by xpath: ' + xpath);" |
| 544 "var frame = document.evaluate(xpath, document, null, " | 544 "var frame = document.evaluate(xpath, document, null, " |
| 545 "XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" | 545 "XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" |
| 546 "console.info(frame == null ? 'found nothing' : frame);" | 546 "console.info(frame == null ? 'found nothing' : frame);" |
| 547 "if (!frame) { return null; }" | 547 "if (!frame) { return null; }" |
| 548 "frame_xpath = ((frame.tagName == 'IFRAME' ? " | 548 "frame_xpath = ((frame.tagName == 'IFRAME' ? " |
| 549 " '(/html/body//iframe)' : '/html/frameset/frame') + index);" | 549 " '(/html/body//iframe)' : '/html/frameset/frame') + index);" |
| 550 "return [frame, frame_xpath];"; | 550 "return [frame, frame_xpath];"; |
| 551 ListValue args; | 551 ListValue args; |
| 552 args.Append(Value::CreateIntegerValue(index)); | 552 args.Append(base::NumberValue::New(index)); |
| 553 return SwitchToFrameWithJavaScriptLocatedFrame(script, &args); | 553 return SwitchToFrameWithJavaScriptLocatedFrame(script, &args); |
| 554 } | 554 } |
| 555 | 555 |
| 556 Error* Session::SwitchToFrameWithElement(const WebElementId& element) { | 556 Error* Session::SwitchToFrameWithElement(const WebElementId& element) { |
| 557 // TODO(jleyba): Extract this, and the other frame switch methods to an atom. | 557 // TODO(jleyba): Extract this, and the other frame switch methods to an atom. |
| 558 std::string script = | 558 std::string script = |
| 559 "var element = arguments[0];" | 559 "var element = arguments[0];" |
| 560 "console.info('Attempting to switch to ' + element);" | 560 "console.info('Attempting to switch to ' + element);" |
| 561 "if (element.nodeType != 1 || !/^i?frame$/i.test(element.tagName)) {" | 561 "if (element.nodeType != 1 || !/^i?frame$/i.test(element.tagName)) {" |
| 562 " console.info('Element is not a frame: ' + element + " | 562 " console.info('Element is not a frame: ' + element + " |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 | 861 |
| 862 Error* Session::GetElementEffectiveStyle( | 862 Error* Session::GetElementEffectiveStyle( |
| 863 const FrameId& frame_id, | 863 const FrameId& frame_id, |
| 864 const WebElementId& element, | 864 const WebElementId& element, |
| 865 const std::string& prop, | 865 const std::string& prop, |
| 866 std::string* value) { | 866 std::string* value) { |
| 867 std::string script = base::StringPrintf( | 867 std::string script = base::StringPrintf( |
| 868 "return (%s).apply(null, arguments);", atoms::GET_EFFECTIVE_STYLE); | 868 "return (%s).apply(null, arguments);", atoms::GET_EFFECTIVE_STYLE); |
| 869 ListValue args; | 869 ListValue args; |
| 870 args.Append(element.ToValue()); | 870 args.Append(element.ToValue()); |
| 871 args.Append(Value::CreateStringValue(prop)); | 871 args.Append(base::StringValue::New(prop)); |
| 872 Value* unscoped_result = NULL; | 872 Value* unscoped_result = NULL; |
| 873 Error* error = ExecuteScript( | 873 Error* error = ExecuteScript( |
| 874 frame_id, script, &args, &unscoped_result); | 874 frame_id, script, &args, &unscoped_result); |
| 875 scoped_ptr<Value> result(unscoped_result); | 875 scoped_ptr<Value> result(unscoped_result); |
| 876 if (error) { | 876 if (error) { |
| 877 error->AddDetails(base::StringPrintf( | 877 error->AddDetails(base::StringPrintf( |
| 878 "GetEffectiveStyle atom failed for property (%s)", prop.c_str())); | 878 "GetEffectiveStyle atom failed for property (%s)", prop.c_str())); |
| 879 return error; | 879 return error; |
| 880 } | 880 } |
| 881 | 881 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 908 } | 908 } |
| 909 | 909 |
| 910 Error* Session::IsElementDisplayed(const FrameId& frame_id, | 910 Error* Session::IsElementDisplayed(const FrameId& frame_id, |
| 911 const WebElementId& element, | 911 const WebElementId& element, |
| 912 bool ignore_opacity, | 912 bool ignore_opacity, |
| 913 bool* is_displayed) { | 913 bool* is_displayed) { |
| 914 std::string script = base::StringPrintf( | 914 std::string script = base::StringPrintf( |
| 915 "return (%s).apply(null, arguments);", atoms::IS_DISPLAYED); | 915 "return (%s).apply(null, arguments);", atoms::IS_DISPLAYED); |
| 916 ListValue args; | 916 ListValue args; |
| 917 args.Append(element.ToValue()); | 917 args.Append(element.ToValue()); |
| 918 args.Append(Value::CreateBooleanValue(ignore_opacity)); | 918 args.Append(base::BooleanValue::New(ignore_opacity)); |
| 919 | 919 |
| 920 Value* unscoped_result = NULL; | 920 Value* unscoped_result = NULL; |
| 921 Error* error = ExecuteScript(frame_id, script, &args, &unscoped_result); | 921 Error* error = ExecuteScript(frame_id, script, &args, &unscoped_result); |
| 922 scoped_ptr<Value> result(unscoped_result); | 922 scoped_ptr<Value> result(unscoped_result); |
| 923 if (error) | 923 if (error) |
| 924 return error; | 924 return error; |
| 925 if (!result->GetAsBoolean(is_displayed)) | 925 if (!result->GetAsBoolean(is_displayed)) |
| 926 return new Error(kUnknownError, "IsDisplayed atom returned non-boolean: " + | 926 return new Error(kUnknownError, "IsDisplayed atom returned non-boolean: " + |
| 927 JsonStringify(result.get())); | 927 JsonStringify(result.get())); |
| 928 return NULL; | 928 return NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 JsonStringify(result)); | 966 JsonStringify(result)); |
| 967 } | 967 } |
| 968 return NULL; | 968 return NULL; |
| 969 } | 969 } |
| 970 | 970 |
| 971 Error* Session::SetOptionElementSelected(const FrameId& frame_id, | 971 Error* Session::SetOptionElementSelected(const FrameId& frame_id, |
| 972 const WebElementId& element, | 972 const WebElementId& element, |
| 973 bool selected) { | 973 bool selected) { |
| 974 ListValue args; | 974 ListValue args; |
| 975 args.Append(element.ToValue()); | 975 args.Append(element.ToValue()); |
| 976 args.Append(Value::CreateBooleanValue(selected)); | 976 args.Append(base::BooleanValue::New(selected)); |
| 977 | 977 |
| 978 std::string script = base::StringPrintf( | 978 std::string script = base::StringPrintf( |
| 979 "return (%s).apply(null, arguments);", atoms::SET_SELECTED); | 979 "return (%s).apply(null, arguments);", atoms::SET_SELECTED); |
| 980 | 980 |
| 981 Value* unscoped_result = NULL; | 981 Value* unscoped_result = NULL; |
| 982 Error* error = ExecuteScript(frame_id, script, &args, &unscoped_result); | 982 Error* error = ExecuteScript(frame_id, script, &args, &unscoped_result); |
| 983 scoped_ptr<Value> result(unscoped_result); | 983 scoped_ptr<Value> result(unscoped_result); |
| 984 return error; | 984 return error; |
| 985 } | 985 } |
| 986 | 986 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1037 return NULL; | 1037 return NULL; |
| 1038 } | 1038 } |
| 1039 | 1039 |
| 1040 Error* Session::GetAttribute(const WebElementId& element, | 1040 Error* Session::GetAttribute(const WebElementId& element, |
| 1041 const std::string& key, Value** value) { | 1041 const std::string& key, Value** value) { |
| 1042 std::string script = base::StringPrintf( | 1042 std::string script = base::StringPrintf( |
| 1043 "return (%s).apply(null, arguments);", atoms::GET_ATTRIBUTE); | 1043 "return (%s).apply(null, arguments);", atoms::GET_ATTRIBUTE); |
| 1044 | 1044 |
| 1045 ListValue args; | 1045 ListValue args; |
| 1046 args.Append(element.ToValue()); | 1046 args.Append(element.ToValue()); |
| 1047 args.Append(Value::CreateStringValue(key)); | 1047 args.Append(base::StringValue::New(key)); |
| 1048 | 1048 |
| 1049 Error* error = ExecuteScript(script, &args, value); | 1049 Error* error = ExecuteScript(script, &args, value); |
| 1050 if (error) { | 1050 if (error) { |
| 1051 return error; | 1051 return error; |
| 1052 } | 1052 } |
| 1053 | 1053 |
| 1054 return NULL; | 1054 return NULL; |
| 1055 } | 1055 } |
| 1056 | 1056 |
| 1057 Error* Session::WaitForAllTabsToStopLoading() { | 1057 Error* Session::WaitForAllTabsToStopLoading() { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 if (error_msg.empty()) | 1194 if (error_msg.empty()) |
| 1195 error_msg = "Script failed with error code: " + base::IntToString(code); | 1195 error_msg = "Script failed with error code: " + base::IntToString(code); |
| 1196 return new Error(code, error_msg); | 1196 return new Error(code, error_msg); |
| 1197 } | 1197 } |
| 1198 | 1198 |
| 1199 Value* tmp; | 1199 Value* tmp; |
| 1200 if (result_dict->Get("value", &tmp)) { | 1200 if (result_dict->Get("value", &tmp)) { |
| 1201 *script_result= tmp->DeepCopy(); | 1201 *script_result= tmp->DeepCopy(); |
| 1202 } else { | 1202 } else { |
| 1203 // "value" was not defined in the returned dictionary; set to null. | 1203 // "value" was not defined in the returned dictionary; set to null. |
| 1204 *script_result= Value::CreateNullValue(); | 1204 *script_result= base::NullValue(); |
| 1205 } | 1205 } |
| 1206 return NULL; | 1206 return NULL; |
| 1207 } | 1207 } |
| 1208 | 1208 |
| 1209 void Session::SendKeysOnSessionThread(const string16& keys, Error** error) { | 1209 void Session::SendKeysOnSessionThread(const string16& keys, Error** error) { |
| 1210 std::vector<WebKeyEvent> key_events; | 1210 std::vector<WebKeyEvent> key_events; |
| 1211 std::string error_msg; | 1211 std::string error_msg; |
| 1212 if (!ConvertKeysToWebKeyEvents(keys, &key_events, &error_msg)) { | 1212 if (!ConvertKeysToWebKeyEvents(keys, &key_events, &error_msg)) { |
| 1213 *error = new Error(kUnknownError, error_msg); | 1213 *error = new Error(kUnknownError, error_msg); |
| 1214 return; | 1214 return; |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1426 const FrameId& frame_id, | 1426 const FrameId& frame_id, |
| 1427 const WebElementId& element, | 1427 const WebElementId& element, |
| 1428 const gfx::Rect& region, | 1428 const gfx::Rect& region, |
| 1429 bool center, | 1429 bool center, |
| 1430 bool verify_clickable_at_middle, | 1430 bool verify_clickable_at_middle, |
| 1431 gfx::Point* location) { | 1431 gfx::Point* location) { |
| 1432 std::string jscript = base::StringPrintf( | 1432 std::string jscript = base::StringPrintf( |
| 1433 "return (%s).apply(null, arguments);", atoms::GET_LOCATION_IN_VIEW); | 1433 "return (%s).apply(null, arguments);", atoms::GET_LOCATION_IN_VIEW); |
| 1434 ListValue jscript_args; | 1434 ListValue jscript_args; |
| 1435 jscript_args.Append(element.ToValue()); | 1435 jscript_args.Append(element.ToValue()); |
| 1436 jscript_args.Append(Value::CreateBooleanValue(center)); | 1436 jscript_args.Append(base::BooleanValue::New(center)); |
| 1437 DictionaryValue* elem_offset_dict = new DictionaryValue(); | 1437 DictionaryValue* elem_offset_dict = new DictionaryValue(); |
| 1438 elem_offset_dict->SetInteger("left", region.x()); | 1438 elem_offset_dict->SetInteger("left", region.x()); |
| 1439 elem_offset_dict->SetInteger("top", region.y()); | 1439 elem_offset_dict->SetInteger("top", region.y()); |
| 1440 elem_offset_dict->SetInteger("width", region.width()); | 1440 elem_offset_dict->SetInteger("width", region.width()); |
| 1441 elem_offset_dict->SetInteger("height", region.height()); | 1441 elem_offset_dict->SetInteger("height", region.height()); |
| 1442 jscript_args.Append(elem_offset_dict); | 1442 jscript_args.Append(elem_offset_dict); |
| 1443 Value* unscoped_value = NULL; | 1443 Value* unscoped_value = NULL; |
| 1444 Error* error = ExecuteScript(frame_id, jscript, &jscript_args, | 1444 Error* error = ExecuteScript(frame_id, jscript, &jscript_args, |
| 1445 &unscoped_value); | 1445 &unscoped_value); |
| 1446 scoped_ptr<Value> value(unscoped_value); | 1446 scoped_ptr<Value> value(unscoped_value); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1527 if (error) | 1527 if (error) |
| 1528 return error; | 1528 return error; |
| 1529 if (!value->GetAsInteger(status)) | 1529 if (!value->GetAsInteger(status)) |
| 1530 return new Error(kUnknownError, | 1530 return new Error(kUnknownError, |
| 1531 "GET_APPCACHE_STATUS script returned non-integer: " + | 1531 "GET_APPCACHE_STATUS script returned non-integer: " + |
| 1532 JsonStringify(value.get())); | 1532 JsonStringify(value.get())); |
| 1533 return NULL; | 1533 return NULL; |
| 1534 } | 1534 } |
| 1535 | 1535 |
| 1536 } // namespace webdriver | 1536 } // namespace webdriver |
| OLD | NEW |