Search in sources :

Example 1 with Device

use of com.qaprosoft.carina.core.foundation.webdriver.device.Device in project carina by qaprosoft.

the class MobileFactory method create.

@Override
public WebDriver create(String name, Device device, DesiredCapabilities capabilities, String seleniumHost) {
    if (seleniumHost == null) {
        seleniumHost = Configuration.get(Configuration.Parameter.SELENIUM_HOST);
    }
    String driverType = Configuration.getDriverType();
    String mobilePlatformName = Configuration.getPlatform();
    // TODO: refactor code 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()) {
        mobilePlatformName = SpecialKeywords.CUSTOM;
    }
    LOGGER.debug("selenium: " + seleniumHost);
    RemoteWebDriver driver = null;
    if (isCapabilitiesEmpty(capabilities)) {
        capabilities = getCapabilities(name, device);
    }
    try {
        if (driverType.equalsIgnoreCase(SpecialKeywords.MOBILE)) {
            if (mobilePlatformName.toLowerCase().equalsIgnoreCase(SpecialKeywords.ANDROID)) {
                driver = new AndroidDriver<AndroidElement>(new URL(seleniumHost), capabilities);
            } else if (mobilePlatformName.toLowerCase().equalsIgnoreCase(SpecialKeywords.IOS)) {
                driver = new IOSDriver<IOSElement>(new URL(seleniumHost), capabilities);
            } else if (mobilePlatformName.toLowerCase().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 capabilities for type: " + driverType + " platform: " + mobilePlatformName);
            }
        }
        if (device.isNull()) {
            // TODO: double check that local run with direct appium works fine
            RemoteDevice remoteDevice = getDeviceInfo(seleniumHost, driver.getSessionId().toString());
            // 3rd party solutions like browserstack or saucelabs return not null remoteDevice object. But inside nothing useful
            if (remoteDevice != null && remoteDevice.getName() != null) {
                device = new Device(remoteDevice);
            } else {
                device = new Device(driver.getCapabilities());
            }
            boolean stfEnabled = R.CONFIG.getBoolean(SpecialKeywords.CAPABILITIES + "." + SpecialKeywords.STF_ENABLED);
            if (stfEnabled) {
                device.connectRemote();
            }
            DevicePool.registerDevice(device);
        }
        // will be performed just in case uninstall_related_apps flag marked as true
        device.uninstallRelatedApps();
    } catch (MalformedURLException e) {
        LOGGER.error("Malformed selenium URL! " + e.getMessage(), e);
    }
    if (driver == null) {
        Assert.fail("Unable to initialize driver: " + name + "!");
    }
    return driver;
}
Also used : IOSDriver(io.appium.java_client.ios.IOSDriver) MalformedURLException(java.net.MalformedURLException) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) RemoteDevice(com.qaprosoft.carina.commons.models.RemoteDevice) Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device) AndroidElement(io.appium.java_client.android.AndroidElement) RemoteDevice(com.qaprosoft.carina.commons.models.RemoteDevice) URL(java.net.URL)

Example 2 with Device

use of com.qaprosoft.carina.core.foundation.webdriver.device.Device in project carina by qaprosoft.

the class DriverPool method quitDriver.

/**
 * Quit driver by name
 *
 * @param name
 *            String driver name
 */
public static void quitDriver(String name) {
    WebDriver drv = getDriver(name);
    // TODO: try to understand valid place for recorded video file to support method/class and suite mode
    // 1. create for each driver their own video file.
    // 2. save it using unique driver/test/thread name - maybe time in ms
    // 3. register link onto the video as test artifact
    Device device = DevicePool.getDevice();
    if (!device.isNull()) {
        device.screenOff();
    }
    try {
        LOGGER.debug("Driver exiting..." + name);
        deregisterDriver(name);
        DevicePool.deregisterDevice();
        drv.quit();
        LOGGER.debug("Driver exited..." + name);
    } catch (WebDriverException e) {
        LOGGER.debug("Error message detected during driver verification: " + e.getMessage(), e);
    // do nothing
    } catch (Exception e) {
        LOGGER.debug("Error discovered during driver quit: " + e.getMessage(), e);
        // TODO: it seems like BROWSER_TIMEOUT or NODE_FORWARDING should be handled here as well
        if (!e.getMessage().contains("Session ID is null.")) {
            throw e;
        }
    } finally {
        // TODO analyze how to forcibly kill session on device
        NDC.pop();
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device) WebDriverException(org.openqa.selenium.WebDriverException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 3 with Device

use of com.qaprosoft.carina.core.foundation.webdriver.device.Device in project carina by qaprosoft.

the class AbstractTest method getDeviceName.

// TODO: remove this private method
private String getDeviceName() {
    String deviceName = "Desktop";
    if (!DevicePool.getDevice().isNull()) {
        // Samsung - Android 4.4.2; iPhone - iOS 7
        Device device = DevicePool.getDevice();
        String deviceTemplate = "%s - %s %s";
        deviceName = String.format(deviceTemplate, device.getName(), device.getOs(), device.getOsVersion());
    }
    return deviceName;
}
Also used : Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device)

Example 4 with Device

use of com.qaprosoft.carina.core.foundation.webdriver.device.Device in project carina by qaprosoft.

the class ZafiraConfigurator method getConfiguration.

@Override
public ConfigurationType getConfiguration() {
    ConfigurationType conf = new ConfigurationType();
    for (Parameter parameter : Parameter.values()) {
        conf.getArg().add(buildArgumentType(parameter.getKey(), R.CONFIG.get(parameter.getKey())));
    }
    if (buildArgumentType("platform", R.CONFIG.get("os")).getValue() != null) {
        // TODO: review and fix for 5.2.2.xx implementation
        // add custom arguments from browserStack
        conf.getArg().add(buildArgumentType("platform", R.CONFIG.get("os")));
        conf.getArg().add(buildArgumentType("platform_version", R.CONFIG.get("os_version")));
    }
    long threadId = Thread.currentThread().getId();
    // add custom arguments from current mobile device
    Device device = DevicePool.getDevice();
    if (!device.getName().isEmpty()) {
        String deviceName = device.getName();
        String deviceOs = device.getOs();
        String deviceOsVersion = device.getOsVersion();
        conf.getArg().add(buildArgumentType("device", deviceName));
        conf.getArg().add(buildArgumentType("platform", deviceOs));
        conf.getArg().add(buildArgumentType("platform_version", deviceOsVersion));
        LOGGER.debug("Detected device: '" + deviceName + "'; os: '" + deviceOs + "'; os version: '" + deviceOsVersion + "'");
    } else {
        LOGGER.debug("Unable to detect current device for threadId: " + threadId);
    }
    return conf;
}
Also used : ConfigurationType(com.qaprosoft.zafira.models.dto.config.ConfigurationType) Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device) Parameter(com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter)

Example 5 with Device

use of com.qaprosoft.carina.core.foundation.webdriver.device.Device in project carina by qaprosoft.

the class CustomTypePageFactory method initPage.

public static <T extends AbstractPage> T initPage(WebDriver driver, Class<T> parentClass, Object... parameters) {
    if (driver == null) {
        LOGGER.error("Page isn't created. There is no any initialized driver for thread: " + Thread.currentThread().getId());
        throw new RuntimeException("Page isn't created. Driver isn't initialized.");
    }
    Set<Class<? extends T>> setClasses = reflections.getSubTypesOf(parentClass);
    LOGGER.debug("Relatives classes count:" + setClasses.size());
    Class<? extends T> versionClass = null, majorVersionClass = null, deviceClass = null, familyClass = null, requiredClass = null;
    Type screenType = DevicePool.getDevice().getDeviceType();
    Device device = DevicePool.getDevice();
    // default version in case if it is desktop driver
    String deviceVersion = "1";
    if (!device.getOsVersion().isEmpty()) {
        deviceVersion = device.getOsVersion();
    }
    String majorVersionNumber = deviceVersion.split(VERSION_SPLITTER)[0];
    LOGGER.debug("Major version of device OS: " + majorVersionNumber);
    for (Class<? extends T> clazz : setClasses) {
        if (clazz.getAnnotation(DeviceType.class) == null || clazz.getAnnotation(DeviceType.class).parentClass() != parentClass) {
            LOGGER.debug("Removing as parentClass is not satisfied or due to absence of @DeviceType annotation:" + clazz.getClass().getName());
            continue;
        }
        DeviceType dt = clazz.getAnnotation(DeviceType.class);
        if (dt.pageType().equals(screenType)) {
            LOGGER.debug("Expected screenType: " + screenType);
            LOGGER.debug("Actual screenType: " + dt.pageType());
            if (Arrays.asList(dt.version()).contains(deviceVersion)) {
                LOGGER.debug("Expected version: " + deviceVersion);
                LOGGER.debug("Actual versions: " + dt.version());
                versionClass = clazz;
                break;
            }
            for (String version : dt.version()) {
                if (version.split(VERSION_SPLITTER)[0].equals(majorVersionNumber)) {
                    majorVersionClass = clazz;
                    LOGGER.debug("Class was chosen by major version number of device");
                    break;
                }
            }
            deviceClass = clazz;
            continue;
        }
        if (dt.pageType().getFamily().equals(screenType.getFamily())) {
            LOGGER.debug(String.format("Family class '%s' correspond to required page.", screenType.getFamily()));
            familyClass = clazz;
        }
    }
    Constructor<? extends T> ctor;
    try {
        if (versionClass != null) {
            LOGGER.debug("Instance by version and platform will be created.");
            requiredClass = versionClass;
        } else if (majorVersionClass != null) {
            LOGGER.debug("Instance by major version and platform will be created.");
            requiredClass = majorVersionClass;
        } else if (deviceClass != null) {
            LOGGER.debug("Instance by platform will be created.");
            requiredClass = deviceClass;
        } else if (familyClass != null) {
            LOGGER.debug("Instance by family will be created.");
            requiredClass = familyClass;
        } else {
            throw new RuntimeException(String.format("There is no any class that satisfy to required conditions: [parent class - %s], [device type - %s]", parentClass.getName(), screenType));
        }
        // handle cases where we have only WebDriver as ctor parameter
        if (parameters.length == 0) {
            parameters = new Object[] { driver };
        }
        LOGGER.debug("Invoking constructor for " + requiredClass);
        ctor = getConstructorByParams(requiredClass, parameters);
        return ctor.newInstance(parameters);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
        LOGGER.debug("Discovered one of the InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException");
        throw new RuntimeException("Unable to instantiate page!", e);
    }
}
Also used : Device(com.qaprosoft.carina.core.foundation.webdriver.device.Device) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(com.qaprosoft.carina.core.foundation.utils.factory.DeviceType.Type)

Aggregations

Device (com.qaprosoft.carina.core.foundation.webdriver.device.Device)7 WebDriver (org.openqa.selenium.WebDriver)2 WebDriverException (org.openqa.selenium.WebDriverException)2 RemoteDevice (com.qaprosoft.carina.commons.models.RemoteDevice)1 Parameter (com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter)1 Type (com.qaprosoft.carina.core.foundation.utils.factory.DeviceType.Type)1 ConfigurationType (com.qaprosoft.zafira.models.dto.config.ConfigurationType)1 AndroidElement (io.appium.java_client.android.AndroidElement)1 IOSDriver (io.appium.java_client.ios.IOSDriver)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1