Skip to content

Commit eb0def7

Browse files
committed
fix execute async script
the timeout needs to be after the script is called so that the asynchronous script provided by the user can be called before the timeout script in the case where both timeouts are equal the test doesn't need 100ms, 10 will suffice.
1 parent 0569d89 commit eb0def7

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

java/client/test/org/openqa/selenium/ExecutingAsyncJavascriptTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ public void shouldCatchErrorsWhenExecutingInitialScript() {
241241
@Test
242242
public void shouldNotTimeoutWithMultipleCallsTheFirstOneBeingSynchronous() {
243243
driver.get(pages.ajaxyPage);
244-
driver.manage().timeouts().setScriptTimeout(100, TimeUnit.MILLISECONDS);
244+
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.MILLISECONDS);
245245
assertTrue((Boolean) executor.executeAsyncScript("arguments[arguments.length - 1](true);"));
246246
assertTrue((Boolean) executor.executeAsyncScript(
247-
"var cb = arguments[arguments.length - 1]; window.setTimeout(function(){cb(true);}, 99);"));
247+
"var cb = arguments[arguments.length - 1]; window.setTimeout(function(){cb(true);}, 9);"));
248248
}
249249

250250
@JavascriptEnabled

javascript/firefox-driver/js/evaluate.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ limitations under the License.
3434
if (isAsync) {
3535
window.clearTimeout(timeoutId);
3636
window.removeEventListener('unload', onunload, false);
37+
timeoutId = true;
3738
}
3839

3940
document.__webdriver_evaluate['args'] = null;
@@ -65,16 +66,19 @@ limitations under the License.
6566

6667
var startTime = new Date().getTime();
6768
try {
68-
if (isAsync) {
69-
timeoutId = window.setTimeout(function() {
70-
sendResponse(
71-
Error('Timed out waiting for async script result after ' +
72-
(new Date().getTime() - startTime) + 'ms'),
73-
28); // "script timeout" == 28
74-
}, timeout);
75-
}
7669
var result = new Function(script).apply(null, args);
77-
if (!isAsync) {
70+
if (isAsync) {
71+
// if the callback method is called synchronously in the provided script
72+
// don't set a timeout function, since the timeout has already been 'cleared'
73+
if (!timeoutId) {
74+
timeoutId = window.setTimeout(function() {
75+
sendResponse(
76+
Error('Timed out waiting for async script result after ' +
77+
(new Date().getTime() - startTime) + 'ms'),
78+
28); // "script timeout" == 28
79+
}, timeout);
80+
}
81+
} else {
7882
sendResponse(result, 0);
7983
}
8084
} catch (e) {

0 commit comments

Comments
 (0)