Search in sources :

Example 1 with EventFiringAppiumCommandExecutor

use of com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringAppiumCommandExecutor in project carina by qaprosoft.

the class MobileFactory method create.

@Override
public WebDriver create(String name, DesiredCapabilities capabilities, String seleniumHost) {
    if (seleniumHost == null) {
        seleniumHost = Configuration.getSeleniumUrl();
    }
    String mobilePlatformName = Configuration.getPlatform(capabilities);
    // TODO: refactor to be able to remove SpecialKeywords.CUSTOM property completely
    // use comparison for custom_capabilities here to localize as possible usage of CUSTOM attribute
    String customCapabilities = Configuration.get(Parameter.CUSTOM_CAPABILITIES);
    if (!customCapabilities.isEmpty() && (customCapabilities.toLowerCase().contains("localhost") || customCapabilities.toLowerCase().contains("browserstack") || customCapabilities.toLowerCase().contains("saucelabs"))) {
        mobilePlatformName = SpecialKeywords.CUSTOM;
    }
    LOGGER.debug("selenium: " + seleniumHost);
    RemoteWebDriver driver = null;
    // if inside capabilities only singly "udid" capability then generate default one and append udid
    if (isCapabilitiesEmpty(capabilities)) {
        capabilities = getCapabilities(name);
    } else if (capabilities.asMap().size() == 1 && capabilities.getCapability("udid") != null) {
        String udid = capabilities.getCapability("udid").toString();
        capabilities = getCapabilities(name);
        capabilities.setCapability("udid", udid);
        LOGGER.debug("Appended udid to cpabilities: " + capabilities);
    }
    LOGGER.debug("capabilities: " + capabilities);
    try {
        EventFiringAppiumCommandExecutor ce = new EventFiringAppiumCommandExecutor(new URL(seleniumHost));
        if (mobilePlatformName.equalsIgnoreCase(SpecialKeywords.ANDROID)) {
            driver = new AndroidDriver<AndroidElement>(ce, capabilities);
        } else if (mobilePlatformName.equalsIgnoreCase(SpecialKeywords.IOS) || mobilePlatformName.equalsIgnoreCase(SpecialKeywords.TVOS)) {
            driver = new IOSDriver<IOSElement>(ce, capabilities);
        } else if (mobilePlatformName.equalsIgnoreCase(SpecialKeywords.CUSTOM)) {
            // that's a case for custom mobile capabilities like browserstack or saucelabs
            driver = new RemoteWebDriver(new URL(seleniumHost), capabilities);
        } else {
            throw new RuntimeException("Unsupported mobile platform: " + mobilePlatformName);
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException("Malformed selenium URL!", e);
    } catch (Exception e) {
        Device device = IDriverPool.nullDevice;
        LOGGER.debug("STF is enabled. Debug info will be extracted from the exception.");
        if (e != null) {
            String debugInfo = getDebugInfo(e.getMessage());
            if (!debugInfo.isEmpty()) {
                String udid = getUdidFromDebugInfo(debugInfo);
                String deviceName = getParamFromDebugInfo(debugInfo, "name");
                device = new Device();
                device.setUdid(udid);
                device.setName(deviceName);
                IDriverPool.registerDevice(device);
            }
        // there is no sense to register device in the pool as driver is not started and we don't have custom exception from MCloud
        }
        throw e;
    }
    try {
        Device device = new Device(driver.getCapabilities());
        IDriverPool.registerDevice(device);
        // will be performed just in case uninstall_related_apps flag marked as true
        device.uninstallRelatedApps();
    } catch (Exception e) {
        // use-case when something wrong happen during initialization and registration device information.
        // the most common problem might be due to the adb connection problem
        // make sure to initiate driver quit
        LOGGER.error("Unable to register device!", e);
        // TODO: try to handle use-case if quit in this place can hangs for minutes!
        LOGGER.error("starting driver quit...");
        driver.quit();
        LOGGER.error("finished driver quit...");
        throw e;
    }
    return driver;
}
Also used : EventFiringAppiumCommandExecutor(com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringAppiumCommandExecutor) IOSDriver(io.appium.java_client.ios.IOSDriver) MalformedURLException(java.net.MalformedURLException) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device) AndroidElement(io.appium.java_client.android.AndroidElement) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException)

Aggregations

Device (com.qaprosoft.carina.core.foundation.webdriver.device.Device)1 EventFiringAppiumCommandExecutor (com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringAppiumCommandExecutor)1 AndroidElement (io.appium.java_client.android.AndroidElement)1 IOSDriver (io.appium.java_client.ios.IOSDriver)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1