OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/test/webdriver/commands/find_element_commands.h" | |
6 | |
7 #include <sstream> | |
8 #include <string> | |
9 | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "third_party/webdriver/atoms.h" | |
14 #include "chrome/test/webdriver/error_codes.h" | |
15 #include "chrome/test/webdriver/utility_functions.h" | |
16 | |
17 namespace webdriver { | |
18 | |
19 bool FindElementCommand::Init(Response* const response) { | |
20 if (!WebDriverCommand::Init(response)) { | |
21 SET_WEBDRIVER_ERROR(response, "Failure on Init for find element", | |
22 kInternalServerError); | |
23 return false; | |
24 } | |
25 | |
26 if (!GetStringASCIIParameter("using", &use_) || | |
27 !GetStringASCIIParameter("value", &value_)) { | |
28 SET_WEBDRIVER_ERROR(response, | |
29 "Request is missing required 'using' and/or 'value' data", kBadRequest); | |
30 return false; | |
31 } | |
32 | |
33 // TODO(jmikhail): The findElement(s) atom should handle this conversion. | |
34 if ("class name" == use_) { | |
35 use_ = "className"; | |
36 } else if ("link text" == use_) { | |
37 use_ = "linkText"; | |
38 } else if ("partial link text" == use_) { | |
39 use_ = "partialLinkText"; | |
40 } else if ("tag name" == use_) { | |
41 use_ = "tagName"; | |
42 } | |
43 | |
44 // Searching under a custom root if the URL pattern is | |
45 // "/session/$session/element/$id/element(s)" | |
46 root_element_id_ = GetPathVariable(4); | |
John Grabowski
2010/11/11 21:43:06
what happens if we don't have >=4 path variables?
Joe
2010/11/30 12:07:02
The url would not match and we would not instanti
| |
47 | |
48 return true; | |
49 } | |
50 | |
51 void FindElementCommand::ExecutePost(Response* const response) { | |
52 scoped_ptr<ListValue> args(new ListValue()); | |
53 DictionaryValue* locator = new DictionaryValue(); | |
54 ErrorCode error; | |
55 std::wstring jscript; | |
56 Value* result = NULL; | |
57 bool done = false; | |
58 | |
59 // Set the command we are using to locate the value beging searched for. | |
60 locator->SetString(use_, value_); | |
61 args->Append(locator); | |
62 args->Append(root_element_id_.size() == 0 ? Value::CreateNullValue() : | |
63 WebDriverCommand::GetElementIdAsDictionaryValue(root_element_id_)); | |
64 if (find_one_element_) { | |
65 jscript = build_atom(FIND_ELEMENT, sizeof FIND_ELEMENT); | |
66 jscript.append(L"var result = findElement(arguments[0], arguments[1]);") | |
67 .append(L"if (!result) {") | |
68 .append(L"var e = Error('Unable to locate element');") | |
69 .append(L"e.code = ") | |
70 .append(UTF8ToWide(base::IntToString(kNoSuchElement))) | |
71 .append(L";throw e;") | |
72 .append(L"} else { return result; }"); | |
73 } else { | |
74 jscript = build_atom(FIND_ELEMENTS, sizeof FIND_ELEMENT); | |
75 jscript.append(L"return findElements(arguments[0], arguments[1]);"); | |
76 } | |
77 | |
78 // The element search needs to loop until at least one element is found or the | |
79 // session's implicit wait timeout expires, whichever occurs first. | |
80 base::Time start_time = base::Time::Now(); | |
81 | |
82 while (!done) { | |
83 if (result) { | |
84 delete result; | |
85 result = NULL; | |
86 } | |
87 | |
88 error = session_->ExecuteScript(jscript, args.get(), &result); | |
89 if (error == kSuccess) { | |
90 // If searching for many elements, make sure we found at least one before | |
91 // stopping. | |
92 done = find_one_element_ || | |
93 (result->GetType() == Value::TYPE_LIST && | |
94 static_cast<ListValue*>(result)->GetSize() > 0); | |
95 } else if (error != kNoSuchElement) { | |
96 SET_WEBDRIVER_ERROR(response, "Internal error in find_element atom", | |
97 kInternalServerError); | |
98 return; | |
99 } | |
100 | |
101 int64 ellapsed_time = (base::Time::Now() - start_time).InMilliseconds(); | |
Paweł Hajdan Jr.
2010/11/10 12:18:45
nit: elapsed
Joe
2010/11/30 12:07:02
Done.
| |
102 done = done || ellapsed_time > session_->implicit_wait(); | |
103 PlatformThread::Sleep(250); // Prevent a busy loop that eats the cpu. | |
John Grabowski
2010/11/11 21:43:06
I'd drop this down to 50ms, which would be good en
Joe
2010/11/30 12:07:02
Done.
| |
104 } | |
105 | |
106 response->set_value(result); | |
107 response->set_status(error); | |
108 } | |
109 | |
110 } // namespace webdriver | |
111 | |
OLD | NEW |