Java – selenium asynchronous script cannot be executed
•
Java
Why do I get selenium 2.25 when I try to execute an asynchronous script Exception for 0
//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 scripts:
var result = true; function getResult(callback){ window.setTimeout(function(){callback(true);},3000); }
This raises 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 setTimeout from my script and I call a callback, it works But that's not what I want
thank you.
Solution
It is very strange that the API timed out within 2 ms
I guess the script timeout is incorrectly configured in some way (< = 0sec) Since your window timeout occurs after 3 seconds, try setting the script timeout to a value greater than 3 seconds before making a call like this:
driver.manage().timeouts().setScriptTimeout(5,TimeUnit.SECONDS);
This may work
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码