0

After the selection of date from the date picker,clicking on 'View Report' button and then its take a time to generate the report and then it download the report.. My following code is working without an error but how do i use fluent wait instead of Thread.sleep(20000),(last line in below code). For fluent or explicit wait i ask to wait for what condition? Also wanted to verify whether the file has been downloaded or not with assertion. Any help will be appreciated.

public void generateReport() throws Exception {
        clickDatePicker.click();
        log.info("Select the Date from datepicker");
        Select month = new Select(selectMonth);
        month.selectByValue("0");
        log.info("Selected the Month from datepicker");
        Select year = new Select(selectYear);
        year.selectByValue("2020");
        log.info("Selected the Year from datepicker");
        act.moveToElement(selectDate).click().build().perform();
        buttonViewReport.click();
        log.info("Finally clicked on Get Report button ");
        Thread.sleep(20000);        
        }
Appi
  • 95
  • 1
  • 10
  • check this [post](https://stackoverflow.com/questions/22714112/wait-for-download-to-finish-in-selenium-webdriver-java/56569251#56569251) – supputuri Jan 24 '20 at 22:40
  • Thanks but i am using java, still will try to understand from the code. – Appi Jan 24 '20 at 22:42
  • My question is, after clicking on 'View Report' it go under the process to generate the report and then start downloading. If i don't add last line Thread.sleep(20000);, it will display the result on console run successfully, no error. – Appi Jan 24 '20 at 22:46
  • Downloading is not taking time, its in KB. – Appi Jan 24 '20 at 22:49
  • Here is the [java solution] (https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364). Did you checked the network tab, if it loads any details related to the report status? – supputuri Jan 24 '20 at 23:27
  • My download is not taking time, after clicking on 'View report' its take time to start downloading. Its take a time to generate the report. – Appi Jan 24 '20 at 23:32
  • I got an idea let me try it and then will share sooner. – supputuri Jan 25 '20 at 01:33

2 Answers2

0

Check the below method, which will make sure the script will wait until the download is started (for max of the minutes specified in the method call)

    public void waitUntilDownloadStarted(WebDriver driver, int maxWaitTimeInMinutes) throws InterruptedException {
        // Store the current window handle
        String mainWindow = driver.getWindowHandle();

        // open a new tab
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("window.open()");
        // switch to new tab
        // Switch to new window opened
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }
        // navigate to chrome downloads
        driver.get("chrome://downloads");
        Instant startTime = Instant.now();

        int elapsedTime = (int) Duration.between(startTime, Instant.now()).toMinutes();
        // wait until the download is started
        while ( (Long)js.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelectorAll('#downloadsList downloads-item').length") == 0) {
            Thread.sleep(1000);
            elapsedTime = (int) Duration.between(startTime, Instant.now()).toMinutes();
            if  (elapsedTime > maxWaitTimeInMinutes) {
                break;
            }

        }
        // close the downloads tab2
        driver.close();
        // switch back to main window
        driver.switchTo().window(mainWindow);
    }

Tested as below.

waitUntilDownloadStarted(driver, 10);
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks but i am little confused with your suggestion as i don't need to switch my control to any window. After clicking on 'Get Report' button and before it start getting download, its take good amount of time to generate/prepare the report ans for that time i need to wait which i have handled through 'Thread.sleep(20000); '. And i want an alternative for that line.. – Appi Jan 27 '20 at 15:33
  • The solution will keep checking if the download is begin or not. If download not kicked off then it will wait until the download started (indirectly you are waiting until the report is generated). Switching to download window does not impact your script or testing... – supputuri Jan 27 '20 at 17:13
  • it does not seems that driver is switching to any window, it just showing that file has been downloaded at the taskbar. – Appi Jan 27 '20 at 17:22
  • Sorry, seems to be I am missing something here. When you click on `buttonViewReport` does it generate the report and then do you have a button to download the generated report? – supputuri Jan 27 '20 at 20:58
  • When i click on buttonViewreport, its just show its under process, loading report, like how page take time to load, then immediately it shows file is getting download. In short after clicking on button, its just show the file is getting download. – Appi Jan 28 '20 at 04:51
0

Does anything appears like that your download has been generated? or inspect any change in HTML. then you can use the following code to wait until change appears.

WebDriverWait wait=new WebDriverWait(driver, 20000); 
wait.until(ExpectedConditions.numberOfElementsToBe(locator, number));

where 20000 is time in milliseconds

Adnan Asghar
  • 64
  • 1
  • 7
  • ,Will you please tell me what i place at the 'locator'. Please go-through my above code and tell me the value of locator and number. – Appi Jan 27 '20 at 15:35