agreif
agreif

Reputation: 421

how to get the number of busy selenium hub slots

Is there a way to get the number of busy slots on a selenium hub? I only know the hub console url (/grid/console), there I can parse the html code ... but this is quite ugly, and quite slow if there are many slots.

Maybe somebody knows a better way?

thanks, Alex.

Upvotes: 0

Views: 1121

Answers (2)

Carlos Cuesta
Carlos Cuesta

Reputation: 1484

This is an old question but perhaps this solution help others. You can extend org.openqa.grid.web.servlet.RegistryBasedServlet, inject it to the hub and get information via http/json

This is the code for the class:

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.ProxySet;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

public class NodesInfoServlet extends RegistryBasedServlet {

    private static final long serialVersionUID = -5559403361498232207L;

    public NodesInfoServlet() {
        super(null);
    }

    public NodesInfoServlet(Registry registry) {
        super(registry);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req, resp);
    }

    protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/json");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(200);
        JsonObject res;
        try {
            res = getFreeBrowsers();
            response.getWriter().print(res);
            response.getWriter().close();
        } catch (JsonIOException e) {
            throw new GridException(e.getMessage());
        }
    }

    private JsonObject getFreeBrowsers() throws IOException, JsonIOException {
        JsonObject requestJSON = new JsonObject();
        ProxySet proxies = this.getRegistry().getAllProxies();

        JsonObject platformsbrowsers = new JsonObject();
        for (RemoteProxy proxy : proxies) {
            for (TestSlot slot : proxy.getTestSlots()) {
                if (slot.getSession() == null){
                    // Plataforma del slot
                    Platform ptf = getPlatform(slot);
                    if (ptf != null){
                        JsonArray browserlist = new JsonArray();
                        if (!platformsbrowsers.has(ptf.toString())){
                            platformsbrowsers.add(ptf.toString(), browserlist);
                        }
                        JsonArray platform = platformsbrowsers.getAsJsonArray(ptf.toString());

                        // Browser del slot
                        DesiredCapabilities cap = new DesiredCapabilities(slot.getCapabilities());
                        JsonPrimitive browser = new JsonPrimitive(cap.getBrowserName());
                        if (!platform.contains(browser)){
                            platform.add(browser);
                        }
                    }
                }
            }
        }
        requestJSON.add("PlatformsBrowsers", platformsbrowsers);

        return requestJSON;
    }

    private static Platform getPlatform(TestSlot slot) {
        Object o = slot.getCapabilities().get(CapabilityType.PLATFORM);
        if (o == null) {
            return Platform.ANY;
        } else {
            if (o instanceof String) {
                return Platform.valueOf((String) o);
            } else if (o instanceof Platform) {
                return (Platform) o;
            } else {
                throw new GridException("Cannot cast " + o + " to org.openqa.selenium.Platform");
            }
        }
    }
}

You need to inject in the hub:

java -classpath C:\Selenium\selenium-server-standalone.jar;C:\Selenium\NodesInfoServlet.jar; org.openqa.grid.selenium.GridLauncher -role hub -hubConfig hubConfig.json -servlets NodesInfoServlet

You get the results via http:

http://localhost:4444/grid/admin/NodesInfoServlet

Result:

{
    PlatformsBrowsers: {
        VISTA: [
            "internet explorer",
            "phantomjs",
            "htmlunit"
        ]
    }
}

Hope this helps!!!

Upvotes: 3

Jorge Mathias
Jorge Mathias

Reputation: 116

What I researched so far, the Selenium API is not providing a method to get the hub information from the client side. It is pretty ugly to parse the source code but that is working for me. Other people mentioned to make some changes to the selenium hub jar file but that won't be scalable and it will be needed with each selenium version. (Firefox versions and Selenium versions are all the time changing... I wouldn't recommend you to do that)

If you are using Java, you can try to use Json simple to parse the string and get easily the browsers. https://code.google.com/p/json-simple/

I hope this can be helpful.

Upvotes: 1

Related Questions