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);
}
}
}
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);
}
}
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()));
}
}
Aggregations