Skip to content

Commit 43ff8a9

Browse files
committed
fix: getIpOfNode
1 parent 55cfd03 commit 43ff8a9

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

source/src/main/java/org/cerberus/core/engine/execution/impl/RobotServerService.java

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import io.appium.java_client.LocksDevice;
2424
import io.appium.java_client.android.AndroidDriver;
2525
import io.appium.java_client.ios.IOSDriver;
26+
import org.apache.commons.io.IOUtils;
2627
import org.apache.http.HttpEntity;
2728
import org.apache.http.HttpResponse;
2829
import org.apache.http.client.HttpClient;
2930
import org.apache.http.client.methods.HttpGet;
31+
import org.apache.http.client.methods.HttpPost;
32+
import org.apache.http.entity.StringEntity;
3033
import org.apache.http.impl.client.HttpClientBuilder;
3134
import org.apache.http.util.EntityUtils;
3235
import org.apache.logging.log4j.LogManager;
@@ -66,6 +69,8 @@
6669
import org.springframework.stereotype.Service;
6770

6871
import java.io.IOException;
72+
import java.io.InputStream;
73+
import java.io.StringWriter;
6974
import java.net.MalformedURLException;
7075
import java.net.URL;
7176
import java.time.Duration;
@@ -1201,43 +1206,80 @@ public boolean stopServer(TestCaseExecution tce) {
12011206
private static void getIPOfNode(TestCaseExecution tCExecution) {
12021207
try {
12031208
Session session = tCExecution.getSession();
1204-
RemoteWebDriver driver = (RemoteWebDriver) session.getDriver();
1205-
SessionId sessionId = driver.getSessionId();
1209+
String nodeUri;
12061210

1207-
// Create a HttpClient
12081211
HttpClient client = HttpClientBuilder.create().build();
12091212

1210-
// Create a HttpGet request to the Selenium Grid's REST API
1211-
HttpGet request = new HttpGet("http://" + tCExecution.getRobotHost() + ":" + tCExecution.getRobotPort() + "/wd/hub/session/" + sessionId);
1213+
nodeUri = getIpOfNodeSelenium4(client, session);
12121214

1213-
// Execute the request
1214-
HttpResponse response = client.execute(request);
1215-
1216-
// Parse the response
1217-
HttpEntity entity = response.getEntity();
1218-
String content = EntityUtils.toString(entity);
1219-
JSONObject json = new JSONObject(content);
1220-
1221-
// Get the IP of the node from the JSON response
1222-
String nodeIp = json.getJSONObject("value").getJSONObject("capabilities").getString("proxyId");
1215+
//If null, we are on selenium 3
1216+
if (StringUtil.isEmptyOrNull(nodeUri)) {
1217+
LOG.debug("nodeUri is null. Trying to get with Selenium 3.");
1218+
nodeUri = getIpOfNodeSelenium3(client, session);
1219+
}
12231220

1224-
if (nodeIp != null) {
1225-
URL myURL = new URL(nodeIp);
1221+
if (nodeUri != null) {
1222+
URL myURL = new URL(nodeUri);
12261223
if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
1227-
LOG.debug("Get remote node information : {} - {}", myURL.getHost(), myURL.getPort());
1224+
LOG.debug("Obtained remote node information : {} - {}", myURL.getHost(), myURL.getPort());
12281225
tCExecution.setRobotHost(myURL.getHost());
12291226
tCExecution.setRobotPort(String.valueOf(myURL.getPort()));
12301227
// Node information at session level is now overwrite with real values.
12311228
tCExecution.getSession().setNodeHost(myURL.getHost());
12321229
tCExecution.getSession().setNodePort(String.valueOf(myURL.getPort()));
12331230
}
12341231
} else {
1235-
LOG.debug("'proxyId' json data not available from remote Selenium Server request");
1232+
LOG.debug("'proxyId' (selenium 3) or 'nodeUri' (selenium 4) json data not available from remote Selenium Server request");
12361233
}
1234+
} catch (IOException | JSONException ex) {
1235+
LOG.error(ex.toString(), ex);
1236+
}
1237+
}
12371238

1239+
private static String getIpOfNodeSelenium3(HttpClient client, Session session) {
1240+
String nodeUri = "";
1241+
try {
1242+
SessionId sessionId = ((RemoteWebDriver) session.getDriver()).getSessionId();
1243+
HttpGet request = new HttpGet(RobotServerService.getBaseUrl(session.getHost(), session.getPort()) + "/grid/api/testsession?session=" + sessionId);
1244+
LOG.debug("Calling Hub to get the node information. {}", request.getURI());
1245+
HttpResponse response = client.execute(request);
1246+
if (!response.getStatusLine().toString().contains("403")
1247+
&& !response.getEntity().getContentType().getValue().contains("text/html")) {
1248+
InputStream contents = response.getEntity().getContent();
1249+
StringWriter writer = new StringWriter();
1250+
IOUtils.copy(contents, writer, "UTF8");
1251+
JSONObject object = new JSONObject(writer.toString());
1252+
if (object.has("proxyId")) {
1253+
nodeUri = object.getString("proxyId");
1254+
} else {
1255+
LOG.debug("'proxyId' json data not available from remote Selenium Server request : {}", writer);
1256+
nodeUri = null;
1257+
}
1258+
}
12381259
} catch (IOException | JSONException ex) {
12391260
LOG.error(ex.toString(), ex);
12401261
}
1262+
return nodeUri;
1263+
}
1264+
1265+
private static String getIpOfNodeSelenium4(HttpClient client, Session session) throws IOException, JSONException {
1266+
LOG.debug("Trying to get node IP with Selenium 4.");
1267+
SessionId sessionId = ((RemoteWebDriver) session.getDriver()).getSessionId();
1268+
HttpPost request = new HttpPost(RobotServerService.getBaseUrl(session.getHost(), session.getPort()) + "/graphql");
1269+
StringEntity params = new StringEntity(String.format("{\"query\":\"{ session (id: \\\"%s\\\") { id, uri, nodeId, nodeUri } } \"}", sessionId));
1270+
request.addHeader("Content-Type", "application/json");
1271+
request.setEntity(params);
1272+
LOG.debug("Calling Hub to get the node information. {}", request.getURI());
1273+
HttpResponse response = client.execute(request);
1274+
//If 404, we are on Selenium 3
1275+
if (response.getStatusLine().getStatusCode() == 404) {
1276+
return null;
1277+
} else {
1278+
HttpEntity entity = response.getEntity();
1279+
String content = EntityUtils.toString(entity);
1280+
JSONObject json = new JSONObject(content);
1281+
return json.getJSONObject("data").getJSONObject("session").getString("nodeUri");
1282+
}
12411283
}
12421284

12431285
@Override

0 commit comments

Comments
 (0)