简体   繁体   中英

How to determine a test has finished on a Selenium Grid

I am looking for a way I can ensure certain things happen after each test on a Selenium Grid node. (Eg 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?

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 (ie 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.

You can do this in two ways

  1. If you want the cleanup to happen irrespective of what test finishes ie, lets say you don't care about the test, you merely want to know when the user calls driver.quit() which signals a terminate session message to the hub, so that you can run some sort of clean-up at the node.

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)

  1. If you want to the cleanup to happen with respect to only certain specific tests ie, lets say you may have been running some particular tests, for which you would like the screen-recording to be done and would like to stop it only for those tests that requested it.

If this is what you want, then

  • First you should build a servlet which when invoked can do the cleanup for you. You just inject this servlet into the node. To learn how to do this, please refer to this article of mine.
  • Just before calling driver.quit() you should do the following :

    1. Obtain the IP and port address of the machine to which your test was routed to. You can learn how to do this by referring to this blog post of mine, or just by using this library of mine.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM