Skip to content

Commit

Permalink
Merge pull request #67 from zebrunner/develop
Browse files Browse the repository at this point in the history
2.1 rc
  • Loading branch information
vdelendik authored Apr 23, 2022
2 parents bc5bab4 + 73240b4 commit 0c5ed0c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ENV STF_URL ""
ENV STF_TOKEN ""
ENV STF_TIMEOUT 3600
ENV HTTP_CLIENT_RETRY_COUNT 1
ENV CHECK_APPIUM_STATUS false

# Grid settings
# As integer, maps to "maxSession"
Expand Down
49 changes: 48 additions & 1 deletion src/main/java/com/zebrunner/mcloud/grid/MobileRemoteProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;

import com.zebrunner.mcloud.grid.integration.client.Path;
import com.zebrunner.mcloud.grid.integration.client.STFClient;
import com.zebrunner.mcloud.grid.models.stf.STFDevice;
import com.zebrunner.mcloud.grid.util.HttpClient;
import com.zebrunner.mcloud.grid.util.HttpClient.Response;

/**
* Mobile proxy that connects/disconnects STF devices.
Expand All @@ -45,6 +48,8 @@ public class MobileRemoteProxy extends DefaultRemoteProxy {
private final String TIMEOUT = System.getenv("STF_TIMEOUT");

private static final String STF_CLIENT = "STF_CLIENT";

private final boolean CHECK_APPIUM_STATUS = Boolean.parseBoolean(System.getenv("CHECK_APPIUM_STATUS"));

public MobileRemoteProxy(RegistrationRequest request, GridRegistry registry) {
super(request, registry);
Expand Down Expand Up @@ -81,10 +86,52 @@ public TestSession getNewSession(Map<String, Object> requestedCapability) {
}

// Check if device is busy in STF
if (client.isEnabled() && !client.isDeviceAvailable((String) testslot.getCapabilities().get("udid"))) {
String udid = (String) testslot.getCapabilities().get("udid");
if (client.isEnabled() && !client.isDeviceAvailable(udid)) {
return null;
}

// additional check if device is ready for session with custom Appium's status verification

if (this.CHECK_APPIUM_STATUS) {
LOGGER.info("CHECK_APPIUM_STATUS is enabled so additional Appium health-check will be verified");
try {
Platform platform = Platform.fromCapabilities(testslot.getCapabilities());
Response<String> response;
switch (platform) {
case ANDROID:
response = HttpClient.uri(Path.APPIUM_STATUS_ADB, testslot.getRemoteURL().toString()).get(String.class);
if (response.getStatus() != 200) {
LOGGER.warning(String.format(
"Device with udid %s is not ready for a session. Error status was received from Appium (/status-adb): %s", udid,
response.getObject()));
return null;
}
LOGGER.info(String.format("Extra Appium health-check (/status-adb) successfully passed for device with udid=%s", udid));
LOGGER.fine("/status-adb response content: " + response.getObject());
break;
case IOS:
response = HttpClient.uri(Path.APPIUM_STATUS_WDA, testslot.getRemoteURL().toString()).get(String.class);
if (response.getStatus() != 200) {
LOGGER.warning(
String.format(
"Device with udid %s is not ready for a session. Error status was received from Appium (/status-wda): %s",
udid, response.getObject()));
return null;
}
LOGGER.info(String.format("Extra Appium health-check (/status-wda) successfully passed for device with udid=%s", udid));
LOGGER.fine("/status-wda response content: " + response.getObject());
break;
default:
LOGGER.info(String.format("Current platform %s is not supported for extra Appium health-check", platform.toString()));
}
} catch (Exception e) {
LOGGER.warning("Exception happened during extra health-check for Appium: " + e.getMessage());
}
} else {
LOGGER.info("CHECK_APPIUM_STATUS is not enabled!");
}

TestSession session = testslot.getNewSession(requestedCapability);
// remember current STF client in test session object
session.put(STF_CLIENT, client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public enum Path {
STF_USER_DEVICES_PATH("/api/v1/user/devices"),
STF_USER_DEVICES_BY_ID_PATH("/api/v1/user/devices/%s"),
STF_USER_DEVICES_REMOTE_CONNECT_PATH("/api/v1/user/devices/%s/remoteConnect"),
APPIUM_STATUS("/status"),
APPIUM_START_RECORDING_SCREEN_PATH("/session/%s/appium/start_recording_screen"),
APPIUM_STOP_RECORDING_SCREEN_PATH("/session/%s/appium/stop_recording_screen"),
APPIUM_GET_LOG_TYPES_PATH("/session/%s/log/types"),
APPIUM_GET_LOGS_PATH("/session/%s/log");
APPIUM_GET_LOGS_PATH("/session/%s/log"),
APPIUM_STATUS("/status"),
APPIUM_STATUS_WDA("/status-wda"),
APPIUM_STATUS_ADB("/status-adb");

private final String relativePath;

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/zebrunner/mcloud/grid/util/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*******************************************************************************/
package com.zebrunner.mcloud.grid.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
Expand Down Expand Up @@ -171,7 +175,15 @@ private <R> Response<R> execute(Class<R> responseClass, Function<WebResource.Bui
int status = response.getStatus();
rs.setStatus(status);
if (responseClass != null && !responseClass.isAssignableFrom(Void.class) && status == 200) {
rs.setObject(response.getEntity(responseClass));
if (responseClass.isAssignableFrom(String.class)) {
String text = new BufferedReader(
new InputStreamReader(response.getEntityInputStream(), StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
rs.setObject((R) text);
} else {
rs.setObject(response.getEntity(responseClass));
}
}
return rs;
});
Expand Down

0 comments on commit 0c5ed0c

Please sign in to comment.