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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 /**
6 * Return the information of asynchronous script execution.
7 *
8 * @return {Object.<string, ?>} Information of asynchronous script execution.
9 */
10 function getAsyncScriptInfo() {
11 var key = 'chromedriverAsyncScriptInfo';
12 if (!(key in document))
13 document[key] = {'id': 0, 'finished': false};
14 return document[key];
15 }
16
17 /**
18 * Execute the given script and save its asynchronous result.
19 *
20 * If script1 finishes after script2 is executed, then script1's result will be
21 * discarded while script2's will be saved.
22 *
23 * @param {!string} script The asynchronous script to be executed.
24 * @param {Array.<*>} args Arguments to be passed to the script.
25 * @param {!number} timeout The timeout for the asynchronous script.
26 */
27 function executeAsyncScript(script, args, timeout) {
28 var info = getAsyncScriptInfo();
29 info.id++;
30 info.finished = false;
31 delete info.result;
32 var id = info.id;
33
34 function callback(result) {
35 if (id == info.id) {
36 info.result = result;
37 info.finished = true;
38 }
39 }
40 args.push(callback);
41
42 try {
43 new Function(script).apply(null, args);
44 } catch (error) {
45 error.code = 17; // Error code for JavaScriptError.
46 throw error;
47 }
48
49 function clearResultIfTimeout() {
50 if (id == info.id) {
51 delete info.result; // Delete reference to result if it is already set.
52 info.id++; // Prevent the script from setting the result if not yet.
53 }
54 }
55 // Allow C++ to pull result in 5 seconds before deletion.
56 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
57 }
OLDNEW
« 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