4

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!

user1
  • 945
  • 2
  • 13
  • 37
tester
  • 3,977
  • 5
  • 39
  • 59

3 Answers3

5

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.

Bobble D
  • 66
  • 3
  • Wow! That is definitely what I am looking for! Thanks a lot! How did you know about the URL query http://hubIP:hubPort/grid/api/testsession?session=xxxxxx ? I mean, is there any documentation to get more of this? Maybe there are more information. But this definitely helped me a lot! Thanks! – tester Aug 26 '11 at 06:21
  • I pulled it from this issue report: http://code.google.com/p/selenium/issues/detail?id=1541 – Bobble D Aug 26 '11 at 14:24
  • 1
    I am using selenium 2.19 and not able to resolve, JSONParser(). Is there any add on jar that I need to add to the class path? – nilesh Feb 23 '12 at 20:44
3
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.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • Sry, but this only returns the address of the hub. I want the address of the machine where the test is running. I start the RemoteWebDriver with the following code: RemoteWebDriver driver = new RemoteWebDriver(new URL(hubUrl), capability); – tester Aug 25 '11 at 14:00
  • Ahhh I see what you're saying. I'm not sure how to do that one. – Mike Kwan Aug 25 '11 at 14:19
  • Okay, maybe it helps if I change something in the hub code. Does anyone know where I can get the source code of the hub? I only have a selenium-server-standalone-2.3.0.jar and I start in command line with the following command: "java -jar selenium-server-standalone-2.3.0.jar -role hub". Maybe I can build in a method to pass the ip of the machine running the test. FIY: I want to automate video capturing on the machine running the test for error tracing. I do it with VLC via command line. But therefore I have to know on which machine the test is running. – tester Aug 25 '11 at 15:31
1

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]);
Leo
  • 11
  • 1