Created
February 8, 2012 08:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 - 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
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