RemoteWebDriver and Grid - is it possible to get t

2019-02-14 20:27发布

问题:

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!

回答1:

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.



回答2:

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:

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]);