-
-
Save krmahadevan/1766772 to your computer and use it in GitHub Desktop.
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; | |
} | |
} |
Here's the Ruby version (for v1.9+, for older versions, may have to resort to require 'net/http'
and use that or something else instead of this REST client to make the HTTP request to get proxy URL with hostname)
#gem install rest-client & selenium as needed
require "selenium-webdriver"
require 'rest-client'
require 'json'
require 'uri'
caps = Selenium::WebDriver::Remote::Capabilities.firefox
driver = Selenium::WebDriver.for(:remote, :url => "http://localhost:4444/wd/hub", :desired_capabilities => caps)
grid_node_extract_url = "http://localhost:4444/grid/api/testsession?session=#{driver.session_id}"
response = RestClient.get grid_node_extract_url
response = JSON.parse(response)
node_host = URI(response['proxyId']).host # use .port for port
puts node_host
driver.quit
getting lot of compilation error, can you please add the imported package as well, if possible? or are those still valid as this post is 10 years old?
@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
@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
@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.
And here's a snippet for Python 2.x, for getting hostname
For Python 3.x, making the following substitutions should work