Satish Michael
Satish Michael

Reputation: 2015

Selenium Grid - How to shutdown the node after execution?

I am trying to implement a solution that'd shutdown the node running inside a docker (Swarm) container after a test run.

I looked at docker remove command but cannot use the docker container rm command as the containers are at the service-task level

I looked at the /lifecycle-manager api but cannot get to the node from client, the docker stack is running through a nginx server and only one port(4444) gets exposed

Finally I looked at extended the grid node (DefaultRemoteProxy). Excuse my bad java code, this is my first stab at writing java code. With this, it looks like I can stop the node but it gets registered to the hub

How can i stop this re-registration process or start the node without it

My goal is to have a new container for every test and let the docker orchestration bring up a new container when the node is shutdown and container gets removed (docker api https://docs.docker.com/engine/api/v1.24/)

public class ExtendedProxy extends DefaultRemoteProxy implements TestSessionListener {

    public ExtendedProxy(RegistrationRequest request, GridRegistry registry) {
        super(request, registry);
    }

    @Override
    public void afterCommand(TestSession session, HttpServletRequest request, HttpServletResponse response) {
        RequestType type = SeleniumBasedRequest.createFromRequest(request, getRegistry()).extractRequestType();

        if(type == STOP_SESSION) {
            System.out.println("Going to Shutdown the Node");

           GridRegistry registry = getRegistry();
           registry.stop();
           registry.removeIfPresent(this);

        }
    }
}

Hub

[DefaultGridRegistry.assignRequestToProxy] - Shutting down registry.
[DefaultGridRegistry.removeIfPresent] - Cleaning up stale test sessions on the unregistered node
[DefaultGridRegistry.add] - Registered a node

Node

[ActiveSessions$1.onStop] - Removing session de04928d-7056-4b39-8137-27e9a0413024 (org.openqa.selenium.firefox.GeckoDriverService)
[SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://localhost:4444/grid/register
[SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use

Upvotes: 2

Views: 1575

Answers (1)

Satish Michael
Satish Michael

Reputation: 2015

I figured out the solution. I am answering my own question, hoping it'd benefit the community.

Start the node with the command line flag. This stops the auto registration thread from ever getting created.

registerCycle - 0

And in your class that extends DefaultRemoteProxy, override the afterSession

@Override
public void afterSession(TestSession session) {
    totalSessionsCompleted++;

    GridRegistry gridRegistry = getRegistry();
    for(TestSlot slot : getTestSlots()) {
        gridRegistry.forceRelease(slot, SessionTerminationReason.PROXY_REREGISTRATION);
    }
    teardown();
    gridRegistry.removeIfPresent(this);
}

When the client executed the driver.quit() method, the node de-registers with the hub.

Upvotes: 1

Related Questions