Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions java/src/org/openqa/selenium/chrome/ChromeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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 @@ -95,8 +96,9 @@ private static ChromeDriverCommandExecutor generateExecutor(
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
if (service.getExecutable() == null) {
String path = DriverFinder.getPath(service, options);
service.setExecutable(path);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
options.setBinary(result.getBrowserPath());
}
return new ChromeDriverCommandExecutor(service, clientConfig);
}
Expand Down
7 changes: 5 additions & 2 deletions java/src/org/openqa/selenium/edge/EdgeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput;
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 @@ -67,8 +69,9 @@ private static EdgeDriverCommandExecutor generateExecutor(
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
if (service.getExecutable() == null) {
String path = DriverFinder.getPath(service, options);
service.setExecutable(path);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
options.setBinary(result.getBrowserPath());
}
return new EdgeDriverCommandExecutor(service, clientConfig);
}
Expand Down
7 changes: 5 additions & 2 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.html5.WebStorage;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManagerOutput;
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 @@ -137,8 +139,9 @@ private static FirefoxDriverCommandExecutor generateExecutor(
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
if (service.getExecutable() == null) {
String path = DriverFinder.getPath(service, options);
service.setExecutable(path);
Result result = DriverFinder.getPath(service, options);
service.setExecutable(result.getDriverPath());
options.setBinary(result.getBrowserPath());
}
return new FirefoxDriverCommandExecutor(service, clientConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/grid/node/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ java_library(
],
deps = [
"//java:auto-service",
"//java/src/org/openqa/selenium/chromium",
"//java/src/org/openqa/selenium/grid/config",
"//java/src/org/openqa/selenium/grid/data",
"//java/src/org/openqa/selenium/grid/node",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/manager",
"//java/src/org/openqa/selenium/remote",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -48,6 +50,7 @@
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 All @@ -58,6 +61,7 @@
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.service.DriverFinder;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.remote.tracing.AttributeKey;
import org.openqa.selenium.remote.tracing.EventAttribute;
Expand Down Expand Up @@ -134,6 +138,13 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
AttributeKey.LOGGER_CLASS.getKey(), EventAttribute.setValue(this.getClass().getName()));

DriverService service = builder.build();
if (service.getExecutable() == null) {
Result result = DriverFinder.getPath(service, capabilities);
service.setExecutable(result.getDriverPath());
if (result.getBrowserPath() != null) {
capabilities = setBrowserBinary(capabilities, result.getBrowserPath());
}
}
try {
service.start();

Expand Down Expand Up @@ -327,4 +338,27 @@ private String getHost() {
return HostIdentifier.getHostName();
}
}

private Capabilities setBrowserBinary(Capabilities options, String browserPath) {
List<String> vendorOptionsCapabilities =
Arrays.asList("moz:firefoxOptions", "goog:chromeOptions", "ms:edgeOptions");
for (String vendorOptionsCapability : vendorOptionsCapabilities) {
if (options.asMap().containsKey(vendorOptionsCapability)) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> vendorOptions =
(Map<String, Object>) options.getCapability(vendorOptionsCapability);
vendorOptions.put("binary", browserPath);
return new PersistentCapabilities(options)
.setCapability(vendorOptionsCapability, vendorOptions);
} catch (Exception e) {
LOG.warning(
String.format(
"Exception while setting the browser binary path. %s: %s",
options, e.getMessage()));
}
}
}
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public InternetExplorerDriver(
service = InternetExplorerDriverService.createDefaultService();
}
if (service.getExecutable() == null) {
String path = DriverFinder.getPath(service, options);
String path = DriverFinder.getPath(service, options).getDriverPath();
service.setExecutable(path);
}
if (clientConfig == null) {
Expand Down
22 changes: 13 additions & 9 deletions java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;

/**
* This implementation is still in beta, and may change.
Expand Down Expand Up @@ -100,7 +101,7 @@ public static SeleniumManager getInstance() {
* @param command the file and arguments to execute.
* @return the standard output of the execution.
*/
private static String runCommand(String... command) {
private static Result runCommand(String... command) {
LOG.fine(String.format("Executing Process: %s", Arrays.toString(command)));
String output;
int code;
Expand All @@ -118,12 +119,12 @@ private static String runCommand(String... command) {
} catch (Exception e) {
throw new WebDriverException("Failed to run command: " + Arrays.toString(command), e);
}
SeleniumManagerJsonOutput jsonOutput = null;
SeleniumManagerOutput jsonOutput = null;
JsonException failedToParse = null;
String dump = output;
if (!output.isEmpty()) {
try {
jsonOutput = new Json().toType(output, SeleniumManagerJsonOutput.class);
jsonOutput = new Json().toType(output, SeleniumManagerOutput.class);
jsonOutput.logs.forEach(
logged -> {
if (logged.level.equalsIgnoreCase(WARN)) {
Expand All @@ -147,12 +148,12 @@ private static String runCommand(String... command) {
+ "\n"
+ dump,
failedToParse);
} else if (failedToParse != null) {
} else if (failedToParse != null || jsonOutput == null) {
throw new WebDriverException(
"Failed to parse json output, executed: " + Arrays.toString(command) + "\n" + dump,
failedToParse);
}
return jsonOutput.result.message;
return jsonOutput.result;
}

/**
Expand Down Expand Up @@ -223,7 +224,7 @@ private String getBrowserBinary(Capabilities options) {
* @param options Browser Options instance.
* @return the location of the driver.
*/
public String getDriverPath(Capabilities options, boolean offline) {
public Result getDriverPath(Capabilities options, boolean offline) {
File binaryFile = getBinary();
if (binaryFile == null) {
return null;
Expand Down Expand Up @@ -265,9 +266,12 @@ public String getDriverPath(Capabilities options, boolean offline) {
}
}

String path = runCommand(commandList.toArray(new String[0]));
LOG.fine(String.format("Using driver at location: %s", path));
return path;
Result result = runCommand(commandList.toArray(new String[0]));
LOG.fine(
String.format(
"Using driver at location: %s, browser at location %s",
result.getDriverPath(), result.getBrowserPath()));
return result;
}

private Level getLogLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
// under the License.
package org.openqa.selenium.manager;

import org.openqa.selenium.json.JsonInput;

import java.util.List;

public class SeleniumManagerJsonOutput {
public class SeleniumManagerOutput {

public List<Log> logs;
public Result result;
Expand Down Expand Up @@ -72,6 +74,19 @@ public void setMessage(String message) {
public static class Result {
public int code;
public String message;
public String driverPath;
public String browserPath;

public Result(String driverPath) {
this.driverPath = driverPath;
}

public Result(int code, String message, String driverPath, String browserPath) {
this.code = code;
this.message = message;
this.driverPath = driverPath;
this.browserPath = browserPath;
}

public int getCode() {
return code;
Expand All @@ -88,5 +103,56 @@ public String getMessage() {
public void setMessage(String message) {
this.message = message;
}

public String getDriverPath() {
return driverPath;
}

public void setDriverPath(String driverPath) {
this.driverPath = driverPath;
}

public String getBrowserPath() {
return browserPath;
}

public void setBrowserPath(String browserPath) {
this.browserPath = browserPath;
}

public static Result fromJson(JsonInput input) {
int code = 0;
String message = null;
String driverPath = null;
String browserPath = null;

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "code":
code = input.read(Integer.class);
break;

case "message":
message = input.read(String.class);
break;

case "driver_path":
driverPath = input.read(String.class);
break;

case "browser_path":
browserPath = input.read(String.class);
break;

default:
input.skipValue();
break;
}
}
input.endObject();

return new Result(code, message, driverPath, browserPath);
}
}
}
23 changes: 12 additions & 11 deletions java/src/org/openqa/selenium/remote/service/DriverFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.manager.SeleniumManager;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.remote.NoSuchDriverException;

public class DriverFinder {

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

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

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

String message;
if (exePath == null) {
if (result.getDriverPath() == null) {
message = String.format("Unable to locate or obtain %s", service.getDriverName());
} else if (!new File(exePath).exists()) {
message = String.format("%s located at %s, but invalid", service.getDriverName(), exePath);
} else if (!new File(exePath).canExecute()) {
} else if (!new File(result.getDriverPath()).exists()) {
message = String.format("%s located at %s, but invalid", service.getDriverName(), result.getDriverPath());
} else if (!new File(result.getDriverPath()).canExecute()) {
message =
String.format("%s located at %s, cannot be executed", service.getDriverName(), exePath);
String.format("%s located at %s, cannot be executed", service.getDriverName(), result.getDriverPath());
} else {
return exePath;
return result;
}

throw new NoSuchDriverException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,6 @@ public void start() throws IOException {
if (process != null) {
return;
}
if (this.executable == null) {
if (getDefaultDriverOptions().getBrowserName().isEmpty()) {
throw new WebDriverException("Driver executable is null and browser name is not set.");
}
this.executable = DriverFinder.getPath(this, getDefaultDriverOptions());
}
LOG.fine(String.format("Starting driver at %s with %s", this.executable, this.args));
process = new CommandLine(this.executable, args.toArray(new String[] {}));
process.setEnvironmentVariables(environment);
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/safari/SafariDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static SafariDriverCommandExecutor generateExecutor(
Require.nonNull("Driver options", options);
Require.nonNull("Driver clientConfig", clientConfig);
if (service.getExecutable() == null) {
String path = DriverFinder.getPath(service, options);
String path = DriverFinder.getPath(service, options).getDriverPath();
service.setExecutable(path);
}
return new SafariDriverCommandExecutor(service, clientConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void shouldBeAbleToReturnWebElementsFromAsyncScripts() {
}

@Test
@Ignore(value = CHROME, reason = "https://bugs.chromium.org/p/chromedriver/issues/detail?id=4525")
void shouldBeAbleToReturnArraysOfWebElementsFromAsyncScripts() {
driver.get(pages.ajaxyPage);

Expand Down
Loading