如何获得繁忙的selenium hub插槽的数量

时间:2014-08-20 09:57:51

标签: selenium

有没有办法获得硒中心的繁忙插槽数量? 我只知道集线器控制台网址(/ grid / console),在那里我可以解析html代码......但是这很难看,而且如果有很多插槽则很慢。

也许有人知道更好的方法?

感谢, 亚历克斯。

2 个答案:

答案 0 :(得分:3)

这是一个老问题,但也许这个解决方案可以帮助其他人。 您可以扩展org.openqa.grid.web.servlet.RegistryBasedServlet,将其注入集线器并通过http / json获取信息

这是该类的代码:

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");
            }
        }
    }
}

您需要注入集线器:

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

您可以通过http:

获得结果
http://localhost:4444/grid/admin/NodesInfoServlet

结果:

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

希望这有帮助!!!

答案 1 :(得分:1)

到目前为止我研究过的是,Selenium API没有提供从客户端获取集线器信息的方法。解析源代码非常难看,但这对我有用。其他人提到要对selenium hub jar文件进行一些更改,但这不会是可扩展的,每个selenium版本都需要它。 (Firefox版本和Selenium版本一直在变化......我不建议你这样做)

如果您使用的是Java,则可以尝试使用Json simple来解析字符串并轻松获取浏览器。 https://code.google.com/p/json-simple/

我希望这会有所帮助。