Skip to content

Commit

Permalink
Merge pull request #360 from testsigmahq/selenium4
Browse files Browse the repository at this point in the history
Selenium 4 and Appium 2 Upgrade
  • Loading branch information
PratheepV authored Jun 24, 2023
2 parents df5070f + 50889d9 commit 842c2b8
Show file tree
Hide file tree
Showing 155 changed files with 1,551 additions and 630 deletions.
2 changes: 1 addition & 1 deletion agent-launcher/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</plugins>
</build>
<properties>
<lombok.version>1.18.10</lombok.version>
<lombok.version>1.18.22</lombok.version>
<systemtray.version>3.17</systemtray.version>
<maven.compiler.plugin>3.8.1</maven.compiler.plugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion agent-launcher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<maven.compiler.plugin>3.8.1</maven.compiler.plugin>
<commons.lang.version>3.4</commons.lang.version>
<systemtray.version>3.17</systemtray.version>
<lombok.version>1.18.10</lombok.version>
<lombok.version>1.18.22</lombok.version>
<httpcomponents.version>4.5.13</httpcomponents.version>
</properties>

Expand Down
10 changes: 9 additions & 1 deletion agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.plugin>3.8.1</maven.compiler.plugin>
<lombok.version>1.18.20</lombok.version>
<selenium.version>4.8.2</selenium.version>
<guava.version>31.1-jre</guava.version>
<lombok.version>1.18.22</lombok.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<aws.version>1.11.46</aws.version>
<android.tools.version>25.3.0</android.tools.version>
Expand Down Expand Up @@ -139,6 +141,12 @@
<groupId>com.android.tools.ddms</groupId>
<artifactId>ddmlib</artifactId>
<version>${android.tools.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@Log4j2
public class TestsigmaAgent {
public static void main(String[] args) {
System.setProperty("webdriver.http.factory", "jdk-http-client");
String wrapperPort = System.getProperty("agent.wrapper.port");
if (StringUtils.isNotBlank(wrapperPort)) {
WrapperConnector.getInstance().disconnectHook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.testsigma.agent.mappers.MobileDeviceMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.testsigma.automator.AutomatorConfig;
import com.testsigma.automator.drivers.DriversUpdateService;
import com.testsigma.automator.entity.Browsers;
import com.testsigma.automator.entity.OsBrowserType;
import com.testsigma.automator.exceptions.AutomatorException;
Expand Down Expand Up @@ -97,7 +96,6 @@ public void syncBrowserDrivers(MobileDevice mobileDevice) {
Browsers browser = OsBrowserType.getBrowserType(browserType);
String driverPath = AutomatorConfig.getInstance().getAppBridge().getDriverExecutablePath(browser.getKey(),
browserVersion);
new DriversUpdateService().syncBrowserDriver(browserType, browserVersion, driverPath);
} catch (AutomatorException e) {
log.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Log4j2
@Component
public class MobileAutomationServer {
private Process mobileAutomationServerProcess;
private File androidHome;
private File jreHome;

private File appiumHome;
private File mobileAutomationServerExecutablePath;
private File logFilePath;
private String serverIpAddress;
Expand All @@ -46,6 +56,10 @@ private File jreHome() {
return new File(PathUtil.getInstance().getJrePath());
}

private File appiumHome(){
return new File(PathUtil.getInstance().getMobileAutomationDriverPath());
}

private String serverIP() {
String address = "127.0.0.1";
try {
Expand All @@ -64,15 +78,16 @@ public void start() {
}
this.androidHome = androidHome();
this.jreHome = jreHome();
this.appiumHome = appiumHome();
this.mobileAutomationServerExecutablePath = new File(PathUtil.getInstance().getMobileAutomationServerPath(), "appium");
if (SystemUtils.IS_OS_WINDOWS) {
this.mobileAutomationServerExecutablePath = new File(PathUtil.getInstance().getMobileAutomationServerPath(), "appium.exe");
}
this.serverIpAddress = serverIP();
this.logFilePath = new File(PathUtil.getInstance().getLogsPath() + File.separator + "appium.log");
Integer serverPort = NetworkUtil.getFreePort();
this.serverURL = String.format("http://%s:%d/wd/hub", serverIpAddress, serverPort);

this.serverURL = String.format("http://%s:%d", serverIpAddress, serverPort);
extractMobileAutomationDrivers();
log.info("Starting Mobile Automation Server at - " + serverURL);

(new Thread(() -> {
Expand All @@ -87,6 +102,7 @@ public void start() {
"--log-timestamp",
"--allow-insecure", "chromedriver_autodownload");
processBuilder.directory(new File(PathUtil.getInstance().getMobileAutomationServerPath()));
processBuilder.environment().put("APPIUM_HOME", appiumHome.getAbsolutePath());
processBuilder.environment().put("ANDROID_HOME", androidHome.getAbsolutePath());
processBuilder.environment().put("JAVA_HOME", jreHome.getAbsolutePath());
processBuilder.redirectErrorStream(true);
Expand All @@ -103,6 +119,81 @@ public void start() {
}
}

private void extractMobileAutomationDrivers(){
log.info("Trying to check if driver folder is present inside Appium directory");
try{
if (!isDriverFolderExists()) {
log.info("Driver folder does not exits.Extracting from the zip files");
File androidDriverLocalPath = Paths.get(PathUtil.getInstance().getMobileAutomationServerPath(), "appium-uiautomator2-driver.zip").toFile();
File iOsDriverLocalPath = Paths.get(PathUtil.getInstance().getMobileAutomationServerPath(), "appium-xcuitest-driver.zip").toFile();
File destinationPath = new File(PathUtil.getInstance().getMobileAutomationDriverDestinationPath());
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<?> unzipAndroidDriver = executor.submit(() -> {
try {
unzipDriver(androidDriverLocalPath,destinationPath);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
});
Future<?> unzipIosDriver = executor.submit(() -> {
try {
unzipDriver(iOsDriverLocalPath,destinationPath);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
});
try {
// Wait for the tasks to complete within the timeout duration
int timeoutSeconds = 120;
unzipAndroidDriver.get(timeoutSeconds, TimeUnit.SECONDS);
unzipIosDriver.get(timeoutSeconds, TimeUnit.SECONDS);
log.info("Unzip completed successfully");
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
executor.shutdown();
}
}
} catch (Exception e){
log.error(e.getMessage(), e);
}
}

private boolean isDriverFolderExists() {
String dirPath = appiumHome.getAbsolutePath();
log.info("Verifying if driver folder exists: " + dirPath);
File driverDirectoryFile = new File(dirPath);
if (driverDirectoryFile.exists()) {
File driverFile = new File(driverDirectoryFile.getAbsolutePath());
return driverFile.exists() && driverFile.isDirectory();
}
return false;
}

private void unzipDriver(File sourcePath,File destinationPath) throws IOException {
File fileZip = new File(String.valueOf(sourcePath));
File destDir = new File(String.valueOf(destinationPath));

try (ZipFile zipFile = new ZipFile(fileZip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();

while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
String entryName = entry.getName();
// to skip creating the "_MACOSX" directory
if (!entryName.startsWith("__MACOSX")) {
File entryFile = new File(destDir, entryName);
Files.createDirectories(entryFile.getParentFile().toPath());
Files.copy(zipFile.getInputStream(entry), entryFile.toPath());
}
}
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}
}

@PreDestroy
public void stop() {
log.info("Stopping Mobile Automation Server...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class AndroidMobileAutomationServer {
private final DeviceContainer deviceContainer;
private final CommandExecutor commandExecutor;

private final String UI_AUTOMATOR2_SERVER_TEST_APK = "appium-uiautomator2-server-test.apk";
private final String UI_AUTOMATOR2_SERVER_APK = "appium-uiautomator2-server.apk";
private final String UI_AUTOMATOR2_SERVER_TEST_APK = "appium-uiautomator2-server-debug-androidTest.apk";
private final String UI_AUTOMATOR2_SERVER_APK = "appium-uiautomator2-server-v5.8.2.apk";
private final String APPIUM_SETTINGS_APK = "settings_apk-debug.apk";
private final String UI_AUTOMATOR2_PACKAGE = "io.appium.uiautomator2.server";
private final String UI_AUTOMATOR2_TEST_PACKAGE = "io.appium.uiautomator2.server.test";
Expand All @@ -38,7 +38,6 @@ public void installDrivers(String uniqueId) throws MobileLibraryInstallException
try {
MobileDevice mobileDevice = deviceContainer.getDevice(uniqueId);
IDevice device = mobileDevice.getIDevice();

this.installAppiumSettings(device);
this.startAppiumSettings(device);
this.installUIAutomatorServer(device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void createSession(String uniqueId) throws MobileAutomationServerSessionE
desiredCapabilities.setCapability("xcodeOrgId", "6F4CKCA4LX");
desiredCapabilities.setCapability("xcodeSigningId", "iPhone Developer");
desiredCapabilities.setCapability("app", "/Users/vikram/ios-apps/ios-test-app/build/Release-iphoneos/TestApp-iphoneos.app");
device.setRemoteWebDriver(new IOSDriver<>(new URL(mobileAutomationServerService.getMobileAutomationServer().getServerURL()),
device.setRemoteWebDriver(new IOSDriver(new URL(mobileAutomationServerService.getMobileAutomationServer().getServerURL()),
desiredCapabilities));
} catch (Exception e) {
throw new MobileAutomationServerSessionException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void startWdaOnDevice(MobileDevice device) throws TestsigmaException {

private void checkWDAProcessStatus(MobileDevice device) throws TestsigmaException, AutomatorException, InterruptedException {
int retries = 3;
while (device.getWdaProcess() == null && (device.getWdaProcess().isAlive() || device.getWdaProcess().exitValue() == 0) && retries-- > 0) {
while (device.getWdaProcess() == null && retries-- > 0) {
log.info("WDA process not started yet, waiting for 5 seconds...");
Thread.sleep(5000);
}
Expand All @@ -161,7 +161,7 @@ private void checkWDAProcessStatus(MobileDevice device) throws TestsigmaExceptio

private void checkWDARelayProcessStatus(MobileDevice device) throws TestsigmaException, AutomatorException, InterruptedException {
int retries = 3;
while (device.getWdaRelayProcess() == null && !device.getWdaRelayProcess().isAlive() && retries-- > 0) {
while (device.getWdaRelayProcess() == null && retries-- > 0) {
log.info("WDA process not started yet, waiting for 5 seconds...");
Thread.sleep(5000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,40 +136,22 @@ private void setRemoteServerURL(TestsigmaDriver testsigmaDriver, DriverSessionRe
}

private void handleLocalDevice(List<WebDriverCapability> caps, DriverSessionRequest driverSessionRequest)
throws TestsigmaException, AutomatorException {
throws TestsigmaException, AutomatorException {
if (driverSessionRequest.getExecutionLabType().equals(ExecutionLabType.Hybrid)) {
appendChromeDriverExecutable(caps, driverSessionRequest);
if (driverSessionRequest.getWorkspaceType() == WorkspaceType.IOSNative) {
setupIosDevice(caps, driverSessionRequest);
}
}
}

private void appendChromeDriverExecutable(List<WebDriverCapability> caps, DriverSessionRequest driverSessionRequest)
throws TestsigmaException {
MobileDevice device = deviceContainer.getDevice(driverSessionRequest.getUniqueId());
if (device.getBrowserList() != null && device.getBrowserList().size() > 0) {
AgentBrowser browser = device.getBrowserList().get(0);
File chromePath = driverExecutableExists(Browsers.GoogleChrome.getKey(),
browser.getMajorVersion() + "");
if (chromePath != null) {
WebDriverCapability cap = new WebDriverCapability(TSCapabilityType.CHROME_DRIVER_EXECUTABLE, chromePath.getAbsolutePath());
caps.add(cap);
} else {
log.warn("Chrome Driver is not yet downloaded.. please try after some time");
}
}
}

public void setupIosDevice(List<WebDriverCapability> caps, DriverSessionRequest driverSessionRequest)
throws TestsigmaException, AutomatorException {
throws TestsigmaException, AutomatorException {
MobileDevice device = deviceContainer.getDevice(driverSessionRequest.getUniqueId());
iosDeviceService.setupWda(device);
WebDriverCapability bundleIdCapability = caps.stream().filter(cap -> cap.getCapabilityName()
.equals(TSCapabilityType.BUNDLE_ID)).findFirst().orElse(null);
.equals(TSCapabilityType.BUNDLE_ID)).findFirst().orElse(null);
if ((bundleIdCapability == null) || StringUtils.isBlank((String) bundleIdCapability.getCapabilityValue())) {
WebDriverCapability appCapability = caps.stream().filter(cap -> cap.getCapabilityName()
.equals(MobileCapabilityType.APP)).findFirst().orElse(null);
.equals(MobileCapabilityType.APP)).findFirst().orElse(null);
AppPathType appPathType = driverSessionRequest.getApplicationPathType();
if ((appCapability != null) && appPathType != AppPathType.APP_DETAILS) {
caps.remove(appCapability);
Expand Down
6 changes: 6 additions & 0 deletions agent/src/main/java/com/testsigma/agent/utils/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class PathUtil {
@Getter
private String mobileAutomationServerPath = null;
@Getter
private String mobileAutomationDriverPath = null;
@Getter
private String mobileAutomationDriverDestinationPath = null;
@Getter
private String upgradePath = null;
@Getter
private String rootPath = null;
Expand Down Expand Up @@ -79,6 +83,8 @@ public void setPathsFromContext(boolean reset) {
driversPath = rootPath + File.separator + "drivers";
jrePath = rootPath + File.separator + "jre";
mobileAutomationServerPath = rootPath + File.separator + "appium";
mobileAutomationDriverPath = mobileAutomationServerPath + File.separator +"drivers";
mobileAutomationDriverDestinationPath = mobileAutomationDriverPath + File.separator + "node_modules";
androidPath = rootPath + File.separator + "android";
iosPath = rootPath + File.separator + "ios";

Expand Down
2 changes: 1 addition & 1 deletion agent/src/main/resources/agent.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
cloud.url=${CLOUD_URL:https://local.testsigmaos.com}
local.server.url=${LOCAL_SERVER_URL:http://localhost:9090}
local.agent.register=${LOCAL_AGENT_REGISTER:true}
agent.version=2.9.1
agent.version=3.0.0
Loading

0 comments on commit 842c2b8

Please sign in to comment.