SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.63k stars 8.18k forks source link

[🐛 Bug]: Does ChromeDriver not support HasDownloads ? #13085

Closed iamxiaojianzheng closed 12 months ago

iamxiaojianzheng commented 12 months ago

What happened?

downloadsEnabled, This variable is null.

default void requireDownloadsEnabled(Capabilities capabilities) {
    boolean downloadsEnabled = (boolean) capabilities.getCapability("se:downloadsEnabled");
    if (!downloadsEnabled) {
      throw new WebDriverException(
          "You must enable downloads in order to work with downloadable files.");
    }
  }

How can we reproduce the issue?

TestCase

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setEnableDownloads(true);
ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
chromeDriver.get("https://www.voidtools.com/zh-cn/");
chromeDriver.findElement(By.xpath("//a[text()='下载安装版']")).click();
List<String> files = ((HasDownloads) chromeDriver).getDownloadableFiles();
File file = new File(files.get(0));
Assertions.assertTrue(file.exists());
file.delete();

Relevant log output

java.lang.NullPointerException
    at org.openqa.selenium.HasDownloads.requireDownloadsEnabled(HasDownloads.java:36)
    at org.openqa.selenium.remote.RemoteWebDriver.getDownloadableFiles(RemoteWebDriver.java:724)

Operating System

Windows 10

Selenium version

Java 4.15.0

What are the browser(s) and version(s) where you see this issue?

Chrome 119.0.6045.105

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 119.0.6045.105

Are you using Selenium Grid?

No response

github-actions[bot] commented 12 months ago

@iamxiaojianzheng, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

diemol commented 12 months ago

This is only meant to be used for Grid, you are using a local Chrome. There is no need for this if you are using local downloads.

@titusfortner maybe we need something in the docs? Should we do something in the code?

iamxiaojianzheng commented 12 months ago

Is it possible to add the function of downloadedFiles to ChromeDriver?

diemol commented 12 months ago

Local downloads already worked since a long time ago, you can set the browser preferences to define the location.

titusfortner commented 12 months ago

We need something in the docs, I'm still fighting the examples code in the CI.

baflQA commented 12 months ago

@titusfortner maybe You can spot what am I doing wrong here by any chance? I might be missing something that You could add into the docs too. https://github.com/SeleniumHQ/selenium/issues/12695

titusfortner commented 12 months ago

I'm guessing you don't have grid started properly....

This works:

int port = PortProber.findFreePort();
Main.main(
    new String[] {
      "standalone",
      "--port",
      String.valueOf(port),
      "--selenium-manager",
      "true",
      "--enable-managed-downloads",
      "true"
    });
URL gridUrl = new URL("http://localhost:" + port);

ChromeOptions options = new ChromeOptions();
options.setEnableDownloads(true);
driver = new RemoteWebDriver(gridUrl, options);
driver = new Augmenter().augment(driver);

driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");
driver.findElement(By.id("file-1")).click();
driver.findElement(By.id("file-2")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
        .until(d -> ((HasDownloads) d).getDownloadableFiles().contains("file_2.jpg"));

String fileName = ((HasDownloads) driver).getDownloadableFiles().get(0);

Path targetLocation = Files.createTempDirectory("download");
((HasDownloads) driver).downloadFile(fileName, targetLocation);

String fileContent = String.join("", Files.readAllLines(targetLocation.resolve(fileName)));
Assertions.assertEquals("Hello, World!", fileContent);

((HasDownloads) driver).deleteDownloadableFiles();
Assertions.assertTrue(((HasDownloads) driver).getDownloadableFiles().isEmpty());
krmahadevan commented 12 months ago

@titusfortner - This was the reason https://github.com/SeleniumHQ/selenium/issues/12695#issuecomment-1790713161

titusfortner commented 12 months ago

I'm closing this one. I'll add conversation about this feature in the docs when I'm done with updating everything.

github-actions[bot] commented 11 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.