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/window_commands.h" | 5 #include "chrome/test/chromedriver/window_commands.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/threading/platform_thread.h" | |
13 #include "base/time.h" | |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/test/chromedriver/basic_types.h" | 15 #include "chrome/test/chromedriver/basic_types.h" |
14 #include "chrome/test/chromedriver/chrome.h" | 16 #include "chrome/test/chromedriver/chrome.h" |
15 #include "chrome/test/chromedriver/element_util.h" | 17 #include "chrome/test/chromedriver/element_util.h" |
18 #include "chrome/test/chromedriver/js.h" | |
16 #include "chrome/test/chromedriver/session.h" | 19 #include "chrome/test/chromedriver/session.h" |
17 #include "chrome/test/chromedriver/status.h" | 20 #include "chrome/test/chromedriver/status.h" |
18 #include "chrome/test/chromedriver/ui_events.h" | 21 #include "chrome/test/chromedriver/ui_events.h" |
19 #include "chrome/test/chromedriver/web_view.h" | 22 #include "chrome/test/chromedriver/web_view.h" |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 Status GetMouseButton(const base::DictionaryValue& params, | 26 Status GetMouseButton(const base::DictionaryValue& params, |
24 MouseButton* button) { | 27 MouseButton* button) { |
25 int button_num; | 28 int button_num; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 if (!params.GetString("script", &script)) | 98 if (!params.GetString("script", &script)) |
96 return Status(kUnknownError, "'script' must be a string"); | 99 return Status(kUnknownError, "'script' must be a string"); |
97 const base::ListValue* args; | 100 const base::ListValue* args; |
98 if (!params.GetList("args", &args)) | 101 if (!params.GetList("args", &args)) |
99 return Status(kUnknownError, "'args' must be a list"); | 102 return Status(kUnknownError, "'args' must be a list"); |
100 | 103 |
101 return web_view->CallFunction( | 104 return web_view->CallFunction( |
102 session->frame, "function(){" + script + "}", *args, value); | 105 session->frame, "function(){" + script + "}", *args, value); |
103 } | 106 } |
104 | 107 |
108 Status ExecuteExecuteAsyncScript( | |
109 Session* session, | |
110 WebView* web_view, | |
111 const base::DictionaryValue& params, | |
112 scoped_ptr<base::Value>* value) { | |
113 std::string script; | |
114 if (!params.GetString("script", &script)) | |
115 return Status(kUnknownError, "'script' must be a string"); | |
116 const base::ListValue* script_args; | |
117 if (!params.GetList("args", &script_args)) | |
118 return Status(kUnknownError, "'args' must be a list"); | |
119 | |
120 base::ListValue args; | |
121 args.AppendString(script); | |
122 args.Append(script_args->DeepCopy()); | |
123 scoped_ptr<base::Value> tmp; | |
124 Status status = web_view->CallFunction( | |
125 session->frame, kExecuteAsyncScriptScript, args, &tmp); | |
126 if (status.IsError()) | |
127 return status; | |
128 | |
129 base::Time start_time = base::Time::Now(); | |
130 base::ListValue args_tmp; | |
131 while (true) { | |
132 scoped_ptr<base::Value> result; | |
133 status = web_view->CallFunction( | |
134 session->frame, | |
135 "function() {" | |
136 " return {" | |
137 " 'finished': document['chromedriverAsyncScriptFinsished']," | |
138 " 'result': document['chromedriverAsyncScriptResult']" | |
139 " };" | |
140 "}", | |
141 args_tmp, | |
142 &result); | |
143 if (status.IsError()) { | |
144 if (status.code() == kNoSuchFrame) | |
145 return Status(kJavaScriptError, "page or frame unload", status); | |
kkania
2013/03/08 17:25:12
this should be unknown error, JavaScriptError is o
chrisgao (Use stgao instead)
2013/03/08 21:06:24
In the protocol(https://code.google.com/p/selenium
| |
146 return status; | |
147 } | |
148 const base::DictionaryValue* dict; | |
149 if (!result->GetAsDictionary(&dict)) | |
150 return Status(kUnknownError, "failed to tell if script has finished"); | |
151 bool finished = false; | |
152 if (!dict->GetBoolean("finished", &finished)) | |
153 return Status(kUnknownError, "flag 'finished' is not returned"); | |
154 if (finished) { | |
155 const base::Value* script_result; | |
156 if (!dict->Get("result", &script_result)) | |
kkania
2013/03/08 17:25:12
i think this will fail if the user calls the callb
chrisgao (Use stgao instead)
2013/03/08 21:06:24
In this case, the result is set as undefined in th
| |
157 return Status(kUnknownError, "failed to get result of script"); | |
158 value->reset(script_result->DeepCopy()); | |
159 return Status(kOk); | |
160 } | |
161 | |
162 if ((base::Time::Now() - start_time).InMilliseconds() >= | |
163 session->script_timeout) | |
164 break; | |
165 | |
166 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | |
167 } | |
168 return Status(kTimeout); | |
169 } | |
170 | |
105 Status ExecuteSwitchToFrame( | 171 Status ExecuteSwitchToFrame( |
106 Session* session, | 172 Session* session, |
107 WebView* web_view, | 173 WebView* web_view, |
108 const base::DictionaryValue& params, | 174 const base::DictionaryValue& params, |
109 scoped_ptr<base::Value>* value) { | 175 scoped_ptr<base::Value>* value) { |
110 const base::Value* id; | 176 const base::Value* id; |
111 if (!params.Get("id", &id)) | 177 if (!params.Get("id", &id)) |
112 return Status(kUnknownError, "missing 'id'"); | 178 return Status(kUnknownError, "missing 'id'"); |
113 | 179 |
114 if (id->IsType(base::Value::TYPE_NULL)) { | 180 if (id->IsType(base::Value::TYPE_NULL)) { |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 WebView* web_view, | 552 WebView* web_view, |
487 const base::DictionaryValue& params, | 553 const base::DictionaryValue& params, |
488 scoped_ptr<base::Value>* value) { | 554 scoped_ptr<base::Value>* value) { |
489 std::string screenshot; | 555 std::string screenshot; |
490 Status status = web_view->CaptureScreenshot(&screenshot); | 556 Status status = web_view->CaptureScreenshot(&screenshot); |
491 if (status.IsError()) | 557 if (status.IsError()) |
492 return status; | 558 return status; |
493 value->reset(new base::StringValue(screenshot)); | 559 value->reset(new base::StringValue(screenshot)); |
494 return Status(kOk); | 560 return Status(kOk); |
495 } | 561 } |
OLD | NEW |