Rahul
Rahul

Reputation: 759

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

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.

Upvotes: 4

Views: 1901

Answers (2)

pburgr
pburgr

Reputation: 1778

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;
      }
}

Upvotes: 2

cruisepandey
cruisepandey

Reputation: 29362

Take two list of Strings :

List<String> expectedFileName ;

in this list add every file name which you must have in String Format.[Your expected File names]

then after downloading all the files, go to download directory :

Check how many files are present by using code like this :

new File(<directory path>).list().length

Now compare the length of expected and actual :

expectedFileName.size() and new File(<directory path>).list().length , If there is any mismatch then return false and print Files are missing.If there's not any mismatch then get all the file names from directory like this :

List<String> actualFileName;
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
   actualFileName.add(listOfFiles[i].getName());
}
} 

Now you have two list of String , you can easily compare it. Though It won't check the file size.

Upvotes: 1

Related Questions