Skip to content

Commit

Permalink
[java] Selenium Manager generates output from argument list (#13385)
Browse files Browse the repository at this point in the history
* [java] rename driver finder and selenium manager methods to getResult

* [java] Selenium Manager generates output from argument list instead of capabilities instance

* [java] allow driver finder to store state instead of static

* [java] rename method to getBinaryPaths

* [java] rename method again

* [java] false and true are different

* a getter method should have a return value

---------

Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
titusfortner and diemol authored Apr 14, 2024
1 parent 25fd49a commit 4ecc103
Show file tree
Hide file tree
Showing 26 changed files with 370 additions and 317 deletions.
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/PersistentCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private PersistentCapabilities(Capabilities previousValues, Capabilities newValu

public PersistentCapabilities setCapability(String name, Object value) {
Require.nonNull("Name", name);
Require.nonNull("Value", value);

return new PersistentCapabilities(this, new ImmutableCapabilities(name, value));
}
Expand Down
10 changes: 5 additions & 5 deletions java/src/org/openqa/selenium/chrome/ChromeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
Expand Down Expand Up @@ -96,10 +95,11 @@ private static ChromeDriverCommandExecutor generateExecutor(
Require.nonNull("Driver service", service);
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
if (result.getBrowserPath() != null && !result.getBrowserPath().isEmpty()) {
options.setBinary(result.getBrowserPath());
DriverFinder finder = new DriverFinder(service, options);
service.setExecutable(finder.getDriverPath());
if (finder.hasBrowserPath()) {
options.setBinary(finder.getBrowserPath());
options.setCapability("browserVersion", (Object) null);
}
return new ChromeDriverCommandExecutor(service, clientConfig);
}
Expand Down
26 changes: 4 additions & 22 deletions java/src/org/openqa/selenium/chrome/ChromeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

import com.google.auto.service.AutoService;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.chromium.ChromiumDriverInfo;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.NoSuchDriverException;
import org.openqa.selenium.remote.service.DriverFinder;

@AutoService(WebDriverInfo.class)
Expand Down Expand Up @@ -65,29 +62,14 @@ public boolean isSupportingBiDi() {

@Override
public boolean isAvailable() {
try {
DriverFinder.getPath(ChromeDriverService.createDefaultService(), getCanonicalCapabilities());
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(ChromeDriverService.createDefaultService(), getCanonicalCapabilities())
.isAvailable();
}

@Override
public boolean isPresent() {
try {
DriverFinder.getPath(
ChromeDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(ChromeDriverService.createDefaultService(), getCanonicalCapabilities())
.isPresent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Capabilities getDefaultDriverOptions() {
/**
* Configures and returns a new {@link ChromeDriverService} using the default configuration. In
* this configuration, the service will use the ChromeDriver executable identified by {@link
* org.openqa.selenium.remote.service.DriverFinder#getPath(DriverService, Capabilities)}. Each
* org.openqa.selenium.remote.service.DriverFinder#getResult(DriverService, Capabilities)}. Each
* service created by this method will be configured to use a free port on the current system.
*
* @return A new ChromeDriverService using the default configuration.
Expand Down
10 changes: 5 additions & 5 deletions java/src/org/openqa/selenium/edge/EdgeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
Expand Down Expand Up @@ -68,10 +67,11 @@ private static EdgeDriverCommandExecutor generateExecutor(
Require.nonNull("Driver service", service);
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
if (result.getBrowserPath() != null && !result.getBrowserPath().isEmpty()) {
options.setBinary(result.getBrowserPath());
DriverFinder finder = new DriverFinder(service, options);
service.setExecutable(finder.getDriverPath());
if (finder.hasBrowserPath()) {
options.setBinary(finder.getBrowserPath());
options.setCapability("browserVersion", (Object) null);
}
return new EdgeDriverCommandExecutor(service, clientConfig);
}
Expand Down
26 changes: 4 additions & 22 deletions java/src/org/openqa/selenium/edge/EdgeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

import com.google.auto.service.AutoService;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.chromium.ChromiumDriverInfo;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.NoSuchDriverException;
import org.openqa.selenium.remote.service.DriverFinder;

@AutoService(WebDriverInfo.class)
Expand Down Expand Up @@ -68,29 +65,14 @@ public boolean isSupportingBiDi() {

@Override
public boolean isAvailable() {
try {
DriverFinder.getPath(EdgeDriverService.createDefaultService(), getCanonicalCapabilities());
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(EdgeDriverService.createDefaultService(), getCanonicalCapabilities())
.isAvailable();
}

@Override
public boolean isPresent() {
try {
DriverFinder.getPath(
EdgeDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(EdgeDriverService.createDefaultService(), getCanonicalCapabilities())
.isPresent();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Capabilities getDefaultDriverOptions() {
/**
* Configures and returns a new {@link EdgeDriverService} using the default configuration. In this
* configuration, the service will use the MSEdgeDriver executable identified by the {@link
* org.openqa.selenium.remote.service.DriverFinder#getPath(DriverService, Capabilities)}. Each
* org.openqa.selenium.remote.service.DriverFinder#getResult(DriverService, Capabilities)}. Each
* service created by this method will be configured to use a free port on the current system.
*
* @return A new EdgeDriverService using the default configuration.
Expand Down
10 changes: 5 additions & 5 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.html5.WebStorage;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand Down Expand Up @@ -138,10 +137,11 @@ private static FirefoxDriverCommandExecutor generateExecutor(
Require.nonNull("Driver service", service);
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
if (result.getBrowserPath() != null && !result.getBrowserPath().isEmpty()) {
options.setBinary(result.getBrowserPath());
DriverFinder finder = new DriverFinder(service, options);
service.setExecutable(finder.getDriverPath());
if (finder.hasBrowserPath()) {
options.setBinary(finder.getBrowserPath());
options.setCapability("browserVersion", (Object) null);
}
return new FirefoxDriverCommandExecutor(service, clientConfig);
}
Expand Down
26 changes: 4 additions & 22 deletions java/src/org/openqa/selenium/firefox/GeckoDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@

import com.google.auto.service.AutoService;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.remote.NoSuchDriverException;
import org.openqa.selenium.remote.service.DriverFinder;

@AutoService(WebDriverInfo.class)
Expand Down Expand Up @@ -68,29 +65,14 @@ public boolean isSupportingBiDi() {

@Override
public boolean isAvailable() {
try {
DriverFinder.getPath(GeckoDriverService.createDefaultService(), getCanonicalCapabilities());
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(GeckoDriverService.createDefaultService(), getCanonicalCapabilities())
.isAvailable();
}

@Override
public boolean isPresent() {
try {
DriverFinder.getPath(
GeckoDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return new DriverFinder(GeckoDriverService.createDefaultService(), getCanonicalCapabilities())
.isPresent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Capabilities getDefaultDriverOptions() {
/**
* Configures and returns a new {@link GeckoDriverService} using the default configuration. In
* this configuration, the service will use the GeckoDriver executable identified by the {@link
* org.openqa.selenium.remote.service.DriverFinder#getPath(DriverService, Capabilities)}. Each
* org.openqa.selenium.remote.service.DriverFinder#getResult(DriverService, Capabilities)}. Each
* service created by this method will be configured to use a free port on the current system.
*
* @return A new GeckoDriverService using the default configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.net.HostIdentifier;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.remote.Command;
Expand Down Expand Up @@ -130,10 +129,10 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), this.getClass().getName());

DriverService service = builder.build();
Result driverResult = DriverFinder.getPath(service, capabilities);
service.setExecutable(driverResult.getDriverPath());
if (driverResult.getBrowserPath() != null && !driverResult.getBrowserPath().isEmpty()) {
capabilities = setBrowserBinary(capabilities, driverResult.getBrowserPath());
DriverFinder finder = new DriverFinder(service, capabilities);
service.setExecutable(finder.getDriverPath());
if (finder.hasBrowserPath()) {
capabilities = setBrowserBinary(capabilities, finder.getBrowserPath());
}

Optional<Platform> platformName = Optional.ofNullable(capabilities.getPlatformName());
Expand Down Expand Up @@ -327,7 +326,8 @@ private Capabilities setBrowserBinary(Capabilities options, String browserPath)
(Map<String, Object>) options.getCapability(vendorOptionsCapability);
vendorOptions.put("binary", browserPath);
return new PersistentCapabilities(options)
.setCapability(vendorOptionsCapability, vendorOptions);
.setCapability(vendorOptionsCapability, vendorOptions)
.setCapability("browserVersion", null);
} catch (Exception e) {
LOG.warning(
String.format(
Expand Down
4 changes: 1 addition & 3 deletions java/src/org/openqa/selenium/ie/InternetExplorerDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
Expand Down Expand Up @@ -125,8 +124,7 @@ public InternetExplorerDriver(
if (service == null) {
service = InternetExplorerDriverService.createDefaultService();
}
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
service.setExecutable(new DriverFinder(service, options).getDriverPath());
if (clientConfig == null) {
clientConfig = ClientConfig.defaultConfig();
}
Expand Down
37 changes: 8 additions & 29 deletions java/src/org/openqa/selenium/ie/InternetExplorerDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@

import com.google.auto.service.AutoService;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.remote.NoSuchDriverException;
import org.openqa.selenium.remote.service.DriverFinder;

@AutoService(WebDriverInfo.class)
Expand Down Expand Up @@ -65,36 +62,18 @@ public boolean isSupportingBiDi() {

@Override
public boolean isAvailable() {
try {
if (Platform.getCurrent().is(Platform.WINDOWS)) {
DriverFinder.getPath(
InternetExplorerDriverService.createDefaultService(), getCanonicalCapabilities());
return true;
}
return false;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return Platform.getCurrent().is(Platform.WINDOWS)
&& new DriverFinder(
InternetExplorerDriverService.createDefaultService(), getCanonicalCapabilities())
.isAvailable();
}

@Override
public boolean isPresent() {
try {
if (Platform.getCurrent().is(Platform.WINDOWS)) {
DriverFinder.getPath(
InternetExplorerDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
}
return false;
} catch (NoSuchDriverException e) {
return false;
} catch (IllegalStateException | WebDriverException e) {
LOG.log(Level.WARNING, "failed to discover driver path", e);
return false;
}
return Platform.getCurrent().is(Platform.WINDOWS)
&& new DriverFinder(
InternetExplorerDriverService.createDefaultService(), getCanonicalCapabilities())
.isPresent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public Capabilities getDefaultDriverOptions() {
/**
* Configures and returns a new {@link InternetExplorerDriverService} using the default
* configuration. In this configuration, the service will use the IEDriverServer executable
* identified by the {@link org.openqa.selenium.remote.service.DriverFinder#getPath(DriverService,
* Capabilities)}. Each service created by this method will be configured to use a free port on
* the current system.
* identified by the {@link
* org.openqa.selenium.remote.service.DriverFinder#getResult(DriverService, Capabilities)}. Each
* service created by this method will be configured to use a free port on the current system.
*
* @return A new InternetExplorerDriverService using the default configuration.
*/
Expand Down
Loading

0 comments on commit 4ecc103

Please sign in to comment.