Home > Mobile >  Selenium Java custom browser timeout
Selenium Java custom browser timeout

Time:04-12

In our organization we are automating UI test cases in Selenium Java with Selenium Grid over several browsers (Chrome, Firefox, IE/Edge).

Our Selenium Grid has a timeout configuration to close the browser instance if no command is send in 30 seconds. This is very useful in the case of some unexpected problems appear during the execution.

In few special test cases we need to increase this value to 15 min due to the specification of the test case. However, if no command is send to the browser, it is closed in 30 seconds.

As we saw, the timeout value is set on the configuration of the node of the Selenium Grid. Before trying another workarounds, Is there a way to set a specific timeout value through capabilities/arguments when opening the web driver in Selenium Java? We want to maintain the 30 seconds timeout in general but apply a custom timeout in some special cases.

We already try with implicitlyWait, pageLoadTimeout and scriptTimeout, but nothing changes.

CodePudding user response:

The grid 2 official documentation says that node is released for other tests, if no interactions are detected in set timeout:

-timeout 30 (300 is default) The timeout in seconds before the hub automatically releases a node that hasn’t received any requests for more than the specified number of seconds. After this time, the node will be released for another test in the queue. This helps to clear client crashes without manual intervention. To remove the timeout completely, specify -timeout 0 and the hub will never release the node.

Since you mention that this is a special case for only some of your tests and 'interaction' with browser can be done with polling I suggest you try a FluentWait. An example in Java, would look like this:

 FluentWait<WebDriver> wait = new FluentWait(driver)
            .pollingEvery(Duration.ofSeconds(15))
            .withTimeout(Duration.ofMinutes(15))
            .ignoring(NoSuchElementException.class);
 wait.until(driver -> driver.findElement(locator));

This would wait 15 minutes for some element to be 'found' ignoring NoSuchElementExceptions. Of course you can wait for some other condition, i.e. using the built in ExpectedConditions class. With polling set to something smaller then node -timeout the browser would not be killed.

CodePudding user response:

When using Selenium Grid to set a specific timeout value you can configure the following parameter:

-timeout, -sessionTimeout
  <Integer> in seconds : Specifies the timeout before the server
  automatically kills a session that hasn't had any activity in the last X
  seconds. The test slot will then be released for another test to use.
  This is typically used to take care of client crashes. For grid hub/node
  roles, cleanUpCycle must also be set. If a node does not specify it, the
  hub value will be used.
  

An example:

  • At NODE level:

    java -jar -Dwebdriver.chrome.driver=chromedriver selenium-server-standalone-3.141.59.jar -role node -hub http://127.0.0.1:4444/grid/register -browser browserName=chrome,maxInstances=2 -timeout 5      
    
  • At HUB level:

    java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4444 -timeout 5
    

You can find a relevant detailed discussions in Is there a way too prevent selenium automatically terminating idle sessions?

  • Related