Upload and download files from VMs using Wikisend
Some applications provide PDF files post operations, for example flight tickets or insurance forms. These PDFs could be validated using Perfecto Visual Analysis or third-party PDF libraries.
You can perform PDF validation in Perfecto using visual analysis, but this would take a lot of time because optical character recognition (OCR) and image check are time-consuming compared to a third-party tool validation of PDFs.
When we run tests on VM browsers in Perfecto and a PDF gets generated, verifying and validating this PDF is not possible on a VM, so there is a need to download this PDF to a local machine where validating it is fast using third-party tools.
To achieve this, there are a lot of online file store/transfer applications (for example Google Drive, Dropbox, Amazon Drive, and so on) available to upload a file onto one machine and later download it onto another machine. But these web apps perform a security check when we log in (for example Send OTP to registered phone, send code to email, and so on). Automating this is time consuming. To avoid this, there is a third-party web app called Wikisend (http://wikisend.com/) that performs file upload/download without login.
Upload a file
Following is the code for uploading a file to Wikisend. The PDF file that we are trying to upload is in the downloads folder, and the file name is boardingpass.pdf
. The upload happens on VMs by using Perfecto’s Visual Analysis features (OCR and image based).
When the upload is complete, the website generates a file ID for the uploaded file that can be used later to download the file on other machines. This file ID is captured in the fileID variable in the following code sample.
You can use the following code sample to upload the file to VMs in the Perfecto cloud.
public static void wikisendUpload(RemoteWebDriver driver) throws InterruptedException {
System.out.println("wikisendUpload");
driver.get("http://wikisend.com/");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"upload\"]/a/img")));
driver.findElement(By.xpath("//*[@id=\"upload\"]/a/img")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"uploadfield\"]")));
driver.findElement(By.xpath("//*[@id=\"uploadfield\"]")).click();
Thread.sleep(15000);
try {
ocrClickOnDownloadsFolder(driver);
ocrClickOnBoardingPassFile(driver);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Didn't find boarding pass file");
System.out.println("Re-trying to click on downloads folder");
ocrClickOnDownloadsFolder(driver);
ocrClickOnBoardingPassFile(driver);
}
ocrClickOnOpenButton(driver);
Thread.sleep(10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"upload_but\"]")));
driver.findElement(By.xpath("//*[@id=\"upload_but\"]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"fileidvalue\"]")));
fileID = driver.findElement(By.xpath("//*[@id=\"fileidvalue\"]")).getText();
System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
System.out.println("fileID is: " + fileID);
System.out.println("wikisendUpload successfull");
}
public static void ocrClickOnDownloadsFolder(RemoteWebDriver driver) {
Map<String, Object> params1 = new HashMap<>();
params1.put("content", "Downloads");
params1.put("timeout", "60");
params1.put("threshold", "100");
params1.put("screen.top", "0%");
params1.put("screen.height", "60%");
params1.put("screen.left", "0%");
params1.put("screen.width", "60%");
driver.executeScript("mobile:text:select", params1);
System.out.println("Clicked on downloads folder");
}
public static void ocrClickOnBoardingPassFile(RemoteWebDriver driver) {
Map<String, Object> params2 = new HashMap<>();
params2.put("content", "boardingpass");
params2.put("threshold", "90");
params2.put("timeout", "60");
params2.put("screen.top", "0%");
params2.put("screen.height", "60%");
params2.put("screen.left", "0%");
params2.put("screen.width", "60%");
driver.executeScript("mobile:text:select", params2);
System.out.println("Clicked on boarding pass file");
}
public static void ocrClickOnOpenButton(RemoteWebDriver driver) {
Map<String, Object> params3 = new HashMap<>();
params3.put("label", "Cancel");
params3.put("threshold", "100");
params3.put("timeout", "60");
params3.put("screen.top", "0%");
params3.put("screen.height", "60%");
params3.put("screen.left", "0%");
params3.put("screen.width", "60%");
params3.put("label.direction", "right");
params3.put("label.offset", "7%");
driver.executeScript("mobile:button-text:click", params3);
System.out.println("Clicked on open button");
}
Download a file
The following code sample downloads a file from Wikisend to the local system where you want to perform PDF validation using a third-party tool post download. It uses the file ID that was captured earlier during the upload.
public static void wikisendDownload(RemoteWebDriver driver) {
System.out.println("wikisendDownload");
driver.get("http://wikisend.com/");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"download\"]/a/img")));
driver.findElement(By.xpath("//*[@id=\"download\"]/a/img")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"fileid\"]")));
driver.findElement(By.xpath("//*[@id=\"fileid\"]")).sendKeys(fileID);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"dbi_but\"]")));
driver.findElement(By.xpath("//*[@id=\"dbi_but\"]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"d_but\"]")));
driver.findElement(By.xpath("//*[@id=\"d_but\"]")).click();
System.out.println("wikisendDownload successfull");
}