我们用Selenium Grid来运行测试。然而,在我们的测试失败时,我们想知道测试是运行在哪个Node机器上面。但是Selenium API并没有办法简单获取这个值。所以我需要发送请求给Grid来查看当前Session所在的Node地址。
try {
HttpHost host = new HttpHost(hub, port);
DefaultHttpClient client = new DefaultHttpClient();
String sessionUrl = "http://" + hub + ":" + port
+ "/grid/api/testsession?session=";
URL session = new URL(sessionUrl
+ ((RemoteWebDriver) driver).getSessionId());
BasicHttpEntityEnclosingRequest req;
req = new BasicHttpEntityEnclosingRequest("POST",
session.toExternalForm());
org.apache.http.HttpResponse response = client.execute(host, req);
JSONObject object = new JSONObject(EntityUtils.toString(response
.getEntity()));
String proxyID = (String) object.get("proxyId");
String node = (proxyID.split("//")[1].split(":")[0]);
System.out.println("WebDriver running in node:"+node);
Logging.log("WebDriver running in node:"+node);
} catch (Exception ex) {
}