RemoteWebDriver and Grid - is it possible to get t

2019-02-14 19:41发布

I am using RemoteWebDriver from Selenium 2 and Grid to divide my tests on several virtual machines. Let's say I have two Linux machines and in a test I specify the capabilities to run on a Linux machine, I can't figure out which of these two machines is being used. Is there any way, to figure it out? Something like driver.getServerIp() or anything else? The reason is, in my Selenium test code I want to start a bash script on the console of the linux machine where the test is being run. Therefore I have to know on which machine the test runs.

Thanks guys!

3条回答
我想做一个坏孩纸
2楼-- · 2019-02-14 20:19
HttpCommandExecutor ce = (HttpCommandExecutor) ((RemoteWebDriver)driver).getCommandExecutor();
ce.getAddressOfRemoteServer();

Although presumably you'd know the address since you passed it into the constructor for RemoteWebDriver anyway.

查看更多
神经病院院长
3楼-- · 2019-02-14 20:27

My java skills aren't the greatest and this code could use some cleaning up, but this works for me.

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 contains the IP of the node.

查看更多
姐就是有狂的资本
4楼-- · 2019-02-14 20:34

Answer provided by Bobble D is working only that there was problem with JSONParser as mentioned by Nilesh as well, here is updated code which can help

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]);
查看更多
登录 后发表回答