com.appium.manager.AppiumDriverManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AppiumTestDistribution Show documentation
Show all versions of AppiumTestDistribution Show documentation
A tool run Android and iOS test in parallel across devices
package com.appium.manager;
import com.appium.entities.MobilePlatform;
import com.appium.ios.IOSDeviceConfiguration;
import com.appium.utils.CommandPrompt;
import com.appium.utils.ConfigFileManager;
import com.appium.capabilities.DesiredCapabilityBuilder;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;
public class AppiumDriverManager {
private static ThreadLocal appiumDriver
= new ThreadLocal<>();
private DesiredCapabilityBuilder desiredCapabilityBuilder;
private ConfigFileManager prop;
private static final Logger LOGGER = Logger.getLogger(Class.class.getName());
public AppiumDriverManager() {
desiredCapabilityBuilder = new DesiredCapabilityBuilder();
prop = ConfigFileManager.getInstance();
}
public static AppiumDriver getDriver() {
return appiumDriver.get();
}
protected static void setDriver(AppiumDriver driver) {
appiumDriver.set(driver);
}
private AppiumDriver initialiseDriver(
Optional capabilities)
throws Exception {
AppiumDriver currentDriverSession;
DesiredCapabilities desiredCapabilities = capabilities.get();
LOGGER.info("Capabilities: " + desiredCapabilities.toString());
String remoteWDHubIP = getRemoteWDHubIP();
if (!AppiumDeviceManager.getAppiumDevice().getDevice().isCloud()
&& AppiumDeviceManager.getMobilePlatform().name().equalsIgnoreCase("iOS")) {
currentDriverSession = new IOSDriver(new URL(remoteWDHubIP),
desiredCapabilities);
LOGGER.info("Session Created for iOS ---- "
+ currentDriverSession.getSessionId() + "---"
+ currentDriverSession.getSessionDetail("udid"));
} else if (!AppiumDeviceManager.getAppiumDevice().getDevice().isCloud()
&& AppiumDeviceManager.getMobilePlatform().name().equalsIgnoreCase("android")) {
currentDriverSession = new AndroidDriver(new URL(remoteWDHubIP),
desiredCapabilities);
LOGGER.info("Session Created for Android ---- "
+ currentDriverSession.getSessionId() + "---"
+ currentDriverSession.getSessionDetail("udid"));
} else {
currentDriverSession = new AppiumDriver<>(new URL(remoteWDHubIP),
desiredCapabilities);
LOGGER.info("Session Created ---- "
+ currentDriverSession.getSessionId() + "---"
+ currentDriverSession.getRemoteAddress().getHost() + "---"
+ currentDriverSession.getSessionDetail("udid"));
}
return currentDriverSession;
}
private String getRemoteWDHubIP() throws Exception {
String hostName = AppiumDeviceManager.getAppiumDevice().getHostName();
IAppiumManager appiumManager = AppiumManagerFactory.getAppiumManager(hostName);
return appiumManager.getRemoteWDHubIP(hostName);
}
private void startAppiumDriverInstance(Optional desiredCapabilities)
throws Exception {
AppiumDriver currentDriverSession;
currentDriverSession = initialiseDriver(desiredCapabilities);
AppiumDriverManager.setDriver(currentDriverSession);
}
// Should be used by Cucumber as well
public void startAppiumDriverInstance() throws Exception {
String userSpecifiedCaps;
DesiredCapabilities desiredCapabilities;
userSpecifiedCaps = getCapsPath();
desiredCapabilities = buildDesiredCapabilities(userSpecifiedCaps);
startAppiumDriverInstance(Optional.ofNullable(desiredCapabilities));
}
private String getCapsPath() {
if (prop.getProperty("CAPS") == null) {
return System.getProperty("user.dir")
+ "/caps/capabilities.json";
} else {
return prop.getProperty("CAPS");
}
}
private DesiredCapabilities buildDesiredCapabilities(String capabilityPath)
throws Exception {
String capabilities = capabilityPath;
if (new File(capabilityPath).exists()) {
Path path = FileSystems.getDefault().getPath(capabilityPath);
if (!path.getParent().isAbsolute()) {
capabilities = path.normalize()
.toAbsolutePath().toString();
}
desiredCapabilityBuilder
.buildDesiredCapability(capabilities);
return DesiredCapabilityBuilder.getDesiredCapability();
} else {
throw new RuntimeException("Capability file not found");
}
}
protected void stopAppiumDriver() throws Exception {
String OS = System.getProperty("os.name").toLowerCase();
String command;
if (AppiumDeviceManager.getAppiumDevice().getDevice().getUdid().length()
== IOSDeviceConfiguration.IOS_UDID_LENGTH) {
String hostName = AppiumDeviceManager.getAppiumDevice().getHostName();
AppiumManagerFactory.getAppiumManager(hostName).destoryIOSWebKitProxy(hostName);
}
if (AppiumDeviceManager.getAppiumDevice().getChromeDriverPort() > 0) {
if (OS.contains("mac")) {
command = "kill -9 $(lsof -ti tcp:"
+ AppiumDeviceManager.getAppiumDevice().getChromeDriverPort() + ")";
new CommandPrompt().runCommand(command);
}
AppiumDeviceManager.getAppiumDevice().setChromeDriverPort(0);
}
if (AppiumDriverManager.getDriver() != null
&& AppiumDriverManager.getDriver().getSessionId() != null) {
LOGGER.info("Session Deleting ---- "
+ AppiumDriverManager.getDriver().getSessionId() + "---"
+ AppiumDriverManager.getDriver().getSessionDetail("udid"));
AppiumDriverManager.getDriver().quit();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy