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" |
16 #include "chrome/test/chromedriver/js.h" | 18 #include "chrome/test/chromedriver/js.h" |
17 #include "chrome/test/chromedriver/session.h" | 19 #include "chrome/test/chromedriver/session.h" |
18 #include "chrome/test/chromedriver/status.h" | 20 #include "chrome/test/chromedriver/status.h" |
19 #include "chrome/test/chromedriver/ui_events.h" | 21 #include "chrome/test/chromedriver/ui_events.h" |
20 #include "chrome/test/chromedriver/web_view.h" | 22 #include "chrome/test/chromedriver/web_view.h" |
21 | 23 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 if (!params.GetString("script", &script)) | 179 if (!params.GetString("script", &script)) |
178 return Status(kUnknownError, "'script' must be a string"); | 180 return Status(kUnknownError, "'script' must be a string"); |
179 const base::ListValue* args; | 181 const base::ListValue* args; |
180 if (!params.GetList("args", &args)) | 182 if (!params.GetList("args", &args)) |
181 return Status(kUnknownError, "'args' must be a list"); | 183 return Status(kUnknownError, "'args' must be a list"); |
182 | 184 |
183 return web_view->CallFunction( | 185 return web_view->CallFunction( |
184 session->frame, "function(){" + script + "}", *args, value); | 186 session->frame, "function(){" + script + "}", *args, value); |
185 } | 187 } |
186 | 188 |
189 Status ExecuteExecuteAsyncScript( | |
190 Session* session, | |
191 WebView* web_view, | |
192 const base::DictionaryValue& params, | |
193 scoped_ptr<base::Value>* value) { | |
194 std::string script; | |
195 if (!params.GetString("script", &script)) | |
196 return Status(kUnknownError, "'script' must be a string"); | |
197 const base::ListValue* script_args; | |
198 if (!params.GetList("args", &script_args)) | |
199 return Status(kUnknownError, "'args' must be a list"); | |
200 | |
201 base::ListValue args; | |
202 args.AppendString(script); | |
203 args.Append(script_args->DeepCopy()); | |
204 args.AppendInteger(session->script_timeout); | |
205 scoped_ptr<base::Value> tmp; | |
206 Status status = web_view->CallFunction( | |
207 session->frame, kExecuteAsyncScriptScript, args, &tmp); | |
208 if (status.IsError()) | |
209 return status; | |
210 | |
211 base::Time start_time = base::Time::Now(); | |
212 base::ListValue args_tmp; | |
213 while (true) { | |
214 scoped_ptr<base::Value> result; | |
215 status = web_view->CallFunction( | |
216 session->frame, | |
217 "function() {" | |
218 " var info = document['chromedriverAsyncScriptInfo'];" | |
219 " var toReturn = { 'finished': info['finished'] };" | |
220 " if (info['finished']) {" | |
221 " toReturn['result'] = info['result'];" | |
chrisgao (Use stgao instead)
2013/03/08 23:28:25
Javascript has an delayed function call to clear t
| |
222 " delete info['result'];" | |
223 " }" | |
224 " return toReturn;" | |
225 "}", | |
226 args_tmp, | |
227 &result); | |
228 if (status.IsError()) { | |
229 if (status.code() == kNoSuchFrame) | |
230 return Status(kJavaScriptError, "page or frame unload", status); | |
231 return status; | |
232 } | |
233 const base::DictionaryValue* dict; | |
234 if (!result->GetAsDictionary(&dict)) | |
235 return Status(kUnknownError, "failed to tell if script has finished"); | |
236 bool finished = false; | |
237 if (!dict->GetBoolean("finished", &finished)) | |
238 return Status(kUnknownError, "flag 'finished' is not returned"); | |
239 if (finished) { | |
240 if (!dict->HasKey("result")) { | |
241 value->reset(base::Value::CreateNullValue()); | |
242 } else { | |
243 const base::Value* script_result; | |
244 dict->Get("result", &script_result); | |
245 value->reset(script_result->DeepCopy()); | |
246 } | |
247 return Status(kOk); | |
248 } | |
249 | |
250 if ((base::Time::Now() - start_time).InMilliseconds() >= | |
251 session->script_timeout) | |
252 break; | |
253 | |
254 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | |
255 } | |
256 return Status( | |
257 kScriptTimeout, | |
258 base::StringPrintf("timed out waiting for result after %d ms", | |
259 session->script_timeout)); | |
260 } | |
261 | |
187 Status ExecuteSwitchToFrame( | 262 Status ExecuteSwitchToFrame( |
188 Session* session, | 263 Session* session, |
189 WebView* web_view, | 264 WebView* web_view, |
190 const base::DictionaryValue& params, | 265 const base::DictionaryValue& params, |
191 scoped_ptr<base::Value>* value) { | 266 scoped_ptr<base::Value>* value) { |
192 const base::Value* id; | 267 const base::Value* id; |
193 if (!params.Get("id", &id)) | 268 if (!params.Get("id", &id)) |
194 return Status(kUnknownError, "missing 'id'"); | 269 return Status(kUnknownError, "missing 'id'"); |
195 | 270 |
196 if (id->IsType(base::Value::TYPE_NULL)) { | 271 if (id->IsType(base::Value::TYPE_NULL)) { |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
649 for (std::list<Cookie>::const_iterator it = cookies.begin(); | 724 for (std::list<Cookie>::const_iterator it = cookies.begin(); |
650 it != cookies.end(); ++it) { | 725 it != cookies.end(); ++it) { |
651 status = web_view->DeleteCookie(it->name, url); | 726 status = web_view->DeleteCookie(it->name, url); |
652 if (status.IsError()) | 727 if (status.IsError()) |
653 return status; | 728 return status; |
654 } | 729 } |
655 } | 730 } |
656 | 731 |
657 return Status(kOk); | 732 return Status(kOk); |
658 } | 733 } |
OLD | NEW |