|
23 | 23 | import io.appium.java_client.LocksDevice; |
24 | 24 | import io.appium.java_client.android.AndroidDriver; |
25 | 25 | import io.appium.java_client.ios.IOSDriver; |
| 26 | +import org.apache.commons.io.IOUtils; |
26 | 27 | import org.apache.http.HttpEntity; |
27 | 28 | import org.apache.http.HttpResponse; |
28 | 29 | import org.apache.http.client.HttpClient; |
29 | 30 | import org.apache.http.client.methods.HttpGet; |
| 31 | +import org.apache.http.client.methods.HttpPost; |
| 32 | +import org.apache.http.entity.StringEntity; |
30 | 33 | import org.apache.http.impl.client.HttpClientBuilder; |
31 | 34 | import org.apache.http.util.EntityUtils; |
32 | 35 | import org.apache.logging.log4j.LogManager; |
|
66 | 69 | import org.springframework.stereotype.Service; |
67 | 70 |
|
68 | 71 | import java.io.IOException; |
| 72 | +import java.io.InputStream; |
| 73 | +import java.io.StringWriter; |
69 | 74 | import java.net.MalformedURLException; |
70 | 75 | import java.net.URL; |
71 | 76 | import java.time.Duration; |
@@ -1201,43 +1206,80 @@ public boolean stopServer(TestCaseExecution tce) { |
1201 | 1206 | private static void getIPOfNode(TestCaseExecution tCExecution) { |
1202 | 1207 | try { |
1203 | 1208 | Session session = tCExecution.getSession(); |
1204 | | - RemoteWebDriver driver = (RemoteWebDriver) session.getDriver(); |
1205 | | - SessionId sessionId = driver.getSessionId(); |
| 1209 | + String nodeUri; |
1206 | 1210 |
|
1207 | | - // Create a HttpClient |
1208 | 1211 | HttpClient client = HttpClientBuilder.create().build(); |
1209 | 1212 |
|
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); |
1212 | 1214 |
|
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 | + } |
1223 | 1220 |
|
1224 | | - if (nodeIp != null) { |
1225 | | - URL myURL = new URL(nodeIp); |
| 1221 | + if (nodeUri != null) { |
| 1222 | + URL myURL = new URL(nodeUri); |
1226 | 1223 | 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()); |
1228 | 1225 | tCExecution.setRobotHost(myURL.getHost()); |
1229 | 1226 | tCExecution.setRobotPort(String.valueOf(myURL.getPort())); |
1230 | 1227 | // Node information at session level is now overwrite with real values. |
1231 | 1228 | tCExecution.getSession().setNodeHost(myURL.getHost()); |
1232 | 1229 | tCExecution.getSession().setNodePort(String.valueOf(myURL.getPort())); |
1233 | 1230 | } |
1234 | 1231 | } 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"); |
1236 | 1233 | } |
| 1234 | + } catch (IOException | JSONException ex) { |
| 1235 | + LOG.error(ex.toString(), ex); |
| 1236 | + } |
| 1237 | + } |
1237 | 1238 |
|
| 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 | + } |
1238 | 1259 | } catch (IOException | JSONException ex) { |
1239 | 1260 | LOG.error(ex.toString(), ex); |
1240 | 1261 | } |
| 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 | + } |
1241 | 1283 | } |
1242 | 1284 |
|
1243 | 1285 | @Override |
|
0 commit comments