我想获得在selenium网格中使用特定浏览器运行的测试数量。
我查看了现有的API,我可以获取slotcount,它是包含所有浏览器的所有可用插槽的总和。
ex:curl -X GET http://localhost:4444/grid/api/hub/ -d '{"configuration":["slotCounts"]}'
输出将是:{"success":true,"slotCounts":{"free":178,"total":196}}
是否有任何API可用于获取Chrome浏览器的插槽数量(比如说)?
我想到的其他选项是解析现有的API
curl -X GET http://localhost:4444/grid/console
返回完整堆栈,我需要解析html结构,就像
<img src='/grid/resources/org/openqa/grid/images/chrome.png' width='16' height='16' class='busy' title='POST - /session/8802ebae-10cb-480d-bbbd-5e7edd7ee7b2/execute executed.' />
答案 0 :(得分:2)
没有。目前,Selenium Grid中没有可用于此的API。
您需要构建一个自定义servlet,在调用时可以为您提取并提供此信息。
您的Hub servlet可能如下所示:
import org.openqa.grid.internal.ProxySet;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.remote.CapabilityType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SimpleServlet extends RegistryBasedServlet {
public SimpleServlet(Registry registry) {
super(registry);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ProxySet proxySet = getRegistry().getAllProxies();
Iterator<RemoteProxy> iterator = proxySet.iterator();
Map<String, List<String>> returnValue = new HashMap<>();
while (iterator.hasNext()) {
RemoteProxy each = iterator.next();
each.getTestSlots().forEach(slot -> {
String browser = (String) slot.getCapabilities().get(CapabilityType.BROWSER_NAME);
String machineIp = each.getRemoteHost().getHost();
List<String> machines = returnValue.get(browser);
if (machines == null) {
machines = new ArrayList<>();
}
machines.add(machineIp);
returnValue.put(browser, machines);
});
}
//Write logic to have the Map returned back as perhaps a JSON payload
}
}
您可以参考Selenium文档this来了解如何将Servlet注入Hub或节点。