简体   繁体   中英

How to verify whether file downloading is completed before closing the web driver in selenium?

How to verify whether file downloading is completed before closing my Selenium web driver in JAVA.

I have written a Selenium code to download 2 files to my desired folder location. However I close the browser instantly after clicking on the 2 links, which given me the downloaded files as temporary files or with an invalid extension. I used Thread.sleep method after clicking on the each of the two links before closing the web driver and it is now working fine.

I need to know whether there is an optimum method to check whether download is completed or not before closing the web driver using explicit method or any other way rather setting a pre-defined time using the Thread.sleep() method.

Here is part of the source code (JAVA, Selenium and Testng) which is relevant to this question.

  // Text file download   
    @Test(priority=2) 
     public void txtFileDownloadTest() {

      fileDownloadPage.clickTxtFileLink();
      try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

  }



    // Java file download
    @Test(priority=3)
    public void javaFileDownloadTest() {

        fileDownloadPage.clickJavaFileLink();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }


    @AfterClass 
    public void setDown() {

      closeUp();

     }

There is a piece of code I was using to download files. Just change fileName and increase timeout in waitSec(driver, int).

public WebDriverWait waitSec(WebDriver driver, int sec) {
      return new WebDriverWait(driver, sec);
}
String fileName = "foo";
String filePathFull = "C:/users/user/downloads/" + fileName  + ".csv";

waitSec(driver, 30).until(new Function<WebDriver, Boolean>() {
    public Boolean apply(WebDriver driver) {
        if(Files.exists(Paths.get(filePathFull))) {
            return true;
        }
        else {
            try {
                Thread.sleep(1000);
                } 
            catch (InterruptedException e) {
            }
        } 
    return false;
    }
});

File exportFile = new File(filePathFull);
    if (Files.size(Paths.get(filePathFull)) == 0) {
        try {
            waitSec(driver, 120).until(new Function<WebDriver, Boolean>() {
                public Boolean apply(WebDriver driver) {
                    try {
                        if(Files.size(Paths.get(filePathFull)) > 0) {
                            return true;
                            }
                        else {
                            try {
                                Thread.sleep(1000);
                                } 
                            catch (InterruptedException e) {
                                }
                            }
                        }
                    catch (IOException e) {                         
                    }
                    return false;
                }
            });
        } 
        catch (TimeoutException e) {
    }
}

There is always a.part file and until download is complete orginial file (csv in my example) has zero size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM