1

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

     }
  • Check my answer [here](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364) – supputuri May 10 '20 at 06:25
  • Does this answer your question? [Selenium give file name when downloading](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading) – supputuri May 10 '20 at 06:27
  • No, this does not answer my question. As it is about changing the name of the downloaded file. I can check whether file exist in the downloaded location by using File.exist() method in JAVA. I asked this question to check file exist as soon as the download completed in the browser without waiting a pre-defined time using Thread.sleep() method. – Suresh Madhusanka Rodrigo May 10 '20 at 07:49
  • You mean the script should keep checking for the given file (with name) in the specified download folder? – supputuri May 11 '20 at 00:24
  • Yeah, think as an example if we would need to be download a large file and after completing X minutes it will check whether file exist in the downloaded folder with the name. However in a slow internet connection day this X minutes will not be enough and it will give an error file does not exist this time. So we need a dynamic / intelligent way to handle this scenario. – Suresh Madhusanka Rodrigo May 14 '20 at 05:40

1 Answers1

0

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.

pburgr
  • 1,722
  • 1
  • 11
  • 26