Chromium Code Reviews| 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..5e3542c5d768ef9844af2fef6614893a9a84c4e5 |
| --- /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']++; |
|
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.
|
| + info['finished'] = false; |
| + delete info['result']; |
| + var id = info['id']; |
| + |
| + function callback(result) { |
| + if (id == info['id']) { |
| + 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.
|
| + info['finished'] = true; |
| + } |
| + } |
| + args.push(callback); |
| + |
| + try { |
| + new Function(script).apply(null, args); |
| + } catch (error) { |
| + error.code = 17; // Error code for JavaScriptError. |
| + throw error; |
| + } |
| + |
| + // If timeout is 0 and callback is not called synchronously, raise a |
| + // ScriptTimeout error. |
| + if (timeout == 0 && !info['finished']) { |
|
chrisgao (Use stgao instead)
2013/03/08 23:28:25
Removed, because this failed one java test.
execu
|
| + info['id']++; // Avoid the asynchronous script to set the result. |
| + var error = new Error(); |
| + error.code = 28; // Error code for ScriptTimeout. |
| + throw error; |
| + } |
| +} |