Amphagory
Amphagory

Reputation: 71

Selenium Grid: Node API?

The problem:

I want to run Selenium Grid on AWS and would like to use their dynamic scaling. On scale down, it will just terminate an instance... which mean that a node can disappear just like that. Not the behaviour I would like, but using scripts or lifecycle hooks, I can try and make sure that any sessions on the node is not active before it is terminated.

Seems like I can hit this API to disconnect the node from the hub: http://NODE-IP:5555/selenium-server/driver/?cmd=shutDownSeleniumServer

Ideally, I need to find an API to the node directly to gather data of session activity.

Alternatives? Sessions logs?

Upvotes: 1

Views: 1323

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

Note: This answer is valid only for Selenium 3.x series (3.14.1 which is as of today the last of the builds in Selenium 3 series). Selenium 4 grid architecture is a complete different one and as such this answer will not necessarily be relevant for Selenium 4 grid (Its yet to be released).

Couple of things. What you are asking for sounds like you need a sort of self healing mechanism. This is not available in the plain vanilla selenium grid flavor.

Selenium node, doesn't have the capability to track sessions that are running within it.

You need to build all of this at the Selenium Hub (which is where all this information resides in).

On a high level, you would need to do the following

  1. Build a custom proxy by extending org.openqa.grid.selenium.proxy.DefaultRemoteProxy which would have the following capabilities:
    1. Add an API which when used would mark the proxy as quiesced (meaning the node has been marked for maintenance and will no longer accept any new session requests)
    2. Override getNewSession(Map<String, Object> requestedCapability) such that it first checks if a node is not quiesced and only then facilitate a new session.
  2. Build a custom servlet which when invoked can do the following:
    1. Given a node it can use the API built via 1.1 and mark a node as quiesced
    2. would return back the list of nodes that don't have any sessions running in them. If you build your servlet by extending org.openqa.grid.web.servlet.RegistryBasedServlet, within your servlet you should be able to get the list of free node urls by doing something like below
List<RemoteProxy> freeProxies =
    StreamSupport.stream(getRegistry().getAllProxies().spliterator(), false)
        .filter(remoteProxy -> !remoteProxy.isBusy())
        .collect(Collectors.toList());
List<URL> urls =
    freeProxies.stream().map(RemoteProxy::getRemoteHost).collect(Collectors.toList());

Now that we have the custom Hub which is now enabled with functionality to do this cleanup, you could now first invoke the 2.1 end-point to mark nodes to be shutdown and then keep polling 2.2 end-point to retrieve all the IP and Port combinations for the nodes that are no longer supporting any test session and then invoke http://NODE-IP:5555/selenium-server/driver/?cmd=shutDownSeleniumServer on them.

That on a high level can do what you are looking for.

Some useful links which can help you get oriented on this (All of the provided links are blogs that I wrote at various points in time).

  1. Self healing grid - https://rationaleemotions.wordpress.com/2013/01/28/building-a-self-maintaining-grid-environment/
  2. Building a custom proxy - https://rationaleemotions.github.io/gridopadesham/CUSTOM_PROXY.html
  3. Building a custom servlet for the hub - https://rationaleemotions.github.io/gridopadesham/CUSTOM_SERVLETS.html

Upvotes: 3

Related Questions