Skip to content

Commit

Permalink
[grid] Using SM to find drivers on PATH
Browse files Browse the repository at this point in the history
Grid portion of #11356
  • Loading branch information
diemol committed Jul 21, 2023
1 parent 44d202c commit 0013140
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 95 deletions.
7 changes: 6 additions & 1 deletion java/src/org/openqa/selenium/chrome/ChromeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return ChromeDriverService.isPresent();
try {
DriverFinder.getPath(ChromeDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
Expand Down
10 changes: 0 additions & 10 deletions java/src/org/openqa/selenium/chrome/ChromeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,6 @@ public static ChromeDriverService createServiceWithConfig(ChromeOptions options)
return new Builder().withLogLevel(level).build();
}

/**
* Checks if the ChromeDriver binary is already present. Grid uses this method to show the
* available browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(CHROME_DRIVER_NAME, CHROME_DRIVER_EXE_PROPERTY) != null;
}

/** Builder used to configure new {@link ChromeDriverService} instances. */
@AutoService(DriverService.Builder.class)
public static class Builder
Expand Down
12 changes: 6 additions & 6 deletions java/src/org/openqa/selenium/edge/EdgeDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return EdgeDriverService.isPresent();
}

@Override
public int getMaximumSimultaneousSessions() {
return Runtime.getRuntime().availableProcessors();
try {
DriverFinder.getPath(EdgeDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
Expand Down
10 changes: 0 additions & 10 deletions java/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ public static EdgeDriverService createDefaultService() {
return new Builder().build();
}

/**
* Checks if the MSEdgeDriver binary is already present. Grid uses this method to show the
* available browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(EDGE_DRIVER_NAME, EDGE_DRIVER_EXE_PROPERTY) != null;
}

/** Builder used to configure new {@link EdgeDriverService} instances. */
@AutoService(DriverService.Builder.class)
public static class Builder extends DriverService.Builder<EdgeDriverService, Builder> {
Expand Down
7 changes: 6 additions & 1 deletion java/src/org/openqa/selenium/firefox/GeckoDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return GeckoDriverService.isPresent();
try {
DriverFinder.getPath(GeckoDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
Expand Down
10 changes: 0 additions & 10 deletions java/src/org/openqa/selenium/firefox/GeckoDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,6 @@ public static GeckoDriverService createDefaultService() {
return new Builder().build();
}

/**
* Checks if the GeckoDriver binary is already present. Grid uses this method to show the
* available browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(GECKO_DRIVER_NAME, GECKO_DRIVER_EXE_PROPERTY) != null;
}

/**
* @param caps Capabilities instance - this is not used
* @return default GeckoDriverService
Expand Down
11 changes: 3 additions & 8 deletions java/src/org/openqa/selenium/grid/node/config/NodeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.Urls;
Expand Down Expand Up @@ -569,18 +568,15 @@ private Map<WebDriverInfo, Collection<SessionFactory>> discoverDrivers(
.sorted(Comparator.comparing(info -> info.getDisplayName().toLowerCase()))
.collect(Collectors.toList());

LOG.log(Level.INFO, "Driver(s) already present on the host: {0}", infos.size());

if (config.getBool(NODE_SECTION, "selenium-manager").orElse(DEFAULT_USE_SELENIUM_MANAGER)) {
List<String> present =
infos.stream().map(WebDriverInfo::getDisplayName).collect(Collectors.toList());
List<WebDriverInfo> driversSM =
StreamSupport.stream(ServiceLoader.load(WebDriverInfo.class).spliterator(), false)
.filter(WebDriverInfo::isAvailable)
.filter(info -> !present.contains(info.getDisplayName()))
.filter(WebDriverInfo::isAvailable)
.sorted(Comparator.comparing(info -> info.getDisplayName().toLowerCase()))
.collect(Collectors.toList());
LOG.log(Level.INFO, "Driver(s) available through Selenium Manager: {0}", driversSM.size());
infos.addAll(driversSM);
}

Expand Down Expand Up @@ -715,11 +711,10 @@ private void report(Map.Entry<WebDriverInfo, Collection<SessionFactory>> entry)

LOG.info(
String.format(
"Adding %s for %s %d times (%s)",
"Adding %s for %s %d times",
entry.getKey().getDisplayName(),
caps.toString().replaceAll("\\s+", " "),
entry.getValue().size(),
entry.getKey().isPresent() ? "Host" : "SM"));
entry.getValue().size()));
}

private String unquote(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
new SessionNotCreatedException(
"New session request capabilities do not " + "match the stereotype."));
}

capabilities = capabilities.merge(stereotype);
LOG.info("Starting session for " + capabilities);

try (Span span = tracer.getCurrentContext().createSpan("relay_session_factory.apply")) {
Expand Down
12 changes: 10 additions & 2 deletions java/src/org/openqa/selenium/ie/InternetExplorerDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return InternetExplorerDriverService.isPresent();
}
try {
if (Platform.getCurrent().is(Platform.WINDOWS)) {
DriverFinder.getPath(
InternetExplorerDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
}
return false;
} catch (IllegalStateException | WebDriverException e) {
return false;
} }

@Override
public int getMaximumSimultaneousSessions() {
Expand Down
10 changes: 0 additions & 10 deletions java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ public static InternetExplorerDriverService createDefaultService() {
return new Builder().build();
}

/**
* Checks if the IEDriverServer binary is available. Grid uses this method to show the available
* browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(IE_DRIVER_NAME, IE_DRIVER_EXE_PROPERTY) != null;
}

/** Builder used to configure new {@link InternetExplorerDriverService} instances. */
@AutoService(DriverService.Builder.class)
public static class Builder
Expand Down
6 changes: 5 additions & 1 deletion java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private String getBrowserBinary(Capabilities options) {
* @param options Browser Options instance.
* @return the location of the driver.
*/
public String getDriverPath(Capabilities options) {
public String getDriverPath(Capabilities options, boolean offline) {
LOG.fine("Applicable driver not found; attempting to install with Selenium Manager (Beta)");
File binaryFile = getBinary();
if (binaryFile == null) {
Expand Down Expand Up @@ -251,6 +251,10 @@ public String getDriverPath(Capabilities options) {
commandList.add("--debug");
}

if (offline) {
commandList.add("--offline");
}

Proxy proxy = (Proxy) options.getCapability("proxy");
if (proxy != null) {
if (proxy.getSslProxy() != null) {
Expand Down
9 changes: 6 additions & 3 deletions java/src/org/openqa/selenium/remote/service/DriverFinder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.openqa.selenium.remote.service;

import java.io.File;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManager;
Expand All @@ -10,18 +9,22 @@
public class DriverFinder {

public static String getPath(DriverService service, Capabilities options) {
return getPath(service, options, false);
}

public static String getPath(DriverService service, Capabilities options, boolean offline) {
Require.nonNull("Browser options", options);
String exePath = System.getProperty(service.getDriverProperty());

if (exePath == null) {
try {
exePath = SeleniumManager.getInstance().getDriverPath(options);
exePath = SeleniumManager.getInstance().getDriverPath(options, offline);
} catch (Exception e) {
throw new NoSuchDriverException(String.format("Unable to obtain: %s", options), e);
}
}

String message = "";
String message;
if (exePath == null) {
message = String.format("Unable to locate or obtain %s", service.getDriverName());
} else if (!new File(exePath).exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ public void setExecutable(String executable) {
this.executable = executable;
}

protected static String findExePath(String exeName, String exeProperty) {
String defaultPath = new ExecutableFinder().find(exeName);
return System.getProperty(exeProperty, defaultPath);
}

protected List<String> getArgs() {
return args;
}
Expand Down Expand Up @@ -465,9 +460,6 @@ protected OutputStream getLogOutput(String logProperty) {
}

if (logLocation == null) {
LOG.info(
"Driver logs no longer sent to console by default; "
+ "https://www.selenium.dev/documentation/webdriver/drivers/service/#setting-log-output");
return ByteStreams.nullOutputStream();
}

Expand Down
7 changes: 6 additions & 1 deletion java/src/org/openqa/selenium/safari/SafariDriverInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return SafariDriverService.isPresent();
try {
DriverFinder.getPath(SafariDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
Expand Down
11 changes: 0 additions & 11 deletions java/src/org/openqa/selenium/safari/SafariDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,6 @@ public static SafariDriverService createDefaultService() {
return new Builder().build();
}

/**
* Checks if the SafariDriver binary is available. Grid uses this method to show the available
* browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(SAFARI_DRIVER_EXECUTABLE.getAbsolutePath(), SAFARI_DRIVER_EXE_PROPERTY)
!= null;
}

@Override
protected void waitUntilAvailable() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ public boolean isAvailable() {

@Override
public boolean isPresent() {
return SafariTechPreviewDriverService.isPresent();
try {
DriverFinder.getPath(
SafariTechPreviewDriverService.createDefaultService(), getCanonicalCapabilities(), true);
return true;
} catch (IllegalStateException | WebDriverException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ public static SafariTechPreviewDriverService createDefaultService() {
return new Builder().build();
}

/**
* Checks if the SafariDriver driver binary is available. Grid uses this method to show the
* available browsers and drivers, hence its visibility.
*
* @return Whether the browser driver path was found.
*/
static boolean isPresent() {
return findExePath(TP_SAFARI_DRIVER_EXECUTABLE.getAbsolutePath(), TP_SAFARI_DRIVER_EXE_PROPERTY)
!= null;
}

@Override
protected void waitUntilAvailable() {
try {
Expand Down

0 comments on commit 0013140

Please sign in to comment.