Search in sources :

Example 1 with Unirest

use of kong.unirest.Unirest in project seleniumRobot by bhecquet.

the class BrowserStackCapabilitiesFactory method uploadFile.

/**
 * Upload application to saucelabs server
 * @param targetAppPath
 * @param serverURL
 * @return
 * @throws IOException
 * @throws AuthenticationException
 */
private String uploadFile(String application) {
    // extract user name and password from getWebDriverGrid
    Matcher matcher = REG_USER_PASSWORD.matcher(SeleniumTestsContextManager.getThreadContext().getWebDriverGrid().get(0));
    String user;
    String password;
    if (matcher.matches()) {
        user = matcher.group(1);
        password = matcher.group(2);
    } else {
        throw new ConfigurationException("webDriverGrid variable does not have the right format for connecting to sauceLabs: \"https://<user>:<token>@hub.browserstack.com/wd/hub\"");
    }
    try (UnirestInstance unirest = Unirest.spawnInstance()) {
        String proxyHost = System.getProperty("https.proxyHost");
        String proxyPort = System.getProperty("https.proxyPort");
        if (proxyHost != null && proxyPort != null) {
            unirest.config().proxy(proxyHost, Integer.valueOf(proxyPort));
        }
        HttpResponse<JsonNode> jsonResponse = unirest.post(BROWSERSTACK_UPLOAD_URL).basicAuth(user, password).field("file", new File(application)).asJson().ifFailure(response -> {
            throw new ConfigurationException(String.format("Application file upload failed: %s", response.getStatusText()));
        }).ifSuccess(response -> {
            logger.info("Application successfuly uploaded to Saucelabs");
        });
        return jsonResponse.getBody().getObject().getString("app_url");
    } catch (UnirestException e) {
        throw new ConfigurationException("Application file upload failed: " + e.getMessage());
    }
}
Also used : HttpResponse(kong.unirest.HttpResponse) MobileCapabilityType(io.appium.java_client.remote.MobileCapabilityType) IOException(java.io.IOException) Capabilities(org.openqa.selenium.Capabilities) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) Unirest(kong.unirest.Unirest) File(java.io.File) SeleniumTestsContextManager(com.seleniumtests.core.SeleniumTestsContextManager) Matcher(java.util.regex.Matcher) JsonNode(kong.unirest.JsonNode) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestInstance(kong.unirest.UnirestInstance) Pattern(java.util.regex.Pattern) AuthenticationException(org.apache.http.auth.AuthenticationException) UnirestException(kong.unirest.UnirestException) DriverConfig(com.seleniumtests.driver.DriverConfig) UnirestInstance(kong.unirest.UnirestInstance) Matcher(java.util.regex.Matcher) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestException(kong.unirest.UnirestException) JsonNode(kong.unirest.JsonNode) File(java.io.File)

Example 2 with Unirest

use of kong.unirest.Unirest in project seleniumRobot by bhecquet.

the class SauceLabsCapabilitiesFactory method uploadFile.

/**
 * Upload application to saucelabs server
 * @param targetAppPath
 * @param serverURL
 * @return
 * @throws IOException
 * @throws AuthenticationException
 */
private void uploadFile(String application) {
    // extract user name and password from getWebDriverGrid
    Matcher matcher = REG_USER_PASSWORD.matcher(SeleniumTestsContextManager.getThreadContext().getWebDriverGrid().get(0));
    String user;
    String password;
    String datacenter;
    if (matcher.matches()) {
        user = matcher.group(1);
        password = matcher.group(2);
        datacenter = matcher.group(3);
    } else {
        throw new ConfigurationException("webDriverGrid variable does not have the right format for connecting to sauceLabs: \"https://<user>:<token>@ondemand.<datacenter>.saucelabs.com:443/wd/hub\"");
    }
    try (UnirestInstance unirest = Unirest.spawnInstance()) {
        String proxyHost = System.getProperty("https.proxyHost");
        String proxyPort = System.getProperty("https.proxyPort");
        if (proxyHost != null && proxyPort != null) {
            unirest.config().proxy(proxyHost, Integer.valueOf(proxyPort));
        }
        unirest.post(String.format(SAUCE_UPLOAD_URL, datacenter)).basicAuth(user, password).field("payload", new File(application)).field("name", new File(application).getName()).asString().ifFailure(response -> {
            throw new ConfigurationException(String.format("Application file upload failed: %s", response.getStatusText()));
        }).ifSuccess(response -> {
            logger.info("Application successfuly uploaded to Saucelabs");
        });
    } catch (UnirestException e) {
        throw new ConfigurationException("Application file upload failed: " + e.getMessage());
    }
}
Also used : MobileCapabilityType(io.appium.java_client.remote.MobileCapabilityType) IOException(java.io.IOException) Capabilities(org.openqa.selenium.Capabilities) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) MutableCapabilities(org.openqa.selenium.MutableCapabilities) Unirest(kong.unirest.Unirest) File(java.io.File) SeleniumTestsContextManager(com.seleniumtests.core.SeleniumTestsContextManager) Matcher(java.util.regex.Matcher) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestInstance(kong.unirest.UnirestInstance) Pattern(java.util.regex.Pattern) AuthenticationException(org.apache.http.auth.AuthenticationException) UnirestException(kong.unirest.UnirestException) DriverConfig(com.seleniumtests.driver.DriverConfig) UnirestInstance(kong.unirest.UnirestInstance) Matcher(java.util.regex.Matcher) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestException(kong.unirest.UnirestException) File(java.io.File)

Aggregations

SeleniumTestsContextManager (com.seleniumtests.core.SeleniumTestsContextManager)2 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 DriverConfig (com.seleniumtests.driver.DriverConfig)2 MobileCapabilityType (io.appium.java_client.remote.MobileCapabilityType)2 File (java.io.File)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Unirest (kong.unirest.Unirest)2 UnirestException (kong.unirest.UnirestException)2 UnirestInstance (kong.unirest.UnirestInstance)2 AuthenticationException (org.apache.http.auth.AuthenticationException)2 Capabilities (org.openqa.selenium.Capabilities)2 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)2 HttpResponse (kong.unirest.HttpResponse)1 JsonNode (kong.unirest.JsonNode)1 MutableCapabilities (org.openqa.selenium.MutableCapabilities)1