7

when SetScriptTimeout should be used and please provide me any example.

I know the defn

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

but not sure what it does exactly.

J_Coder
  • 707
  • 5
  • 14
  • 32

3 Answers3

11

You've got two answers already, neither of which I find explain clearly the point of setting a script timeout.

First, it is important the script timeout affects only JavaScript code executed with executeAsyncScript and nothing else. In particular, executeScript is not affected by it.

So why do you want to set a timeout for executeAsyncScript? Chandan Nayak correctly explained that the default timeout is 0s so you have to change this timeout if you want to use executeAsyncScript with asynchronous scripts that actually perform some work. But why not just set it to -1 and be done with it? After all, if you set it to -1 then you turn off the timeout. So you won't get any timeouts anymore. Mission accomplished, right? Nope.

What you want to do is set the timeout to a value that allows the code you use with executeAsyncScript to perform it works while at the same time detect when a script has gone rogue. For instance, if from experience you know that a script you pass to executeAsyncScript is going to be done in 2 seconds or less (except perhaps in extremely unusual circumstances), then you set the timeout to 2 seconds so that if there is a bug somewhere and the code never terminates, you get a timeout after 2 seconds. Otherwise, Selenium will happily wait forever for the script to complete.

Community
  • 1
  • 1
Louis
  • 146,715
  • 28
  • 274
  • 320
  • NOTE: In C# the timeout is a TimeSpan struct which cannot be set to -1. In C# you must set it to TimeSpan.MinValue instead. – Elmue Nov 18 '15 at 13:17
5

From WebDriver documentation: setScriptTimeout(long time, java.util.concurrent.TimeUnit unit) Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. This works only for Assync scripts (executeAsyncScript)

Let's run a simple javascript: (Do not set setScriptTimeout ) - Now this shall execute without throwing any issue.

((JavascriptExecutor) driver).executeScript("alert('hello world');");


Lets run a simple Assync Script: ( Do not set setScriptTimeout) - This shall fail with error - "Timed out waiting for async script result after 0ms"

 ((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");

To resolve the issue: setScriptTimeout to 1 Second:

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

And then run the same Assync Script mentioned above and it shall execute without any error.


Reason: 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(long, java.util.concurrent.TimeUnit) beforehand to a value sufficiently large enough

More Reference Links:
When should setScriptTimeout be used?
WebDriver executeAsyncScript vs executeScript
WebDriver Doc

Stephan
  • 41,764
  • 65
  • 238
  • 329
Chandan Nayak
  • 10,117
  • 5
  • 26
  • 36
0

Web application automation is dependent on many factors like browser, network speed, embedded scripting etc. To write robust code for running in all environments we need to inserts wait for WebElements before performing any operation on that. WebDriver wait (synchronization) can be obtained either by using support.ui or driver().manage().timeouts()

If we use driver.manage().timeouts(), a common practice to achieve synchronization is to use JavaScript via JavascriptExecutor which in turn provide two methods for script execution:

executeAsyncScript -> This method doesn't block the execution of next line of code...till execution of this method is completed. This method will execute as well as next line of code will be executed...asynchronously. (without blocking each other)

executeScript -> This method will block the execution till it's execution is completed and then it moves to next line of code. In short your automation code will halt till the Javascript is executed via this method.

Now since executeAsyncScript method doesn't block the execution of next line of code, it might be beneficial to use driver.manage().timeouts().setScriptTimeout(30,SECONDS); so that our code can wait for specified amount of time for an asynchronous script to finish execution before throwing an error.

san1deep2set3hi
  • 4,904
  • 1
  • 23
  • 23