How to check whether a zip file is successfully downloaded or not using java-selenium webdriver?

Rahul :

I am downloading multiple zip files from an application and each file has different size. Obviously, the downloading time depends upon the file size. I have the below java code to check the download based on the file size .But it is not working as expected.It is just waiting for 10 seconds and terminating the loop by continuing to the next set of lines. Could someone please help me solve this issue?

public void isFileDownloaded(String downloadPath, String folderName) {

        String source = "downloadPath" + folderName + ".zip";

        long fileSize1;
        long fileSize2;
        do {
            System.out.println("Entered do-while loop to check if file is downloaded successfully");

            String tempFile = source + "crdownload";
            fileSize1 = tempFile.length(); // check file size
            System.out.println("file size before wait time: " + fileSize1 + " Bytes");
            Thread.sleep(10); // wait for 10 seconds
            fileSize2 = tempFile.length(); // check file size again
            System.out.println("file size after wait time: " + fileSize2 + " Bytes");

        } while (fileSize2 != fileSize1);

        }

The fileSize before and after wait time is always returning 43 bytes.

pburgr :

This is how I manage downloading files:

public class Rahul {

    String downloadDir = "C:\\Users\\pburgr\\Downloads\\";

    public WebDriverWait waitSec(WebDriver driver, int sec) {
        return new WebDriverWait(driver, sec);
    }

    public File waitToDownloadFile(WebDriver driver, int sec, String fileName) {
        String filePath = downloadDir + fileName;
        waitSec(driver, 30).until(new Function<WebDriver, Boolean>() {
          public Boolean apply(WebDriver driver) {
            if (Files.exists(Paths.get(filePath))) {
              System.out.println("Downloading " + filePath + " finished.");
              return true;
            } else {
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) {
                 System.out.println("Downloading " + filePath + " not finished yet.");
              }
            }
            return false;
          }
        });
        File downloadedFile = new File(filePath);
        return downloadedFile;
      }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=78724&siteId=1