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

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. 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']++;
kkania 2013/03/08 21:56:50 instead of info['id'] style, do info.id; same for
chrisgao (Use stgao instead) 2013/03/08 23:28:25 Done.
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;
kkania 2013/03/08 21:56:50 there's still a small bug where this will hold a r
chrisgao (Use stgao instead) 2013/03/08 23:28:25 Done.
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 // If timeout is 0 and callback is not called synchronously, raise a
50 // ScriptTimeout error.
51 if (timeout == 0 && !info['finished']) {
chrisgao (Use stgao instead) 2013/03/08 23:28:25 Removed, because this failed one java test. execu
52 info['id']++; // Avoid the asynchronous script to set the result.
53 var error = new Error();
54 error.code = 28; // Error code for ScriptTimeout.
55 throw error;
56 }
57 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698