| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/commands.h" | 5 #include "chrome/test/chromedriver/commands.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/test/chromedriver/chrome.h" | 15 #include "chrome/test/chromedriver/chrome.h" |
| 16 #include "chrome/test/chromedriver/chrome_launcher.h" | 16 #include "chrome/test/chromedriver/chrome_launcher.h" |
| 17 #include "chrome/test/chromedriver/session.h" | 17 #include "chrome/test/chromedriver/session.h" |
| 18 #include "chrome/test/chromedriver/status.h" | 18 #include "chrome/test/chromedriver/status.h" |
| 19 #include "third_party/webdriver/atoms.h" | 19 #include "third_party/webdriver/atoms.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 Status FindElementByJs( | 23 Status FindElementByJs( |
| 24 int interval_ms, |
| 24 bool only_one, | 25 bool only_one, |
| 26 bool has_root_element, |
| 25 Session* session, | 27 Session* session, |
| 26 const base::DictionaryValue& params, | 28 const base::DictionaryValue& params, |
| 27 scoped_ptr<base::Value>* value) { | 29 scoped_ptr<base::Value>* value) { |
| 28 std::string strategy; | 30 std::string strategy; |
| 29 if (!params.GetString("using", &strategy)) | 31 if (!params.GetString("using", &strategy)) |
| 30 return Status(kUnknownError, "'using' must be a string"); | 32 return Status(kUnknownError, "'using' must be a string"); |
| 31 std::string target; | 33 std::string target; |
| 32 if (!params.GetString("value", &target)) | 34 if (!params.GetString("value", &target)) |
| 33 return Status(kUnknownError, "'value' must be a string"); | 35 return Status(kUnknownError, "'value' must be a string"); |
| 34 | 36 std::string root_element; |
| 35 if (strategy == "class name") | 37 if (has_root_element && !params.GetString("id", &root_element)) |
| 36 strategy = "className"; | 38 return Status(kUnknownError, "'id' of root element must be a string"); |
| 37 else if (strategy == "css selector") | |
| 38 strategy = "css"; | |
| 39 else if (strategy == "link text") | |
| 40 strategy = "linkText"; | |
| 41 else if (strategy == "partial link text") | |
| 42 strategy = "partialLinkText"; | |
| 43 else if (strategy == "tag name") | |
| 44 strategy = "tagName"; | |
| 45 | 39 |
| 46 std::string script; | 40 std::string script; |
| 47 if (only_one) | 41 if (only_one) |
| 48 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT); | 42 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT); |
| 49 else | 43 else |
| 50 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS); | 44 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS); |
| 51 scoped_ptr<base::DictionaryValue> locator(new base::DictionaryValue()); | 45 scoped_ptr<base::DictionaryValue> locator(new base::DictionaryValue()); |
| 52 locator->SetString(strategy, target); | 46 locator->SetString(strategy, target); |
| 53 base::ListValue arguments; | 47 base::ListValue arguments; |
| 54 arguments.Append(locator.release()); | 48 arguments.Append(locator.release()); |
| 49 if (has_root_element) { |
| 50 scoped_ptr<base::DictionaryValue> id(new base::DictionaryValue()); |
| 51 id->SetString("ELEMENT", root_element); |
| 52 arguments.Append(id.release()); |
| 53 } |
| 55 | 54 |
| 56 base::Time start_time = base::Time::Now(); | 55 base::Time start_time = base::Time::Now(); |
| 57 while (true) { | 56 while (true) { |
| 58 scoped_ptr<base::Value> temp; | 57 scoped_ptr<base::Value> temp; |
| 59 Status status = session->chrome->CallFunction( | 58 Status status = session->chrome->CallFunction( |
| 60 session->frame, script, arguments, &temp); | 59 session->frame, script, arguments, &temp); |
| 61 if (status.IsError()) | 60 if (status.IsError()) |
| 62 return status; | 61 return status; |
| 63 | 62 |
| 64 if (!temp->IsType(base::Value::TYPE_NULL)) { | 63 if (!temp->IsType(base::Value::TYPE_NULL)) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 79 | 78 |
| 80 if ((base::Time::Now() - start_time).InMilliseconds() >= | 79 if ((base::Time::Now() - start_time).InMilliseconds() >= |
| 81 session->implicit_wait) { | 80 session->implicit_wait) { |
| 82 if (only_one) { | 81 if (only_one) { |
| 83 return Status(kNoSuchElement); | 82 return Status(kNoSuchElement); |
| 84 } else { | 83 } else { |
| 85 value->reset(new base::ListValue()); | 84 value->reset(new base::ListValue()); |
| 86 return Status(kOk); | 85 return Status(kOk); |
| 87 } | 86 } |
| 88 } | 87 } |
| 89 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | 88 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(interval_ms)); |
| 90 } | 89 } |
| 91 | 90 |
| 92 return Status(kUnknownError); | 91 return Status(kUnknownError); |
| 93 } | 92 } |
| 94 | 93 |
| 95 } // namespace | 94 } // namespace |
| 96 | 95 |
| 97 Status ExecuteNewSession( | 96 Status ExecuteNewSession( |
| 98 SessionMap* session_map, | 97 SessionMap* session_map, |
| 99 ChromeLauncher* launcher, | 98 ChromeLauncher* launcher, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 " return document.title;" | 236 " return document.title;" |
| 238 " else" | 237 " else" |
| 239 " return document.URL;" | 238 " return document.URL;" |
| 240 "}"; | 239 "}"; |
| 241 base::ListValue args; | 240 base::ListValue args; |
| 242 return session->chrome->CallFunction( | 241 return session->chrome->CallFunction( |
| 243 session->frame, kGetTitleScript, args, value); | 242 session->frame, kGetTitleScript, args, value); |
| 244 } | 243 } |
| 245 | 244 |
| 246 Status ExecuteFindElement( | 245 Status ExecuteFindElement( |
| 246 int interval_ms, |
| 247 Session* session, | 247 Session* session, |
| 248 const base::DictionaryValue& params, | 248 const base::DictionaryValue& params, |
| 249 scoped_ptr<base::Value>* value) { | 249 scoped_ptr<base::Value>* value) { |
| 250 return FindElementByJs(true, session, params, value); | 250 return FindElementByJs(interval_ms, true, false, session, params, value); |
| 251 } | 251 } |
| 252 | 252 |
| 253 Status ExecuteFindElements( | 253 Status ExecuteFindElements( |
| 254 int interval_ms, |
| 254 Session* session, | 255 Session* session, |
| 255 const base::DictionaryValue& params, | 256 const base::DictionaryValue& params, |
| 256 scoped_ptr<base::Value>* value) { | 257 scoped_ptr<base::Value>* value) { |
| 257 return FindElementByJs(false, session, params, value); | 258 return FindElementByJs(interval_ms, false, false, session, params, value); |
| 259 } |
| 260 |
| 261 Status ExecuteFindChildElement( |
| 262 int interval_ms, |
| 263 Session* session, |
| 264 const base::DictionaryValue& params, |
| 265 scoped_ptr<base::Value>* value) { |
| 266 return FindElementByJs(interval_ms, true, true, session, params, value); |
| 267 } |
| 268 |
| 269 Status ExecuteFindChildElements( |
| 270 int interval_ms, |
| 271 Session* session, |
| 272 const base::DictionaryValue& params, |
| 273 scoped_ptr<base::Value>* value) { |
| 274 return FindElementByJs(interval_ms, false, true, session, params, value); |
| 258 } | 275 } |
| 259 | 276 |
| 260 Status ExecuteSetTimeout( | 277 Status ExecuteSetTimeout( |
| 261 Session* session, | 278 Session* session, |
| 262 const base::DictionaryValue& params, | 279 const base::DictionaryValue& params, |
| 263 scoped_ptr<base::Value>* value) { | 280 scoped_ptr<base::Value>* value) { |
| 264 int ms; | 281 int ms; |
| 265 if (!params.GetInteger("ms", &ms) || ms < 0) | 282 if (!params.GetInteger("ms", &ms) || ms < 0) |
| 266 return Status(kUnknownError, "'ms' must be a non-negative integer"); | 283 return Status(kUnknownError, "'ms' must be a non-negative integer"); |
| 267 std::string type; | 284 std::string type; |
| 268 if (!params.GetString("type", &type)) | 285 if (!params.GetString("type", &type)) |
| 269 return Status(kUnknownError, "'type' must be a string"); | 286 return Status(kUnknownError, "'type' must be a string"); |
| 270 if (type == "implicit") | 287 if (type == "implicit") |
| 271 session->implicit_wait = ms; | 288 session->implicit_wait = ms; |
| 272 else if (type == "script") | 289 else if (type == "script") |
| 273 session->script_timeout = ms; | 290 session->script_timeout = ms; |
| 274 else if (type == "page load") | 291 else if (type == "page load") |
| 275 session->page_load_timeout = ms; | 292 session->page_load_timeout = ms; |
| 276 else | 293 else |
| 277 return Status(kUnknownError, "unknown type of timeout:" + type); | 294 return Status(kUnknownError, "unknown type of timeout:" + type); |
| 278 return Status(kOk); | 295 return Status(kOk); |
| 279 } | 296 } |
| OLD | NEW |