Using the SessionIds ,
Following python code will print session info from a grid
import urllib.request
import json
grid_url = "http://127.0.0.1:4444/wd/hub"
sessions_req = urllib.request.urlopen(grid_url + "/sessions")
sessions_data = sessions_req.read()
sessions_encoding = sessions_req.info().get_content_charset('utf-8')
sessions = json.loads(sessions_data.decode(sessions_encoding))
for session in sessions["value"]:
print (session["id"])
print (session["capabilities"]["browserName"])
output should be :
26294a77-7ab2-47f1-81fd-e11f593bd960
firefox
29aa25cb-a60a-4454-a35c-315f76ff1251
chrome
After your test completion, you can assert the sessionIds to determine the status of your tests. An active session must have an Id. To get insights of that specific test, inject the Session Id in a driver instance and use getCurrentUrl() or takeScreenshot() method.
If your focus is on management of orphan browsers then selenium Grid can help you at configuration level. The selenium Grid specifically has three parameters that are meant for 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.