9

Why am I getting an exception with selenium 2.25.0 when trying to execute asynchronous script.

//navigate to my test page.

String script = "var callback = arguments[arguments.length - 1];"  +
                "getResult(callback)";

Object result = ((JavascriptExecutor)driver).executeAsyncScript(script, "");

System.out.println(result);

The test page contains the following script:

var result = true;
function getResult(callback){
    window.setTimeout(function(){callback(true);}, 3000);
}

This throws an exception:

FAILED: testSeleniumAsync
org.openqa.selenium.TimeoutException: Script execution failed. Script: var callback = arguments[arguments.length - 1];getResult(callback);
 Timed out waiting for asyncrhonous script result after 2 ms (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 11 milliseconds
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 22:18:01'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-27-generic', java.version: '1.6.0_26'
Driver info: driver.version: RemoteWebDriver
Session ID: 6347b507cf22b6c2d3312937a82a0a02
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

If I remove the setTimeout from my script and I call callback it works. But this is not what I want.

Thanks.

1 Answer 1

21

Its very strange that the API is timing out in 2 ms.

I am guessing that the script time out is somehow incorrectly configured (<= 0sec). Since your window timeout occurs after 3 secs, try setting the script time out to some value greater than 3 secs, before you make the call.

Like so:

driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

That might work.

Sign up to request clarification or add additional context in comments.

5 Comments

Let me add something I think to be a common pitfall, ((JavascriptExecutor)driver).executeAsyncScript(script, ""); // will return immediately.
@redsonic +1 any day. But in this case the OP is invoking the callback.
I have the same issue in protractor, how can i configure it ? i did browser.manage().timeouts().setScriptTimeout(60000); but i have the same error !
@AshwinPrabhu, I've the same issue in perl selenium. I tried with adding $driver->set_async_script_timeout(1000); before calling $driver->execute_async_script method but it still give me error. Please help!
Docs for executeAsyncScript() says "The default timeout for a script to be executed is 0ms. In most cases, including the examples below, one must set the script timeout WebDriver.Timeouts.setScriptTimeout(java.time.Duration) beforehand to a value sufficiently large enough."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.