4

我正在使用 Selenium 2 和 Grid 中的 RemoteWebDriver 在多个虚拟机上划分我的测试。假设我有两台 Linux 机器,并且在测试中我指定了在 Linux 机器上运行的功能,但我无法确定正在使用这两台机器中的哪一台。有什么办法,可以弄清楚吗?像 driver.getServerIp() 或其他什么?原因是,在我的 Selenium 测试代码中,我想在运行测试的 linux 机器的控制台上启动一个 bash 脚本。因此我必须知道测试在哪台机器上运行。

多谢你们!

4

3 回答 3

4

我的 java 技能不是最好的,这段代码可能需要一些清理,但这对我有用。

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort + "/grid/api/testsession?session=" +  driver.getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = (JSONObject)new JSONParser().parse(EntityUtils.toString(response.getEntity()));     
String proxyID = object.get("proxyId"));

proxyID 包含节点的 IP。

于 2011-08-25T16:33:16.613 回答
2
HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
ce.getAddressOfRemoteServer();

尽管您可能知道地址,因为无论如何您将它传递给 RemoteWebDriver 的构造函数。

于 2011-08-25T13:48:46.707 回答
1

Bobble D 提供的答案仅适用于 Nilesh 提到的 JSONParser 存在问题,这里是更新的代码,可以提供帮助

HttpHost host = new HttpHost(yourHubIP, yourHubPort); 
DefaultHttpClient client = new DefaultHttpClient(); 
URL testSessionApi = new URL("http://" + yourHubIP + ":" + yourHubPort +         "/grid/api/testsession?session=" +  ((RemoteWebDriver) driver).getSessionId()); 
BasicHttpEntityEnclosingRequest r = new 
BasicHttpEntityEnclosingRequest("POST", testSessionApi.toExternalForm()); 
HttpResponse response  = client.execute(host,r);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity()));   
String proxyID = object.get("proxyId"));
System.out.println(proxyID.split("//")[1].split(":")[0]);
于 2012-06-29T10:13:18.610 回答