[JAVA] Extend Selenium Grid (Hub side)

Introduction

Before RESTing to Selenium Grid for various reasons, I tried to programmatically see the state of Node connected to Hub. You can see it on the console of the web screen, but if possible, I would like you to return it with Json, but there is no such function ...

I couldn't find the code by referring to the original Customizing the Grid, and the startup method seemed to be old: innocent: Is there the latest information somewhere? : thinking:

For this purpose, it is only on the Hub side, but I will record it so as not to forget the extension method.

Software used

soft version Use
java 1.8.0_191
selenium-server-standalone.jar 3.14.0

Environment

I am building a development environment on Windows. Just set it in Maven. Use gson to generate selenium-server and response Json. aWS050498.JPG

Maven settings

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>selenium-grid-extend</groupId>
  <artifactId>selenium-grid-extend</artifactId>
  <version>0.0.1</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>src</testSourceDirectory>
    <resources>
      <resource>
        <directory>resource</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>resource</directory>
      </testResource>
    </testResources>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-server</artifactId>
      <version>3.141.5</version>
    </dependency>
    <dependency>
    	<groupId>com.google.code.gson</groupId>
    	<artifactId>gson</artifactId>
    	<version>2.8.5</version>
    </dependency>
  </dependencies>
  <properties>
    <java.version>1.8</java.version>
    <file.encoding>UTF-8</file.encoding>
    <project.build.sourceEncoding>${file.encoding}</project.build.sourceEncoding>
    <project.reporting.outputEncoding>${file.encoding}</project.reporting.outputEncoding>
    <maven.compiler.encoding>${file.encoding}</maven.compiler.encoding>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
  </properties>
</project>

program

According to Customizing the Grid, inherit RegistryBasedServlet when accessing the inside of Hub, and inherit HttpServlet if not. It seems to be good.

This time I want to see the state of Node connected to Hub, so I will inherit RegistryBasedServlet. I created two programs as a trial.

Extension class that returns all Nodes as they are in Json

AllNodes.java


package selenium.extend.hub.servlet;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.ProxySet;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.servlet.RegistryBasedServlet;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

public class AllNodes extends RegistryBasedServlet {

	public AllNodes() {
		this(null);
	}

	public AllNodes(GridRegistry registry) {
		super(registry);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.setStatus(200);

		try {
			JsonObject res = getResponse(request);
			response.getWriter().print(res);
			response.getWriter().close();
		} catch (JsonSyntaxException e) {
			throw new GridException(e.getMessage());
		}
	}

	private JsonObject getResponse(HttpServletRequest request) {
		JsonObject json = new JsonObject();
		ProxySet proxies = super.getRegistry().getAllProxies();
		json.add("Nodes", getNodes(proxies));
		return json;
	}

	private JsonArray getNodes(ProxySet proxies) {
		JsonArray array = new JsonArray();
		Iterator<RemoteProxy> itr = proxies.iterator();
		Gson gson = new Gson();

		while (itr.hasNext()) {
			RemoteProxy proxy = itr.next();
			JsonObject proxyJson = new JsonObject();
			proxyJson.add("Node", gson.toJsonTree(proxy.getOriginalRegistrationRequest()));
			array.add(proxyJson);
		}
		return array;
	}

}
Extension class that divides all Nodes into unused / used and returns with Json

AllNodesState.java


package selenium.extend.hub.servlet;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.ProxySet;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.servlet.RegistryBasedServlet;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

public class AllNodesState extends RegistryBasedServlet {

	public AllNodesState() {
		this(null);
	}

	public AllNodesState(GridRegistry registry) {
		super(registry);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.setStatus(200);

		try {
			JsonObject res = getResponse(request);
			response.getWriter().print(res);
			response.getWriter().close();
		} catch (JsonSyntaxException e) {
			throw new GridException(e.getMessage());
		}
	}

