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 bool only_one, | 24 bool only_one, |
25 bool has_root_element, | |
25 Session* session, | 26 Session* session, |
26 const base::DictionaryValue& params, | 27 const base::DictionaryValue& params, |
27 scoped_ptr<base::Value>* value) { | 28 scoped_ptr<base::Value>* value) { |
28 std::string strategy; | 29 std::string strategy; |
29 if (!params.GetString("using", &strategy)) | 30 if (!params.GetString("using", &strategy)) |
30 return Status(kUnknownError, "'using' must be a string"); | 31 return Status(kUnknownError, "'using' must be a string"); |
31 std::string target; | 32 std::string target; |
32 if (!params.GetString("value", &target)) | 33 if (!params.GetString("value", &target)) |
33 return Status(kUnknownError, "'value' must be a string"); | 34 return Status(kUnknownError, "'value' must be a string"); |
34 | 35 std::string root_element; |
35 if (strategy == "class name") | 36 if (has_root_element && !params.GetString("id", &root_element)) |
chrisgao (Use stgao instead)
2013/01/15 20:53:59
In the webdriver Json protocol(http://code.google.
kkania
2013/01/15 22:24:36
Does this actually work with the Java bindings?
| |
36 strategy = "className"; | 37 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 | 38 |
46 std::string script; | 39 std::string script; |
47 if (only_one) | 40 if (only_one) |
48 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT); | 41 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT); |
49 else | 42 else |
50 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS); | 43 script = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS); |
51 scoped_ptr<base::DictionaryValue> locator(new base::DictionaryValue()); | 44 scoped_ptr<base::DictionaryValue> locator(new base::DictionaryValue()); |
52 locator->SetString(strategy, target); | 45 locator->SetString(strategy, target); |
53 base::ListValue arguments; | 46 base::ListValue arguments; |
54 arguments.Append(locator.release()); | 47 arguments.Append(locator.release()); |
48 if (has_root_element) { | |
49 scoped_ptr<base::DictionaryValue> id(new base::DictionaryValue()); | |
50 id->SetString("ELEMENT", root_element); | |
51 arguments.Append(id.release()); | |
52 } | |
55 | 53 |
56 base::Time start_time = base::Time::Now(); | 54 base::Time start_time = base::Time::Now(); |
57 while (true) { | 55 while (true) { |
58 scoped_ptr<base::Value> temp; | 56 scoped_ptr<base::Value> temp; |
59 Status status = session->chrome->CallFunction( | 57 Status status = session->chrome->CallFunction( |
60 session->frame, script, arguments, &temp); | 58 session->frame, script, arguments, &temp); |
61 if (status.IsError()) | 59 if (status.IsError()) |
62 return status; | 60 return status; |
63 | 61 |
64 if (!temp->IsType(base::Value::TYPE_NULL)) { | 62 if (!temp->IsType(base::Value::TYPE_NULL)) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 "}"; | 238 "}"; |
241 base::ListValue args; | 239 base::ListValue args; |
242 return session->chrome->CallFunction( | 240 return session->chrome->CallFunction( |
243 session->frame, kGetTitleScript, args, value); | 241 session->frame, kGetTitleScript, args, value); |
244 } | 242 } |
245 | 243 |
246 Status ExecuteFindElement( | 244 Status ExecuteFindElement( |
247 Session* session, | 245 Session* session, |
248 const base::DictionaryValue& params, | 246 const base::DictionaryValue& params, |
249 scoped_ptr<base::Value>* value) { | 247 scoped_ptr<base::Value>* value) { |
250 return FindElementByJs(true, session, params, value); | 248 return FindElementByJs(true, false, session, params, value); |
251 } | 249 } |
252 | 250 |
253 Status ExecuteFindElements( | 251 Status ExecuteFindElements( |
254 Session* session, | 252 Session* session, |
255 const base::DictionaryValue& params, | 253 const base::DictionaryValue& params, |
256 scoped_ptr<base::Value>* value) { | 254 scoped_ptr<base::Value>* value) { |
257 return FindElementByJs(false, session, params, value); | 255 return FindElementByJs(false, false, session, params, value); |
256 } | |
257 | |
258 Status ExecuteFindChildElement( | |
259 Session* session, | |
260 const base::DictionaryValue& params, | |
261 scoped_ptr<base::Value>* value) { | |
262 return FindElementByJs(true, true, session, params, value); | |
263 } | |
264 | |
265 Status ExecuteFindChildElements( | |
266 Session* session, | |
267 const base::DictionaryValue& params, | |
268 scoped_ptr<base::Value>* value) { | |
269 return FindElementByJs(false, true, session, params, value); | |
258 } | 270 } |
259 | 271 |
260 Status ExecuteSetTimeout( | 272 Status ExecuteSetTimeout( |
261 Session* session, | 273 Session* session, |
262 const base::DictionaryValue& params, | 274 const base::DictionaryValue& params, |
263 scoped_ptr<base::Value>* value) { | 275 scoped_ptr<base::Value>* value) { |
264 int ms; | 276 int ms; |
265 if (!params.GetInteger("ms", &ms) || ms < 0) | 277 if (!params.GetInteger("ms", &ms) || ms < 0) |
266 return Status(kUnknownError, "'ms' must be a non-negative integer"); | 278 return Status(kUnknownError, "'ms' must be a non-negative integer"); |
267 std::string type; | 279 std::string type; |
268 if (!params.GetString("type", &type)) | 280 if (!params.GetString("type", &type)) |
269 return Status(kUnknownError, "'type' must be a string"); | 281 return Status(kUnknownError, "'type' must be a string"); |
270 if (type == "implicit") | 282 if (type == "implicit") |
271 session->implicit_wait = ms; | 283 session->implicit_wait = ms; |
272 else if (type == "script") | 284 else if (type == "script") |
273 session->script_timeout = ms; | 285 session->script_timeout = ms; |
274 else if (type == "page load") | 286 else if (type == "page load") |
275 session->page_load_timeout = ms; | 287 session->page_load_timeout = ms; |
276 else | 288 else |
277 return Status(kUnknownError, "unknown type of timeout:" + type); | 289 return Status(kUnknownError, "unknown type of timeout:" + type); |
278 return Status(kOk); | 290 return Status(kOk); |
279 } | 291 } |
OLD | NEW |