google-chromenodesselenium-gridnode-config

Force automation on particular node in Selenium grid


We have a Selenium grid setup on our office environment for web automation & for some testing purpose there is a scenario where the user may want to force the automation to happen on Chrome browser on node 3 out of the 4 active nodes. Later on the user can decide to force the Chrome automation on node 2 maybe some other day.

Is there way to force that to happen? I was looking into nodeconfig.json file format & I have not been able to find a parameter that can help me give a unique identity to a node.


Solution

  • This is very much possible. Here's how you do it.

    First you need to add labels to your node via your node configuration.

    For a template of how this node configuration looks like, you can refer to the one in the selenium codebase here

    So here's a sample node configuration file that contains the label:

    {
        "capabilities": [
            {
                "browserName": "chrome",
                "applicationName":"hercules",
                "maxInstances": 10,
                "seleniumProtocol": "WebDriver"
            }
        ],
        "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        "maxSession": 10
    }
    

    So here we are adding a label to the chrome browser and calling it as "hercules". This can be anything but the key always has to be applicationName

    Now you start a node by passing in this node configuration file via the command line -nodeConfig. For more details on this refer to my grid tutorial here

    You would now instantiate your RemoteWebDriver instance as shown below:

    DesiredCapabilities caps = DesiredCapabilities.chrome();
    String whichNode = System.getProperty("nodeName", "");
    if (!whichNode.trim().isEmpty()) {
        caps.setCapability("applicationName", whichNode);
    }
    
    RemoteWebDriver driver = new RemoteWebDriver(gridUrl, caps);
    

    Here as you see, we are setting the same key viz., applicationName and passing in a value, if you specified something via the JVM argument -DnodeName. If you dont pass anything, the test gets routed using the default logic. If you passed in an application name, then it gets routed to the node which matches the application name.

    The above doesn't require you to change anything in the selenium grid and you can use everything as is.

    But if you would like something more sophisticated, then you can do so by building your own Custom capability matcher, which you would inject at the hub's end, and then work with an appropriate capability at the client side (as in the above sample code).

    I created a detailed blog post about this something back. You can refer to it here for more details.