	private JsonObject getResponse(HttpServletRequest request) {
		JsonObject json = new JsonObject();
		ProxySet proxies = super.getRegistry().getAllProxies();
		json.add("Nodes", getNodes(proxies));
		return json;
	}

	private JsonArray getNodes(ProxySet proxies) {
		JsonArray array = new JsonArray();
		Iterator<RemoteProxy> itr = proxies.iterator();
		Gson gson = new Gson();

		JsonArray freeProxies = new JsonArray();
		JsonArray busyProxies = new JsonArray();

		while (itr.hasNext()) {
			RemoteProxy proxy = itr.next();
			JsonObject proxyJson = new JsonObject();
			proxyJson.add("Node", gson.toJsonTree(proxy.getOriginalRegistrationRequest()));

			if (!proxy.isBusy()) {
				freeProxies.add(proxyJson);
			} else {
				busyProxies.add(proxyJson);
			}
		}

		JsonObject freeJson = new JsonObject();
		freeJson.add("FreeNodes", freeProxies);
		array.add(freeJson);

		JsonObject busyJson = new JsonObject();
		busyJson.add("BusyNodes", busyProxies);
		array.add(busyJson);

		return array;
	}

}

Make it a jar

Only the created class is included. This time I created it with the name extend.jar. aWS050500.JPG

Operation check

Directory structure

Place all the dependent jars in lib.

C:\GRID
├─start-hub-extend.bat
│
└─lib
     ├ extend.jar
     ├ gson-2.8.5.jar
     └ selenium-server-standalone-3.14.0.jar
Hub start command

All you have to do is specify the classpath and start it. I specify the class to be added with -servlets, but this time there are two, so they are separated by commas.

start-hub-extend.bat


