What happened?
First of all, thank you for the new download function. It works really well.
Even though the documentation clearly indicates that a download is only supported via a Selenium Grid, I found an unfortunate NPE when trying to perform a download without Selenium Grid.
The NPE occurs when HasDownloads.requireDownloadsEnabled() tries to cast the return value of capabilities.getCapability("se:downloadsEnabled") to a boolean. In the case of a locally launched browser, capabilities.getCapability("se:downloadsEnabled") returns null, causing the following NPE:
java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because the return value of "org.openqa.selenium.Capabilities.getCapability(String)" is null
I think there are to ways to prevent this NPE.
- Changing
(boolean) capabilities.getCapability("se:downloadsEnabled") to Boolean.parseBoolean(capabilities.getCapability("se:downloadsEnabled")) since it will evaluate null to false.
- Check for
null and throw a WebDriverException stating that downloads are only supported when running tests via Selenium Grid:
Object downloadCapability = capabilities.getCapability("se:downloadsEnabled");
if (downloadCapability == null) {
throw new WebDriverException("Use a Selenium Grid in order to work with downloadable files.")
}
boolean downloadsEnabled = (boolean) downloadCapability;
if (!downloadsEnabled) {
throw new WebDriverException(
"You must enable downloads in order to work with downloadable files.");
}
How can we reproduce the issue?
public static void main(String[] args) throws InterruptedException {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setEnableDownloads(true);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.navigate().to("https://www.selenium.dev/downloads/");
WebDriver augmentedDriver = new Augmenter().augment(driver);
// This will cause the NPE
List<String> downloadedFiles = ((HasDownloads) augmentedDriver).getDownloadableFiles();
}
Relevant log output
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because the return value of "org.openqa.selenium.Capabilities.getCapability(String)" is null
at org.openqa.selenium.HasDownloads.requireDownloadsEnabled(HasDownloads.java:36)
at org.openqa.selenium.remote.RemoteWebDriver.getDownloadableFiles(RemoteWebDriver.java:636)
Operating System
Windows 10
Selenium version
Java 4.17.0
What are the browser(s) and version(s) where you see this issue?
Chrome 121
What are the browser driver(s) and version(s) where you see this issue?
ChromeDriver 121
Are you using Selenium Grid?
No response
What happened?
First of all, thank you for the new download function. It works really well.
Even though the documentation clearly indicates that a download is only supported via a Selenium Grid, I found an unfortunate NPE when trying to perform a download without Selenium Grid.
The NPE occurs when
HasDownloads.requireDownloadsEnabled()tries to cast the return value ofcapabilities.getCapability("se:downloadsEnabled")to a boolean. In the case of a locally launched browser,capabilities.getCapability("se:downloadsEnabled")returnsnull, causing the following NPE:java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because the return value of "org.openqa.selenium.Capabilities.getCapability(String)" is nullI think there are to ways to prevent this NPE.
(boolean) capabilities.getCapability("se:downloadsEnabled")toBoolean.parseBoolean(capabilities.getCapability("se:downloadsEnabled"))since it will evaluatenulltofalse.nulland throw a WebDriverException stating that downloads are only supported when running tests via Selenium Grid:How can we reproduce the issue?
public static void main(String[] args) throws InterruptedException { ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setEnableDownloads(true); WebDriver driver = new ChromeDriver(chromeOptions); driver.navigate().to("https://www.selenium.dev/downloads/"); WebDriver augmentedDriver = new Augmenter().augment(driver); // This will cause the NPE List<String> downloadedFiles = ((HasDownloads) augmentedDriver).getDownloadableFiles(); }Relevant log output
Operating System
Windows 10
Selenium version
Java 4.17.0
What are the browser(s) and version(s) where you see this issue?
Chrome 121
What are the browser driver(s) and version(s) where you see this issue?
ChromeDriver 121
Are you using Selenium Grid?
No response