1

I'm trying to wait for a file to get downloaded using fluent wait. But since the file downloads with different date format. I want to validate with "Partial filename" of the file using fluentWait. For Eg: cancelled_07092019, cancelled_09_07_2019. So here the filename 'canceled_' remains constant

Below code works fine for the actual filename.

Downloaded_report= new File("DownloadPath");
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
        wait.pollingEvery(1, TimeUnit.SECONDS);
        wait.withTimeout(15, TimeUnit.SECONDS);
        wait.until(x -> downloaded_report.exists());

Here, I want to check the startof the file name. How do I do this? Thanks in Advance

user1430391
  • 83
  • 1
  • 8
  • 1
    Have you checked this [post](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364) which will make sure the script waits until the file download is completed. – supputuri Jul 08 '19 at 19:47
  • Hi Supputuri, Yes I went through the post but the code which I have used here is also working fine. But the only thing is, I want to check if the "partial_file_name" exists instead of the actual one using fluent wait. – user1430391 Jul 08 '19 at 20:32
  • Do you mean to check if the file name consist of the "partial filename" that you are passing? – supputuri Jul 08 '19 at 21:30
  • Yes, if you see the example provided- the first half of the filename remains the same. So, I want to check if the file exists based on the 'partial name' which I will be passing. – user1430391 Jul 09 '19 at 04:51

1 Answers1

1

I am not sure if I understood the question correctly but you can check if the file with the partial name exists in the given directory.

File dir = new File("DownloadPath");
String partialName = downloaded_report.split("_")[0].concat("_"); //get cancelled and add underscore
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
        wait.pollingEvery(1, TimeUnit.SECONDS);
        wait.withTimeout(15, TimeUnit.SECONDS);
        wait.until(x -> {
            File[] filesInDir = dir.listFiles();
            for (File fileInDir : filesInDir) {
                if (fileInDir.getName().startsWith(partialName)) {
                    return true;
                }
            }
            return false;
        });

In the above code, we list all files in the download path directory. Then, iterate over each file, get its name and check if it contains the partial name that we want.

It might be a problem if you have multiple reports in the directory. Then I would advise clearing DownloadPath from files that start with cancelled_ in some kind of @Before hook.

Fenio
  • 3,528
  • 1
  • 13
  • 27