Reputation: 6910
I am looking for a way I can ensure certain things happen after each test on a Selenium Grid node. (E.g. controlling certain processes on the node)
What is the best way to do on Selenium Grid? Perhaps I don't need to reinvent the wheel and there is some way I can use Selenium Grid infrastructure to determine when a test has finished on a node programatically?
Upvotes: 0
Views: 1880
Reputation: 14746
You can do this in two ways
For achieving this you should be building your own custom proxy (by extending org.openqa.grid.selenium.proxy.DefaultRemoteProxy
) and embed your logic of clean-up as part of afterSession()
so that it gets executed after driver.quit()
is executed but before the session is released by the Grid. You need to ensure that you don’t trigger any exceptions here. Please refer to this article that I wrote up on grid to help you understand how to work with custom proxies.
(or)
If this is what you want, then
Just before calling driver.quit()
you should do the following :
Now after you call driver.quit()
, trigger a http operation (GET/POST)
to the servlet that you added to the node, by making use of the IP and port address that you obtained from above.
Upvotes: 1
Reputation: 1804
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.
Upvotes: 1