Upload and download files from VMs using Wikisend

Some applications provide PDF files post operations ex: Flight Tickets, Insurance Forms etc. These PDFs could be validated using Perfecto Visual Analysis or third-party PDF libraries.

To perform PDF validation in Perfecto, it can be done by Visual Analysis, but this would take a lot of time since OCR and Image Check consumes time to perform action when compared to third party tool validation of PDF.

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 hectic and 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 file

The following is the code for uploading file to Wikisend. The PDF file that we are trying to upload is in the downloads folder, and the file name is boadingpass.pdf. The uploading is done on VMs by using Perfecto’s Visual Analysis features (OCR and Image based).

When the upload complete, the website would generate a file ID for the uploaded file that can be used later to download the file in other machines. This file ID is captured in the fileID variable in the following code.

The following code can be used to upload file in VMs in the Perfecto cloud.

Copy
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 file

The following is the code for downloading a file from Wikisend. Use the file ID that was captured earlier while doing upload and use it in the code to download it.

The following code can be used to download a file to the local system where you want to perform PDF validation using a third-party tool post download.

Copy
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");
    }