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

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: Address comments and add command setScriptTimeout 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..3522dc4863583d3608a561e27de89ea7df00e809
--- /dev/null
+++ b/chrome/test/chromedriver/js/execute_async_script.js
@@ -0,0 +1,57 @@
+// 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.
+
+/**
+* Return the information of asynchronous script execution.
+*
+* @return {Object.<string, ?>} Information of asynchronous script execution.
+*/
+function getAsyncScriptInfo() {
+ var key = 'chromedriverAsyncScriptInfo';
+ if (!(key in document))
+ document[key] = {'id': 0, 'finished': false};
+ return document[key];
+}
+
+/**
+* Execute the given script and save its asynchronous result.
+*
+* If script1 finishes after script2 is executed, 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.
+* @param {!number} timeout The timeout for the asynchronous script.
+*/
+function executeAsyncScript(script, args, timeout) {
+ var info = getAsyncScriptInfo();
+ info.id++;
+ info.finished = false;
+ delete info.result;
+ var id = info.id;
+
+ function callback(result) {
+ if (id == info.id) {
+ info.result = result;
+ info.finished = true;
+ }
+ }
+ args.push(callback);
+
+ try {
+ new Function(script).apply(null, args);
+ } catch (error) {
+ error.code = 17; // Error code for JavaScriptError.
+ throw error;
+ }
+
+ function clearResultIfTimeout() {
+ if (id == info.id) {
+ delete info.result; // Delete reference to result if it is already set.
+ info.id++; // Prevent the script from setting the result if not yet.
+ }
+ }
+ // Allow C++ to pull result in 5 seconds before deletion.
+ setTimeout(clearResultIfTimeout, timeout + 5000);
chrisgao (Use stgao instead) 2013/03/08 23:28:25 I think 5 seconds should be long enough for C++ to
+}
« no previous file with comments | « chrome/test/chromedriver/command_executor_impl.cc ('k') | chrome/test/chromedriver/js/execute_async_script_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698