Search in sources :

Example 1 with SeleniumGridException

use of com.seleniumtests.customexception.SeleniumGridException in project seleniumRobot by bhecquet.

the class SeleniumRobotGridConnector method uploadMobileApp.

/**
 * In case an app is required on the node running the test, upload it to the grid hub
 * This will then be made available through HTTP GET URL to the node (appium will receive an url instead of a file)
 */
@Override
public void uploadMobileApp(Capabilities caps) {
    String appPath = (String) caps.getCapability(MobileCapabilityType.APP);
    // check whether app is given and app path is a local file
    if (appPath != null && new File(appPath).isFile()) {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // zip file
            List<File> appFiles = new ArrayList<>();
            appFiles.add(new File(appPath));
            File zipFile = FileUtility.createZipArchiveFromFiles(appFiles);
            HttpHost serverHost = new HttpHost(hubUrl.getHost(), hubUrl.getPort());
            URIBuilder builder = new URIBuilder();
            builder.setPath("/grid/admin/FileServlet/");
            builder.addParameter(OUTPUT_FIELD, "app");
            HttpPost httpPost = new HttpPost(builder.build());
            httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
            FileInputStream fileInputStream = new FileInputStream(zipFile);
            InputStreamEntity entity = new InputStreamEntity(fileInputStream);
            httpPost.setEntity(entity);
            CloseableHttpResponse response = client.execute(serverHost, httpPost);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new SeleniumGridException("could not upload application file: " + response.getStatusLine().getReasonPhrase());
            } else {
                // set path to the mobile application as an URL on the grid hub
                ((DesiredCapabilities) caps).setCapability(MobileCapabilityType.APP, IOUtils.toString(response.getEntity().getContent()) + "/" + appFiles.get(0).getName());
            }
        } catch (IOException | URISyntaxException e) {
            throw new SeleniumGridException("could not upload application file", e);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) FileInputStream(java.io.FileInputStream) URIBuilder(org.apache.http.client.utils.URIBuilder) InputStreamEntity(org.apache.http.entity.InputStreamEntity) SeleniumGridException(com.seleniumtests.customexception.SeleniumGridException) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File)

Example 2 with SeleniumGridException

use of com.seleniumtests.customexception.SeleniumGridException in project seleniumRobot by bhecquet.

the class SeleniumRobotGridConnector method uploadFile.

/**
 * Upload a file given file path
 * @param filePath
 */
@Override
public void uploadFile(String filePath) {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        // zip file
        List<File> appFiles = new ArrayList<>();
        appFiles.add(new File(filePath));
        File zipFile = FileUtility.createZipArchiveFromFiles(appFiles);
        HttpHost serverHost = new HttpHost(hubUrl.getHost(), hubUrl.getPort());
        URIBuilder builder = new URIBuilder();
        builder.setPath("/grid/admin/FileServlet/");
        builder.addParameter(OUTPUT_FIELD, "app");
        HttpPost httpPost = new HttpPost(builder.build());
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
        FileInputStream fileInputStream = new FileInputStream(zipFile);
        InputStreamEntity entity = new InputStreamEntity(fileInputStream);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = client.execute(serverHost, httpPost);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new SeleniumGridException("could not upload file: " + response.getStatusLine().getReasonPhrase());
        } else {
            throw new NotImplementedException("call remote Robot to really upload file");
        }
    } catch (IOException | URISyntaxException e) {
        throw new SeleniumGridException("could not upload file", e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) NotImplementedException(org.apache.commons.lang.NotImplementedException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) FileInputStream(java.io.FileInputStream) URIBuilder(org.apache.http.client.utils.URIBuilder) InputStreamEntity(org.apache.http.entity.InputStreamEntity) SeleniumGridException(com.seleniumtests.customexception.SeleniumGridException) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File)

Example 3 with SeleniumGridException

use of com.seleniumtests.customexception.SeleniumGridException in project seleniumRobot by bhecquet.

the class SeleniumRobotGridConnector method uploadFileToNode.

/**
 * Upload file to node
 * @param filePath			the file to upload
 * @param returnLocalFile	if true, returned path will be the local path on grid node. If false, we get file://upload/file/<uuid>/
 * @return
 */
@Override
public String uploadFileToNode(String filePath, boolean returnLocalFile) {
    if (nodeUrl == null) {
        throw new ScenarioException("You cannot upload file to browser before driver has been created and corresponding node instanciated");
    }
    // zip file
    File zipFile = null;
    try {
        List<File> appFiles = new ArrayList<>();
        appFiles.add(new File(filePath));
        zipFile = FileUtility.createZipArchiveFromFiles(appFiles);
    } catch (IOException e1) {
        throw new SeleniumGridException("Error in uploading file, when zipping: " + e1.getMessage());
    }
    logger.info("uploading file to node: " + zipFile.getName());
    try {
        HttpRequestWithBody req = Unirest.post(String.format("%s%s", nodeUrl, FILE_SERVLET)).header(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString()).queryString(OUTPUT_FIELD, "file");
        if (returnLocalFile) {
            req = req.queryString("localPath", "true");
        }
        HttpResponse<String> response = req.field("upload", zipFile).asString();
        if (response.getStatus() != 200) {
            throw new SeleniumGridException(String.format("Error uploading file: %s", response.getBody()));
        } else {
            return response.getBody();
        }
    } catch (UnirestException e) {
        throw new SeleniumGridException(String.format("Cannot upload file: %s", e.getMessage()));
    }
}
Also used : SeleniumGridException(com.seleniumtests.customexception.SeleniumGridException) HttpRequestWithBody(kong.unirest.HttpRequestWithBody) ArrayList(java.util.ArrayList) UnirestException(kong.unirest.UnirestException) IOException(java.io.IOException) File(java.io.File) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Aggregations

SeleniumGridException (com.seleniumtests.customexception.SeleniumGridException)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 FileInputStream (java.io.FileInputStream)2 URISyntaxException (java.net.URISyntaxException)2 HttpHost (org.apache.http.HttpHost)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 InputStreamEntity (org.apache.http.entity.InputStreamEntity)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 ScenarioException (com.seleniumtests.customexception.ScenarioException)1 HttpRequestWithBody (kong.unirest.HttpRequestWithBody)1 UnirestException (kong.unirest.UnirestException)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)1