Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6897)

Unified Diff: chrome/test/chromedriver/js/execute_async_script.js

Issue 12675002: [chromedriver] Implement command: executeAsyncScript and setScriptTimeout (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/chromedriver/js/execute_async_script.js
diff --git a/chrome/test/chromedriver/js/execute_async_script.js b/chrome/test/chromedriver/js/execute_async_script.js
new file mode 100644
index 0000000000000000000000000000000000000000..59d53c0f5c2457ea18cc028e88aedd9e770d7193
--- /dev/null
+++ b/chrome/test/chromedriver/js/execute_async_script.js
@@ -0,0 +1,41 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+* Execute the given script and save its result.
kkania 2013/03/08 17:25:12 saves its asynchronous result
chrisgao (Use stgao instead) 2013/03/08 21:06:24 Done.
+*
+* A callback function {function(*) : null} will be appended to the end of
+* <code>args</code>.
+* The script should call this callback when it is completed. The value passed to
+* this callback will be saved in window.chromedriverAsyncScriptResult. Variable
kkania 2013/03/08 17:25:12 try not to include unnecessary implementation deta
chrisgao (Use stgao instead) 2013/03/08 21:06:24 Done.
+* window.chromedriverAsyncScriptFinsished indicates whether the script
+* is completed.
+*
+* If script1 is executed before script2, but script1 completes after script2,
+* then script1's result will be discarded while script2's will be saved.
+*
+* @param {!string} script The asynchronous script to be executed.
+* @param {Array.<*>} args Arguments to be passed to the script.
+*/
+function executeAsyncScript(script, args) {
+ var idName = 'chromedriverAsyncScriptId';
kkania 2013/03/08 17:25:12 how about: function getAsyncScriptInfo() { var
chrisgao (Use stgao instead) 2013/03/08 21:06:24 Done.
+ var finishFlagName = 'chromedriverAsyncScriptFinsished';
kkania 2013/03/08 17:25:12 i think technically you don't need the finish flag
chrisgao (Use stgao instead) 2013/03/08 21:06:24 ok.
+ var resultName = 'chromedriverAsyncScriptResult';
+ if (!(idName in document))
+ document[idName] = 0;
+ document[idName] ++;
kkania 2013/03/08 17:25:12 style says no space i thnik
chrisgao (Use stgao instead) 2013/03/08 21:06:24 Done.
+ document[resultName] = null;
kkania 2013/03/08 17:25:12 what's the point of this?
chrisgao (Use stgao instead) 2013/03/08 21:06:24 If last script timeout, its result might still be
+ document[finishFlagName] = false;
+ var id = document[idName];
+
+ function callback(result) {
+ if (id == document[idName]) {
kkania 2013/03/08 17:25:12 i can think of two improvements here: 1) if the us
chrisgao (Use stgao instead) 2013/03/08 21:06:24 1) Raise Timeout error below if this case happens.
+ document[resultName] = result;
+ document[finishFlagName] = true;
+ }
+ }
+ args.push(callback);
+
+ new window['Function'](script).apply(null, args);
kkania 2013/03/08 17:25:12 I don't think you need the window. new Function(
chrisgao (Use stgao instead) 2013/03/08 21:06:24 Done. Also set error code here.
+}

Powered by Google App Engine
This is Rietveld 408576698