Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created February 8, 2012 08:25
Show Gist options
  • Save krmahadevan/1766772 to your computer and use it in GitHub Desktop.
Save krmahadevan/1766772 to your computer and use it in GitHub Desktop.
A utility class that extracts the actual IP and port to which your remote executions are being routed to by Grid2
public class GridInfoExtracter{
private static String[] getHostNameAndPort(String hostName, int port,
SessionId session) {
String[] hostAndPort = new String[2];
String errorMsg = "Failed to acquire remote webdriver node and port info. Root cause: ";
try {
HttpHost host = new HttpHost(hostName, port);
DefaultHttpClient client = new DefaultHttpClient();
URL sessionURL = new URL("http://" + hostName + ":" + port + "/grid/api/testsession?session=" + session);
BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
HttpResponse response = client.execute(host, r);
JSONObject object = extractObject(response);
URL myURL = new URL(object.getString("proxyId"));
if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
hostAndPort[0] = myURL.getHost();
hostAndPort[1] = Integer.toString(myURL.getPort());
}
} catch (Exception e) {
logger.log(Level.SEVERE, errorMsg, e);
throw new RuntimeException(errorMsg, e);
}
return hostAndPort;
}
private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuffer s = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
s.append(line);
}
rd.close();
JSONObject objToReturn = new JSONObject(s.toString());
return objToReturn;
}
}
@mdsapon
Copy link

mdsapon commented Mar 20, 2022

@mdsapon - You should perhaps take a look at this library that I built as a follow up to this gist : https://github.com/RationaleEmotions/talk2grid

Thanks a lot, this is amazing and works great with Selenium 3, but didn't work with Selenium 4. Do you have any plan to upgrade? Btw thanks a ton for creating this library

@krmahadevan
Copy link
Author

@mdsapon - I haven't yet delved into Grid4 completely. But its definitely on the cards. I didnt take this library so seriously because I didn't see many people even have a want to use it :D

I will work on sorting it out hopefully soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment