Skip to content

Commit

Permalink
Appium Tests (#92)
Browse files Browse the repository at this point in the history
Added Appium Android Support
  • Loading branch information
RameshBabuPrudhvi authored Apr 22, 2022
1 parent 5046690 commit 3b0d5ab
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,35 @@
package io.github.selcukes.core.driver;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;
import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.commons.exception.DriverSetupException;
import io.github.selcukes.core.enums.DriverType;
import io.github.selcukes.commons.helper.FileHelper;
import lombok.CustomLog;
import lombok.SneakyThrows;
import org.openqa.selenium.Capabilities;

import java.net.URL;

@CustomLog
public class MobileManager implements RemoteManager {
public class AppiumManager implements RemoteManager {
AppiumDriver driver;
AppiumDriverLocalService service;

@Override
public AppiumDriver createDriver() {
String browser = ConfigFactory.getConfig().getWeb().get("browserName");

if (null == driver) {
try {
logger.info(() -> "Initiating New Browser Session...");
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.usingPort(4723)
.build();
service.start();

MobileOptions browserOptions = new MobileOptions();
Capabilities capabilities = browserOptions.getMobileOptions(DriverType.valueOf(browser));
driver = new AppiumDriver(getServiceUrl(), capabilities);
logger.info(() -> "Initiating New Mobile Session...");
startAppiumService();
Capabilities capabilities = DesktopOptions.getUserOptions();
if (capabilities == null) {
String app = FileHelper.loadThreadResource(ConfigFactory.getConfig()
.getMobile().get("app")).getAbsolutePath();
capabilities = DesktopOptions.getAndroidOptions(app);
}
driver = new AndroidDriver(service.getUrl(), capabilities);
} catch (Exception e) {
throw new DriverSetupException("Driver was not setup properly.", e);
}
Expand All @@ -60,15 +58,22 @@ public void destroyDriver() {
if (driver != null) {
driver.quit();
}
if (service != null) {
service.stop();
}
stopAppiumService();
}

@SneakyThrows
@Override
public URL getServiceUrl() {
String serviceUrl = ConfigFactory.getConfig().getMobile().get("serviceUrl");
return new URL(serviceUrl);
public void startAppiumService() {
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.usingAnyFreePort()
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withArgument(GeneralServerFlag.BASEPATH, "/wd/")
.build();
logger.info(() -> "Starting Appium server...");
service.start();
}

public void stopAppiumService() {
if (service != null)
service.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.github.selcukes.core.driver;

import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.core.enums.DriverType;
import io.github.selcukes.wdb.WebDriverBinary;
import org.openqa.selenium.Capabilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
import io.appium.java_client.windows.WindowsDriver;
import io.github.selcukes.commons.config.ConfigFactory;
import lombok.CustomLog;
import lombok.SneakyThrows;

import java.net.URL;
import java.util.Objects;

@CustomLog
public class DesktopManager implements RemoteManager {

public class DesktopManager extends AppiumManager {
private WindowsDriver windowsDriver;
private Process winProcess;

@Override
public synchronized WindowsDriver createDriver() {
if (null == windowsDriver) {
startWinAppDriver();
startAppiumService();
String app = ConfigFactory.getConfig().getWindows().get("app");
windowsDriver = new WindowsDriver(Objects.requireNonNull(getServiceUrl()),
DesktopOptions.setCapabilities(app));
URL serviceUrl = Objects.requireNonNull(service.getUrl());
DesktopOptions.setServiceUrl(serviceUrl);
windowsDriver = new WindowsDriver(serviceUrl, DesktopOptions.getWinAppOptions(app));
}
return windowsDriver;
}
Expand All @@ -44,26 +43,7 @@ public void destroyDriver() {
if (windowsDriver != null) {
windowsDriver.closeApp();
}
killWinAppDriver();
}

@SneakyThrows
private void startWinAppDriver() {
ProcessBuilder processBuilder = new ProcessBuilder(ConfigFactory.getConfig().getWindows().get("winApp-path"));
processBuilder.inheritIO();
winProcess = processBuilder.start();
logger.info(() -> "WinAppDriver started...");
stopAppiumService();
}

private void killWinAppDriver() {
winProcess.destroy();
logger.info(() -> "WinAppDriver killed...");
}

@SneakyThrows
@Override
public URL getServiceUrl() {
String serviceUrl = ConfigFactory.getConfig().getWindows().get("serviceUrl");
return new URL(serviceUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,51 @@

package io.github.selcukes.core.driver;

import io.github.selcukes.commons.config.ConfigFactory;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;

import java.net.URL;

import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import java.util.Map;

@UtilityClass
public class DesktopOptions {
URL serviceUrl;
Capabilities caps;

@SneakyThrows
public static URL getServiceUrl() {
String serviceUrl = ConfigFactory.getConfig().getWindows().get("serviceUrl");
return new URL(serviceUrl);
public URL getServiceUrl() {
return serviceUrl;
}

public static Capabilities setAppTopLevelWindow(String windowId) {
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("appTopLevelWindow", windowId);
return capabilities;
public void setServiceUrl(URL url) {
serviceUrl = url;
}

public Capabilities setAppTopLevelWindow(String windowId) {
return setCapability("appTopLevelWindow", windowId);
}

public MutableCapabilities getWinAppOptions(String app) {
return new MutableCapabilities(Map.of("platformName", "Windows",
"deviceName", "WindowsPC", "app", app));
}

public static MutableCapabilities setCapabilities(String app) {
public MutableCapabilities getAndroidOptions(String app) {
return setCapability("app", app);
}

public MutableCapabilities setCapability(String capabilityName, String value) {
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability(PLATFORM_NAME, "Windows");
capabilities.setCapability("deviceName", "WindowsPC");
capabilities.setCapability("app", app);
capabilities.setCapability(capabilityName, value);
return capabilities;
}

public void setUserOptions(Capabilities capabilities) {
caps = capabilities;
}

public Capabilities getUserOptions() {
return caps;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public final class DriverFactory<D extends RemoteWebDriver> {

Expand All @@ -28,7 +29,8 @@ public final class DriverFactory<D extends RemoteWebDriver> {
private static final List<Object> STORED_DRIVER = new ArrayList<>();

static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> STORED_DRIVER.forEach(d -> ((RemoteWebDriver) d).quit())));
Runtime.getRuntime().addShutdownHook(new Thread(() -> STORED_DRIVER.stream()
.filter(Objects::nonNull).forEach(d -> ((RemoteWebDriver) d).quit())));
}

private DriverFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
import io.github.selcukes.commons.logging.Logger;
import io.github.selcukes.commons.logging.LoggerFactory;
import io.github.selcukes.core.enums.DeviceType;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.Arrays;


public class DriverManager<D extends RemoteWebDriver> {
private final Logger logger = LoggerFactory.getLogger(DriverManager.class);
private RemoteManager remoteManager;

public D createDriver(DeviceType deviceType) {

public D createDriver(DeviceType deviceType, Capabilities... capabilities) {
Arrays.stream(capabilities).findAny().ifPresent(DesktopOptions::setUserOptions);
if (DriverFactory.getDriver() == null) {
logger.info(() -> "Creating new session..." + deviceType);
logger.info(() -> String.format("Creating new %s session...", deviceType));
switch (deviceType) {
case BROWSER:
remoteManager = new WebManager();
Expand All @@ -41,7 +44,7 @@ public D createDriver(DeviceType deviceType) {
DriverFactory.setDriver(remoteManager.createDriver());
break;
case MOBILE:
remoteManager = new MobileManager();
remoteManager = new AppiumManager();
DriverFactory.setDriver(remoteManager.createDriver());
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

package io.github.selcukes.core.driver;

import java.net.URL;

public interface RemoteManager {
Object createDriver();

void destroyDriver();

URL getServiceUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public synchronized WebDriver createDriver() {
if (null == driver) {
try {
logger.info(() -> "Initiating New Browser Session...");

BrowserOptions browserOptions = new BrowserOptions();
Capabilities capabilities = browserOptions.getBrowserOptions(DriverType.valueOf(browser));

Capabilities capabilities = DesktopOptions.getUserOptions();
if (capabilities == null) {
BrowserOptions browserOptions = new BrowserOptions();
capabilities = browserOptions.getBrowserOptions(DriverType.valueOf(browser));
}
RemoteWebDriverBuilder webDriverBuilder = RemoteWebDriver.builder().oneOf(capabilities);
if (ConfigFactory.getConfig().getWeb().get("remote").equalsIgnoreCase("true")) {
Main.main(new String[]{"standalone", "--port", "4444"});
Expand All @@ -63,7 +64,6 @@ public void destroyDriver() {
}

@SneakyThrows
@Override
public URL getServiceUrl() {
String serviceUrl = ConfigFactory.getConfig().getWeb().get("serviceUrl");
return new URL(serviceUrl);
Expand Down
Loading

0 comments on commit 3b0d5ab

Please sign in to comment.