12

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?

2
  • The hub never denies a new session request because a slot is not available. The new session ends up in the Grid's queue. Why does your test not close the session properly ? Have you looked at that part ? Irrespective of whether a test passes or fails (either due to assertion failures or due to exceptions), they should always invoke driver.quit() to delete a session. Commented Aug 9, 2017 at 13:56
  • You are probably right. When I stop test e.g by Intellij, tearDown method which close the driver will not run, but how to achieve to run tearDown method even if I stop test using Intellij? Commented Aug 9, 2017 at 14:31

3 Answers 3

20

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

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

2 Comments

I have set -timeout=60 and -browserTimeout=180 but didn't find -sessionTimeout in kubernetes selenium grid helm chart. How can I modify this?
Interesting approach, I am thinking if I can query the API in order to discover the active sessions and after this kill the orphaned ones?
9

The delete method works for me. This is my python code:

def clear_sessions(session_id=None):
    """
    Here we query and delete orphan sessions
    docs: https://www.selenium.dev/documentation/grid/advanced_features/endpoints/
    :return: None
    """
    url = "http://127.0.0.1:4444"
    if not session_id:
        # delete all sessions
        r = requests.get("{}/status".format(url))
        data = json.loads(r.text)
        for node in data['value']['nodes']:
            for slot in node['slots']:
                if slot['session']:
                    id = slot['session']['sessionId']
                    r = requests.delete("{}/session/{}".format(url, id))
    else:
        # delete session from params
        r = requests.delete("{}/session/{}".format(url, session_id))

I hope this is helpful for you

1 Comment

Also useful for checking if a session is active in the grid to prevent queues and timeouts.
2

Based on Adrian's answer, you can very easily kill a session by running the following command:

curl -X "DELETE" http://<your grid IP>:4444/session/<sessionId>

(Replace <your grid IP> with the IP of your grid, and <sessionId> with the Id of the session you want to kill). The session Id is the long number you see near the relevant session in the "sessions" tab in the UI of your grid (/ui#/sessions).

Comments

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.