How to terminate session in Selenium Grid(Extras)

slovvic picture slovvic · Aug 9, 2017 · Viewed 14k times · Source

How to terminate session in Selenium Grid? My problem is that if my test fail hub still keep session for this test and I can't run another test (it run but fail because can't get free node, because it is registered in hub). I have found this How to kill thread in a Selenium Grid node and there is an answer to use DELETE /session/:sessionId but it didn't work for me. Documentation on Selenium Grid or Extras is very pure, maybe some one have similar issue and know how to resole it?

Answer

Krishnan Mahadevan picture Krishnan Mahadevan · Aug 9, 2017

The hub never denies a new session request because a slot is not available. The new session ends up in the Grid's queue.

You would need to take a look at your code to check why is driver.quit() not being called all the time (irrespective of why a test fails).

Sometimes your client may crash (For e.g., the JVM where your test cases run may crash, or you may hit the stop button in your IDE to kill your tests. In those circumstances, you end up creating orphaned sessions.

But the Grid has mechanisms for clearing them up as well via properties that you can specify to the Grid when bringing it up.

The selenium Grid specifically has three parameters that are meant for these sort of cleanups.

  • -browserTimeout in seconds : number of seconds a browser session is allowed to hang while a WebDriver command is running (example: driver.get(url)). If the timeout is reached while a WebDriver command is still processing, the session will quit. Minimum value is 60. An unspecified, zero, or negative value means wait indefinitely. Default: 0

  • -cleanUpCycle in milliseconds : specifies how often the hub will poll running proxies for timed-out (i.e. hung) threads. Must also specify timeout option.Default: 5000 (5 seconds)

  • -timeout, -sessionTimeout 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. Default: 1800

Using a combination of all the above 3 parameters, you can configure your node to automatically close orphaned browser instances and sessions.

This documentation is available within the selenium uber jar itself as command line documentation. You can refer to this SO answer to learn how to get it and see what other options are available.

There's some additional documentation related to timeouts on the Grid'2 wiki page here.

For more information refer to my comments in the stackoverflow post : Selenium driver instance persists if test is aborted on Jenkins