Skip to content

Commit d6d9867

Browse files
committed
adding sample to get node details
1 parent e808bdf commit d6d9867

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<dependency>
1313
<groupId>org.seleniumhq.selenium</groupId>
1414
<artifactId>selenium-server</artifactId>
15-
<version>2.33.0</version>
15+
<version>2.32.0</version>
1616
</dependency>
1717
<dependency>
1818
<groupId>junit</groupId>
@@ -27,7 +27,12 @@
2727
<dependency>
2828
<groupId>org.apache.httpcomponents</groupId>
2929
<artifactId>httpclient</artifactId>
30-
<version>4.1</version>
30+
<version>4.2.5</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.httpcomponents</groupId>
34+
<artifactId>httpcore</artifactId>
35+
<version>4.2.4</version>
3136
</dependency>
3237
</dependencies>
3338

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.seleniumgrid2api.api;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
8+
9+
10+
public class GridInfo {
11+
12+
private URL proxyId;
13+
private String host;
14+
private int port;
15+
private String internalKey;
16+
private String session;
17+
private String inactivityTime;
18+
private String msg;
19+
private String success;
20+
21+
public URL getProxyId() {
22+
return proxyId;
23+
}
24+
25+
public String getHost() {
26+
return host;
27+
}
28+
29+
public int getPort() {
30+
return port;
31+
}
32+
33+
public String getInternalKey() {
34+
return internalKey;
35+
}
36+
37+
public String getSession() {
38+
return session;
39+
}
40+
41+
public String getInactivityTime() {
42+
return inactivityTime;
43+
}
44+
45+
public String getMsg() {
46+
return msg;
47+
}
48+
49+
public String getSuccess() {
50+
return success;
51+
}
52+
53+
public GridInfo(JSONObject object) {
54+
try {
55+
proxyId = new URL(object.getString("proxyId"));
56+
if ((proxyId.getHost() != null) && (proxyId.getPort() != -1)) {
57+
host = proxyId.getHost();
58+
port = proxyId.getPort();
59+
}
60+
internalKey = object.getString("internalKey");
61+
session = object.getString("session");
62+
inactivityTime = object.getString("inactivityTime");
63+
msg = object.getString("msg");
64+
success = object.getString("success");
65+
66+
} catch (MalformedURLException | JSONException e) {
67+
System.out.println("Error Parsing Grid Info.");
68+
}
69+
70+
}
71+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.seleniumgrid2api.api;
2+
3+
import org.apache.http.HttpHost;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.HttpStatus;
6+
import org.apache.http.impl.client.DefaultHttpClient;
7+
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
import org.openqa.selenium.remote.SessionId;
11+
12+
import java.io.BufferedReader;
13+
import java.io.IOException;
14+
import java.io.InputStreamReader;
15+
import java.net.URL;
16+
17+
public final class GridInfoExtractor {
18+
19+
private GridInfoExtractor() {
20+
}
21+
22+
public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId session) {
23+
24+
GridInfo retVal = null;
25+
26+
try {
27+
HttpHost host = new HttpHost(hubHost, hubPort);
28+
DefaultHttpClient client = new DefaultHttpClient();
29+
URL sessionURL = new URL("http://" + hubHost + ":" + hubPort + "/grid/api/testsession?session=" + session);
30+
BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
31+
HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest);
32+
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
33+
JSONObject object = extractObject(response);
34+
retVal = new GridInfo(object);
35+
} else {
36+
System.out.println("Problem connecting to Grid Server");
37+
}
38+
} catch (JSONException | IOException e) {
39+
throw new RuntimeException("Failed to acquire remote webdriver node and port info", e);
40+
}
41+
return retVal;
42+
}
43+
44+
private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
45+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()))) {
46+
StringBuilder stringBuilder = new StringBuilder();
47+
String line;
48+
while ((line = bufferedReader.readLine()) != null) {
49+
stringBuilder.append(line);
50+
}
51+
bufferedReader.close();
52+
return new JSONObject(stringBuilder.toString());
53+
} catch (Exception e) {
54+
System.out.println("error" + e.toString());
55+
}
56+
return null;
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.seleniumgrid2api;
2+
3+
import com.seleniumgrid2api.api.GridInfo;
4+
import com.seleniumgrid2api.api.GridInfoExtractor;
5+
import org.junit.Test;
6+
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.remote.DesiredCapabilities;
8+
import org.openqa.selenium.remote.RemoteWebDriver;
9+
10+
import java.net.URL;
11+
12+
public class GetNodeDetails {
13+
14+
@Test
15+
public void getNodeDetailsViaTestSession() throws Exception {
16+
17+
DesiredCapabilities capability = DesiredCapabilities.firefox();
18+
19+
URL hub = new URL("http://localhost:4444/wd/hub");
20+
21+
WebDriver driver = new RemoteWebDriver(hub, capability);
22+
23+
System.out.println("Session Id: " + ((RemoteWebDriver) driver).getSessionId());
24+
25+
GridInfo gridInfo = GridInfoExtractor.getHostNameAndPort(hub.getHost(), hub.getPort(), ((RemoteWebDriver) driver).getSessionId());
26+
27+
System.out.println("I'm running on: " + gridInfo.getHost() + ":" + gridInfo.getPort());
28+
29+
}
30+
}

0 commit comments

Comments
 (0)