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.
6 * Enum for WebDriver status codes.
17 * Dictionary key for asynchronous script info.
20 var ASYNC_INFO_KEY
= '$chrome_asyncScriptInfo';
23 * Return the information of asynchronous script execution.
25 * @return {Object<string, ?>} Information of asynchronous script execution.
27 function getAsyncScriptInfo() {
28 if (!(ASYNC_INFO_KEY
in document
))
29 document
[ASYNC_INFO_KEY
] = {'id': 0};
30 return document
[ASYNC_INFO_KEY
];
34 * Execute the given script and save its asynchronous result.
36 * If script1 finishes after script2 is executed, then script1's result will be
37 * discarded while script2's will be saved.
39 * @param {string} script The asynchronous script to be executed. The script
40 * should be a proper function body. It will be wrapped in a function and
41 * invoked with the given arguments and, as the final argument, a callback
42 * function to invoke to report the asynchronous result.
43 * @param {!Array<*>} args Arguments to be passed to the script.
44 * @param {boolean} isUserSupplied Whether the script is supplied by the user.
45 * If not, UnknownError will be used instead of JavaScriptError if an
46 * exception occurs during the script, and an additional error callback will
47 * be supplied to the script.
48 * @param {?number} opt_timeoutMillis The timeout, in milliseconds, to use.
49 * If the timeout is exceeded and the callback has not been invoked, a error
50 * result will be saved and future invocation of the callback will be
53 function executeAsyncScript(script
, args
, isUserSupplied
, opt_timeoutMillis
) {
54 var info
= getAsyncScriptInfo();
59 function report(status
, value
) {
63 info
.result
= {status
: status
, value
: value
};
65 function reportValue(value
) {
66 report(StatusCode
.OK
, value
);
68 function reportScriptError(error
) {
69 var code
= isUserSupplied
? StatusCode
.JAVASCRIPT_ERROR
:
70 (error
.code
|| StatusCode
.UNKNOWN_ERROR
);
71 var message
= error
.message
;
73 message
+= "\nJavaScript stack:\n" + error
.stack
;
75 report(code
, message
);
77 args
.push(reportValue
);
79 args
.push(reportScriptError
);
82 new Function(script
).apply(null, args
);
84 reportScriptError(error
);
88 if (typeof(opt_timeoutMillis
) != 'undefined') {
89 window
.setTimeout(function() {
90 var code
= isUserSupplied
? StatusCode
.SCRIPT_TIMEOUT
:
91 StatusCode
.UNKNOWN_ERROR
;
92 var errorMsg
= 'result was not received in ' + opt_timeoutMillis
/ 1000 +
94 report(code
, errorMsg
);
95 }, opt_timeoutMillis
);