java -cp lib/* org.openqa.grid.selenium.GridLauncherV3 -role hub -servlets "selenium.extend.hub.servlet.AllNodes,selenium.extend.hub.servlet.AllNodesState"
Selenium Grid log

When I try to execute the batch, the characters "[Hub. \ <Init >]" that are not normally displayed appear on the 3rd and 4th lines. Simply put, you should access it with "/ grid / admin / AllNodes /".

14:43:51.090 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.14.0', revision: 'aacccce0'
14:43:51.106 INFO [GridLauncherV3$2.launch] - Launching Selenium Grid hub on port 4444
14:43:51.137 INFO [Hub.<init>] - binding selenium.extend.hub.servlet.AllNodes to /grid/admin/AllNodes/*
14:43:51.137 INFO [Hub.<init>] - binding selenium.extend.hub.servlet.AllNodesState to /grid/admin/AllNodesState/*
2018-11-09 14:43:51.480:INFO::main: Logging initialized @780ms to org.seleniumhq.jetty9.util.log.StdErrLog
14:43:51.777 INFO [Hub.start] - Selenium Grid hub is up and running
14:43:51.777 INFO [Hub.start] - Nodes should register to http://XXX.XXX.XXX.XXX:4444/grid/register/
14:43:51.777 INFO [Hub.start] - Clients should connect to http://XXX.XXX.XXX.XXX:4444/wd/hub
Try to access

/ grid / console is in the following state aWS050502.JPG

With / grid / admin / AllNodes ...

{
  "Nodes": [
    {
      "Node": {
        "configuration": {
          "remoteHost": "http://XXX.XXX.XXX.XXX:37803",
          "id": "http://XXX.XXX.XXX.XXX:37803",
          "capabilities": [
            {
              "caps": {
                "browserName": "chrome",
                "maxInstances": 1,
                "platform": "WINDOWS",
                "platformName": "WINDOWS",
                "seleniumProtocol": "WebDriver",
                "server:CONFIG_UUID": "8405f2b4-e375-4251-a2e8-50e432eb24b8"
              }
            }
          ],
          "downPollingLimit": 2,
          "hub": "http://XXX.XXX.XXX.XXX:4444/grid/register",
          "nodePolling": 5000,
          "nodeStatusCheckTimeout": 5000,
          "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
          "register": true,
          "registerCycle": 5000,
          "unregisterIfStillDownAfter": 60000,
          "enablePlatformVerification": true,
          "custom": {},
          "maxSession": 5,
          "servlets": [],
          "withoutServlets": [],
          "avoidProxy": false,
          "browserSideLog": false,
          "captureLogsOnQuit": false,
          "browserTimeout": 0,
          "debug": false,
          "host": "172.16.11.134",
          "port": 37803,
          "role": "node",
          "timeout": 1800
        }
      }
    },
    {
      "Node": {
        "configuration": {
          "remoteHost": "http://XXX.XXX.XXX.XXX:3753",
          "id": "http://XXX.XXX.XXX.XXX:3753",
          "capabilities": [
            {
              "caps": {
                "browserName": "firefox",
                "maxInstances": 2,
                "platform": "WINDOWS",
                "platformName": "WINDOWS",
                "seleniumProtocol": "WebDriver",
                "server:CONFIG_UUID": "61acf94e-1db8-4274-9587-1e32a0ebb378"
              }
            },
            {
              "caps": {
                "browserName": "internet explorer",
                "maxInstances": 1,
                "platform": "WINDOWS",
                "platformName": "WINDOWS",
                "seleniumProtocol": "WebDriver",
                "server:CONFIG_UUID": "4adb7e6b-784e-46c3-896f-a2fcb7413bfb"
              }
            }
          ],
          "downPollingLimit": 2,
          "hub": "http://XXX.XXX.XXX.XXX:4444/grid/register",
          "nodePolling": 5000,
          "nodeStatusCheckTimeout": 5000,
          "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
          "register": true,
          "registerCycle": 5000,
          "unregisterIfStillDownAfter": 60000,
          "enablePlatformVerification": true,
          "custom": {},
          "maxSession": 5,
          "servlets": [],
          "withoutServlets": [],
          "avoidProxy": false,
          "browserSideLog": false,
          "captureLogsOnQuit": false,
          "browserTimeout": 0,
          "debug": false,
          "host": "172.16.8.84",
          "port": 3753,
          "role": "node",
          "timeout": 1800
        }
      }
    }
  ]
}

With / grid / admin / AllNodesState ...

{
  "Nodes": [
    {
      "FreeNodes": [
        {
          "Node": {
            "configuration": {
              "remoteHost": "http://XXX.XXX.XXX.XXX:37803",
              "id": "http://XXX.XXX.XXX.XXX:37803",
              "capabilities": [
                {
                  "caps": {
                    "browserName": "chrome",
                    "maxInstances": 1,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "8405f2b4-e375-4251-a2e8-50e432eb24b8"
                  }
                }
              ],
              "downPollingLimit": 2,
              "hub": "http://XXX.XXX.XXX.XXX:4444/grid/register",
              "nodePolling": 5000,
              "nodeStatusCheckTimeout": 5000,
              "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
              "register": true,
              "registerCycle": 5000,
              "unregisterIfStillDownAfter": 60000,
              "enablePlatformVerification": true,
              "custom": {},
              "maxSession": 5,
              "servlets": [],
              "withoutServlets": [],
              "avoidProxy": false,
              "browserSideLog": false,
              "captureLogsOnQuit": false,
              "browserTimeout": 0,
              "debug": false,
              "host": "172.16.11.134",
              "port": 37803,
              "role": "node",
              "timeout": 1800
            }
          }
        },
        {
          "Node": {
            "configuration": {
              "remoteHost": "http://XXX.XXX.XXX.XXX:3753",
              "id": "http://XXX.XXX.XXX.XXX:3753",
              "capabilities": [
                {
                  "caps": {
                    "browserName": "firefox",
                    "maxInstances": 2,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "61acf94e-1db8-4274-9587-1e32a0ebb378"
                  }
                },
                {
                  "caps": {
                    "browserName": "internet explorer",
                    "maxInstances": 1,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "4adb7e6b-784e-46c3-896f-a2fcb7413bfb"
                  }
                }
              ],
              "downPollingLimit": 2,
              "hub": "http://XXX.XXX.XXX.XXX:4444/grid/register",
              "nodePolling": 5000,
              "nodeStatusCheckTimeout": 5000,
              "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
              "register": true,
              "registerCycle": 5000,
              "unregisterIfStillDownAfter": 60000,
              "enablePlatformVerification": true,
              "custom": {},
              "maxSession": 5,
              "servlets": [],
              "withoutServlets": [],
              "avoidProxy": false,
              "browserSideLog": false,
              "captureLogsOnQuit": false,
              "browserTimeout": 0,
              "debug": false,
              "host": "172.16.8.84",
              "port": 3753,
              "role": "node",
              "timeout": 1800
            }
          }
        }
      ]
    },
    {
      "BusyNodes": []
    }
  ]
}
About busy state

It was unexpected, but even if you set several browsers to work, if even one browser is running, that Node seems to be busy. I haven't investigated the API I'm using this time, so I may have made a mistake in using it in the first place: thinking:

For example, even if only one Firefox is running ... aWS050503.JPG

If you look at / grid / admin / AllNodesState, it is determined to be BusyNodes.

{
  "Nodes": [
    {
      "FreeNodes": [
        {
          "Node": {
            "configuration": {
              "remoteHost": "http://172.16.11.134:37803",
              "id": "http://172.16.11.134:37803",
              "capabilities": [
                {
                  "caps": {
                    "browserName": "chrome",
                    "maxInstances": 1,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "8405f2b4-e375-4251-a2e8-50e432eb24b8"
                  }
                }
              ],
              "downPollingLimit": 2,
              "hub": "http://goto-main:4444/grid/register",
              "nodePolling": 5000,
              "nodeStatusCheckTimeout": 5000,
              "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
              "register": true,
              "registerCycle": 5000,
              "unregisterIfStillDownAfter": 60000,
              "enablePlatformVerification": true,
              "custom": {},
              "maxSession": 5,
              "servlets": [],
              "withoutServlets": [],
              "avoidProxy": false,
              "browserSideLog": false,
              "captureLogsOnQuit": false,
              "browserTimeout": 0,
              "debug": false,
              "host": "172.16.11.134",
              "port": 37803,
              "role": "node",
              "timeout": 1800
            }
          }
        }
      ]
    },
    {
      "BusyNodes": [
        {
          "Node": {
            "configuration": {
              "remoteHost": "http://172.16.8.84:3753",
              "id": "http://172.16.8.84:3753",
              "capabilities": [
                {
                  "caps": {
                    "browserName": "firefox",
                    "maxInstances": 2,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "61acf94e-1db8-4274-9587-1e32a0ebb378"
                  }
                },
                {
                  "caps": {
                    "browserName": "internet explorer",
                    "maxInstances": 1,
                    "platform": "WINDOWS",
                    "platformName": "WINDOWS",
                    "seleniumProtocol": "WebDriver",
                    "server:CONFIG_UUID": "4adb7e6b-784e-46c3-896f-a2fcb7413bfb"
                  }
                }
              ],
              "downPollingLimit": 2,
              "hub": "http://localhost:4444/grid/register",
              "nodePolling": 5000,
              "nodeStatusCheckTimeout": 5000,
              "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
              "register": true,
              "registerCycle": 5000,
              "unregisterIfStillDownAfter": 60000,
              "enablePlatformVerification": true,
              "custom": {},
              "maxSession": 5,
              "servlets": [],
              "withoutServlets": [],
              "avoidProxy": false,
              "browserSideLog": false,
              "captureLogsOnQuit": false,
              "browserTimeout": 0,
              "debug": false,
              "host": "172.16.8.84",
              "port": 3753,
              "role": "node",
              "timeout": 1800
            }
          }
        }
      ]
    }
  ]
}

Recommended Posts

Extend Selenium Grid (Hub side)
JMX support for Selenium Grid
Add image selector to Selenium Grid