kazurayam / selenium-webdriver-java

Examples of the O'Reilly book "Hands-On Selenium WebDriver with Java"
https://oreil.ly/1E7CX
Apache License 2.0
0 stars 0 forks source link

ch09/download/DownloadFirefox*Test is interrupted by a Dialog "Cancel All Downloads?" #27

Open kazurayam opened 9 months ago

kazurayam commented 9 months ago

When I run https://github.com/kazurayam/selenium-webdriver-java/blob/develop/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch09/download/DownloadFirefoxJUnit4Test.java

Firefox displays a modal dialog "Cancel All Download?" as the following screenshot shows

Screenshot 2023-11-07 at 20 49 26

My Firefox version is 119.0.

I want to avoid being interrupted by the dialog.

kazurayam commented 9 months ago

In the console, I found the following messages:

> Task :selenium-webdriver-junit4:compileJava UP-TO-DATE
> Task :selenium-webdriver-junit4:processResources UP-TO-DATE
> Task :selenium-webdriver-junit4:classes UP-TO-DATE
> Task :selenium-webdriver-junit4:compileTestJava UP-TO-DATE
> Task :selenium-webdriver-junit4:processTestResources UP-TO-DATE
> Task :selenium-webdriver-junit4:testClasses UP-TO-DATE
2023-11-07 21:09:28 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.<init>(227) - Using WebDriverManager 5.6.0
2023-11-07 21:09:28 [Test worker] DEBUG i.g.b.wdm.cache.ResolutionCache.checkKeyInResolutionCache(186) - Resolution firefox=119 in cache (valid until 21:14:35 07/11/2023 JST)
2023-11-07 21:09:28 [Test worker] DEBUG i.g.b.wdm.cache.ResolutionCache.checkKeyInResolutionCache(186) - Resolution firefox119=0.33.0 in cache (valid until 20:14:35 08/11/2023 JST)
2023-11-07 21:09:28 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.resolveDriverVersion(1229) - Using geckodriver 0.33.0 (resolved driver for Firefox 119)
2023-11-07 21:09:28 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.manage(1184) - Driver geckodriver 0.33.0 found in cache
2023-11-07 21:09:28 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.exportDriver(1290) - Exporting webdriver.gecko.driver as /Users/kazuakiurayama/.cache/selenium/geckodriver/mac64/0.33.0/geckodriver
Nov 07, 2023 9:09:36 PM org.openqa.selenium.os.ExternalProcess$Builder lambda$start$0
WARNING: failed to copy the output of process 14770
java.io.IOException: Stream closed
    at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168)
    at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at java.base/java.io.InputStream.transferTo(InputStream.java:782)
    at org.openqa.selenium.os.ExternalProcess$Builder.lambda$start$0(ExternalProcess.java:209)
    at java.base/java.lang.Thread.run(Thread.java:833)

> Task :selenium-webdriver-junit4:testSelective
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 14s
5 actionable tasks: 1 executed, 4 up-to-date
21:09:37: Execution finished ':selenium-webdriver-junit4:testSelective --tests "io.github.bonigarcia.webdriver.junit4.ch09.download.DownloadFirefoxJUnit4Test"'.
kazurayam commented 9 months ago

Why Firefox displays the dialogue "Cancel All Downloads?"

The process of downloading files from the remote URL runs asynchronously from the browser's process. Downloading may take long seconds. If you try to close the browser while one or more downloading processes are still running, then the Firefox displays the dialog and ask you if you really want to stop the browser window and terminate the downloading process as well.

If you read the source of DownloadFirefox*Test, the current code has the following lines:

        ConditionFactory await = Awaitility.await()
                .atMost(Duration.ofSeconds(5));
        File wdmLogo = new File(targetFolder, "webdrivermanager.png");
        await.until(() -> wdmLogo.exists());

        File wdmDoc = new File(targetFolder, "webdrivermanager.pdf");
        await.until(() -> wdmDoc.exists());

I understand the intention here is

  1. the code waits for the files "webdrivermanager.png" and "webdrivermanager.pdf" to be downloaded and to become present. If the download process of the PDF file took 2 seconds, then the test should wait for 2 seconds until the PDF file become present
  2. once the file has become present, then the @Test-annotated method will finish, then the @After-annotated code
    @After
    public void teardown() {
        driver.quit();
    }

    will invoked --- gently the browser will be closed.

  3. in other words, the code assumes that the 2 files are initially not there.

I reviewed the current code, and found that the code does not remove the 2 files created by the previous run. In case that the files were previously created and stayed there, the test will not wait for the downloading process to finish. The test will call driver.quit() too soon.

So I changed the code to remove the previous output sub directory, as this:

   @BeforeClass
    static void setupClass() throws IOException {
        too = TestOutputOrganizerFactory.create(DownloadFirefoxNGTest.class);
        too.cleanOutputSubDirectory();
    }

The TestOutputOrganizer class is proposed by the Pull Request #748 by @kazurayam

I expected that this change would be enough to avoid the dialog.


But ... that was not enough. It seemed that the code

await.until(() -> wdmDoc.exists());

is not effective. Apparently, driver.quit() was called before the downloading PDF process finishes. Why? --- I don't know yet.

Tentatively, I inserted a line of hard wait:

await.until(() -> wdmDoc.exists());

Thread.sleep(3000);

Now I do not see the dialog any longer.