All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.testvagrant.optimus.core.mobile.ServerManager Maven / Gradle / Ivy

Go to download

Optimus Lite API to manage test devices and create appium driver based on platform

There is a newer version: 0.1.7-beta
Show newest version
package com.testvagrant.optimus.core.mobile;

import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.AndroidServerFlag;
import io.appium.java_client.service.local.flags.ServerArgument;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Platform;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE;
import static org.awaitility.Awaitility.await;

abstract class ServerManager {

  protected final ThreadLocal appiumDriverLocalServiceThreadLocal;

  ServerManager() {
    this.appiumDriverLocalServiceThreadLocal = new ThreadLocal<>();
  }

  AppiumDriverLocalService startService(Map serverArguments,
                                        String udid, String browserName) {
    try {
      boolean enableLogs = serverArguments.containsKey(OptimusServerFlag.ENABLE_CONSOLE_LOGS);

      AppiumDriverLocalService appiumService = buildAppiumService(udid, serverArguments, browserName);
      if (!enableLogs) appiumService.clearOutPutStreams();

      appiumService.start();
      await().atMost(5, TimeUnit.SECONDS).until(appiumService::isRunning);
      appiumDriverLocalServiceThreadLocal.set(appiumService);
      return appiumDriverLocalServiceThreadLocal.get();
    } catch (Exception ex) {
      throw new RuntimeException("Unable to start Appium service.\nError:" + ex.getMessage());
    }
  }

  private AppiumDriverLocalService buildAppiumService(
          String udid, Map serverArguments, String browserName) {
    File logFile =
        new File(
            String.format(
                "logs/appium" + File.separator + "appium_%s.log",
                udid.replaceAll("-", "_").trim()));

    AppiumServiceBuilder appiumServiceBuilder =
        new AppiumServiceBuilder()
            .withArgument(SESSION_OVERRIDE)
            .usingAnyFreePort()
            .withLogFile(logFile)
            .withArgument(
                AndroidServerFlag.BOOTSTRAP_PORT_NUMBER,
                String.valueOf(randomOpenPortOnAllLocalInterfaces()))
            .withArgument(
                OptimusServerFlag.WDA_PORT, String.valueOf(randomOpenPortOnAllLocalInterfaces()));

    Map updatedServerArguments =
        ignoreOptimusServerArguments(serverArguments);

    updatedServerArguments.forEach(
        (argument, value) -> {
          if (value.isEmpty()) {
            appiumServiceBuilder.withArgument(argument);
          } else {
            appiumServiceBuilder.withArgument(argument, value);
          }
        });

    addMobileWebDriverPath(browserName, appiumServiceBuilder);

    return AppiumDriverLocalService.buildService(appiumServiceBuilder);
  }

  private void addMobileWebDriverPath(String browserName, AppiumServiceBuilder appiumServiceBuilder) {
    switch (browserName.toLowerCase()) {
      case "chrome":
        WebDriverManager webDriverManager = WebDriverManager.chromedriver();
         webDriverManager.setup();
         appiumServiceBuilder.withArgument(AndroidServerFlag.CHROME_DRIVER_EXECUTABLE, webDriverManager.getBinaryPath());
      break;
      default:
        break;
    }
  }

  private Integer randomOpenPortOnAllLocalInterfaces() {
    try (ServerSocket socket = new ServerSocket(0)) {
      return socket.getLocalPort();
    } catch (IOException e) {
      throw new RuntimeException("No open ports available to start Appium service");
    }
  }

  private Map ignoreOptimusServerArguments(
      Map serverArguments) {
    ServerArgument[] serverArgArray = {
      SESSION_OVERRIDE,
      AndroidServerFlag.BOOTSTRAP_PORT_NUMBER,
      OptimusServerFlag.WDA_PORT,
      OptimusServerFlag.ENABLE_CONSOLE_LOGS
    };
    Arrays.stream(serverArgArray).forEach(serverArguments::remove);
    return serverArguments;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy