Search in sources :

Example 1 with RemoteDevice

use of com.qaprosoft.carina.commons.models.RemoteDevice in project carina by qaprosoft.

the class DeviceInfo method process.

protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.setStatus(HttpStatus.SC_NOT_FOUND);
    String id = request.getParameter("session");
    if (id != null) {
        TestSession session = this.getRegistry().getExistingSession(ExternalSessionKey.fromString(id));
        if (session != null) {
            Map<String, Object> cap = session.getSlot().getCapabilities();
            if (cap.containsKey("udid")) {
                RemoteDevice device = new RemoteDevice();
                device.setName((String) cap.get("deviceName"));
                device.setOs((String) cap.get("platformName"));
                device.setOsVersion((String) cap.get("platformVersion"));
                device.setType((String) cap.get("deviceType"));
                device.setUdid((String) cap.get("udid"));
                STFDevice stfDevice = STF.getDevice(device.getUdid());
                if (stfDevice != null) {
                    device.setRemoteURL((String) stfDevice.getRemoteConnectUrl());
                }
                response.setStatus(HttpStatus.SC_OK);
                response.getWriter().print(new ObjectMapper().writeValueAsString(device));
                response.getWriter().close();
            }
        }
    }
}
Also used : TestSession(org.openqa.grid.internal.TestSession) RemoteDevice(com.qaprosoft.carina.commons.models.RemoteDevice) STFDevice(com.qaprosoft.zafira.models.stf.STFDevice) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RemoteDevice

use of com.qaprosoft.carina.commons.models.RemoteDevice 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 3 with RemoteDevice

use of com.qaprosoft.carina.commons.models.RemoteDevice in project carina by qaprosoft.

the class MobileFactory method getDeviceInfo.

/**
 * Returns device information from Grid Hub using STF service.
 *
 * @param seleniumHost - Selenium Grid host
 * @param sessionId - Selenium session id
 * @return remote device information
 */
private RemoteDevice getDeviceInfo(String seleniumHost, String sessionId) {
    RemoteDevice device = null;
    try {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(seleniumHost.split("wd")[0] + "grid/admin/DeviceInfo?session=" + sessionId);
        HttpResponse response = client.execute(request);
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        device = mapper.readValue(response.getEntity().getContent(), RemoteDevice.class);
    } catch (JsonParseException e) {
    // do nothing as it is direct call to the Appium without selenium
    } catch (Exception e) {
        LOGGER.error("Unable to get device info: " + e.getMessage());
    }
    return device;
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) RemoteDevice(com.qaprosoft.carina.commons.models.RemoteDevice) JsonParseException(org.codehaus.jackson.JsonParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) MalformedURLException(java.net.MalformedURLException) JsonParseException(org.codehaus.jackson.JsonParseException)

Aggregations

RemoteDevice (com.qaprosoft.carina.commons.models.RemoteDevice)3 MalformedURLException (java.net.MalformedURLException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Device (com.qaprosoft.carina.core.foundation.webdriver.device.Device)1 STFDevice (com.qaprosoft.zafira.models.stf.STFDevice)1 AndroidElement (io.appium.java_client.android.AndroidElement)1 IOSDriver (io.appium.java_client.ios.IOSDriver)1 URL (java.net.URL)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 JsonParseException (org.codehaus.jackson.JsonParseException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 TestSession (org.openqa.grid.internal.TestSession)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1