Skip to content

[java] Use Environment variable to set driver location #15653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ maven.install(
"org.slf4j:slf4j-jdk14:2.0.17",
"org.tomlj:tomlj:1.1.1",
"org.zeromq:jeromq:0.6.0",
"uk.org.webcompere:system-stubs-jupiter:2.1.8",
"uk.org.webcompere:system-stubs-core:2.1.8",
],
boms = [
"io.opentelemetry:opentelemetry-bom:1.46.0",
Expand Down
51 changes: 45 additions & 6 deletions java/maven_install.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": 868628742,
"__RESOLVED_ARTIFACTS_HASH": -933630985,
"__INPUT_ARTIFACTS_HASH": 1624456402,
"__RESOLVED_ARTIFACTS_HASH": 784399109,
"artifacts": {
"com.beust:jcommander": {
"shasums": {
Expand Down Expand Up @@ -452,10 +452,10 @@
},
"net.bytebuddy:byte-buddy-agent": {
"shasums": {
"jar": "316d2c0795c2a4d4c4756f2e6f9349837c7430ac34e0477ead874d05f5cc19e5",
"sources": "a73d2869afd79b63396a0a07ac037bbfa184eb7306916343c030feb5335ac940"
"jar": "e1dae7efc5562c29ad3b625b90e6864208de69bad5632c3f93a547f17622ac51",
"sources": "57b0ee93eab8caa3cfc424aa24616462543bb2a7f13848cbb537c225314dffaf"
},
"version": "1.15.11"
"version": "1.17.4"
},
"net.sf.saxon:Saxon-HE": {
"shasums": {
Expand Down Expand Up @@ -793,6 +793,20 @@
"sources": "c20e579710d385869496d25217a7820665f7687dc1499b21be6e0ac7e9485a03"
},
"version": "0.6.0"
},
"uk.org.webcompere:system-stubs-core": {
"shasums": {
"jar": "9c27322cfc7043c75384ad444007b0880ca18fe7231d69bfa69616bc773cafe1",
"sources": "6e6c4adf9094e0200400a5d9f8086c8a1c4a0940919fe0b30d30543b483d5687"
},
"version": "2.1.8"
},
"uk.org.webcompere:system-stubs-jupiter": {
"shasums": {
"jar": "9a24867a51f5d22db67d9052a06bc5dd2e9a3e273bc2ee9814620f2d9f25d0a8",
"sources": "208bfe2907ab022fe206402f2ef6b1ea445f8240f072a14a49e338c1448fb2b6"
},
"version": "2.1.8"
}
},
"dependencies": {
Expand Down Expand Up @@ -1125,6 +1139,13 @@
],
"org.zeromq:jeromq": [
"eu.neilalexander:jnacl"
],
"uk.org.webcompere:system-stubs-core": [
"net.bytebuddy:byte-buddy",
"net.bytebuddy:byte-buddy-agent"
],
"uk.org.webcompere:system-stubs-jupiter": [
"uk.org.webcompere:system-stubs-core"
]
},
"packages": {
Expand Down Expand Up @@ -2890,6 +2911,20 @@
"zmq.socket.scattergather",
"zmq.util",
"zmq.util.function"
],
"uk.org.webcompere:system-stubs-core": [
"uk.org.webcompere.systemstubs",
"uk.org.webcompere.systemstubs.environment",
"uk.org.webcompere.systemstubs.exception",
"uk.org.webcompere.systemstubs.properties",
"uk.org.webcompere.systemstubs.resource",
"uk.org.webcompere.systemstubs.security",
"uk.org.webcompere.systemstubs.stream",
"uk.org.webcompere.systemstubs.stream.input",
"uk.org.webcompere.systemstubs.stream.output"
],
"uk.org.webcompere:system-stubs-jupiter": [
"uk.org.webcompere.systemstubs.jupiter"
]
},
"repositories": {
Expand Down Expand Up @@ -3119,7 +3154,11 @@
"org.yaml:snakeyaml",
"org.yaml:snakeyaml:jar:sources",
"org.zeromq:jeromq",
"org.zeromq:jeromq:jar:sources"
"org.zeromq:jeromq:jar:sources",
"uk.org.webcompere:system-stubs-core",
"uk.org.webcompere:system-stubs-core:jar:sources",
"uk.org.webcompere:system-stubs-jupiter",
"uk.org.webcompere:system-stubs-jupiter:jar:sources"
]
},
"version": "2"
Expand Down
6 changes: 6 additions & 0 deletions java/src/org/openqa/selenium/chrome/ChromeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class ChromeDriverService extends DriverService {
*/
public static final String CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver";

public static final String CHROME_DRIVER_EXE_ENVIRONMENT_VARIABLE = "SE_CHROMEDRIVER";

/** System property that toggles the formatting of the timestamps of the logs */
public static final String CHROME_DRIVER_READABLE_TIMESTAMP =
"webdriver.chrome.readableTimestamp";
Expand Down Expand Up @@ -122,6 +124,10 @@ public String getDriverProperty() {
return CHROME_DRIVER_EXE_PROPERTY;
}

public String getDriverEnvironmentVariable() {
return CHROME_DRIVER_EXE_ENVIRONMENT_VARIABLE;
}

@Override
public Capabilities getDefaultDriverOptions() {
return new ChromeOptions();
Expand Down
6 changes: 6 additions & 0 deletions java/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class EdgeDriverService extends DriverService {
*/
public static final String EDGE_DRIVER_EXE_PROPERTY = "webdriver.edge.driver";

public static final String EDGE_DRIVER_EXE_ENVIRONMENT_VARIABLE = "SE_EDGEDRIVER";

/** System property that toggles the formatting of the timestamps of the logs */
public static final String EDGE_DRIVER_READABLE_TIMESTAMP = "webdriver.edge.readableTimestamp";

Expand Down Expand Up @@ -111,6 +113,10 @@ public String getDriverProperty() {
return EDGE_DRIVER_EXE_PROPERTY;
}

public String getDriverEnvironmentVariable() {
return EDGE_DRIVER_EXE_ENVIRONMENT_VARIABLE;
}

@Override
public Capabilities getDefaultDriverOptions() {
return new EdgeOptions();
Expand Down
6 changes: 6 additions & 0 deletions java/src/org/openqa/selenium/firefox/GeckoDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class GeckoDriverService extends FirefoxDriverService {
*/
public static final String GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver";

public static final String GECKO_DRIVER_EXE_ENVIRONMENT_VARIABLE = "SE_GECKODRIVER";

/**
* System property that defines the location of the file where GeckoDriver should write log
* messages to.
Expand Down Expand Up @@ -103,6 +105,10 @@ public String getDriverProperty() {
return GECKO_DRIVER_EXE_PROPERTY;
}

public String getDriverEnvironmentVariable() {
return GECKO_DRIVER_EXE_ENVIRONMENT_VARIABLE;
}

@Override
public Capabilities getDefaultDriverOptions() {
return new FirefoxOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class InternetExplorerDriverService extends DriverService {
*/
public static final String IE_DRIVER_EXE_PROPERTY = "webdriver.ie.driver";

public static final String IE_DRIVER_EXE_ENVIRONMENT_VARIABLE = "SE_IEDRIVER";

/**
* System property that defines the location of the file where IEDriverServer should write log
* messages to.
Expand Down Expand Up @@ -98,6 +100,10 @@ public String getDriverProperty() {
return IE_DRIVER_EXE_PROPERTY;
}

public String getDriverEnvironmentVariable() {
return IE_DRIVER_EXE_ENVIRONMENT_VARIABLE;
}

@Override
public Capabilities getDefaultDriverOptions() {
return new InternetExplorerOptions();
Expand Down
23 changes: 13 additions & 10 deletions java/src/org/openqa/selenium/remote/service/DriverFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,20 @@ private Result getBinaryPaths() {
String driverName = service.getDriverName();
result = new Result(service.getExecutable());
if (result.getDriverPath() == null) {
result = new Result(System.getProperty(service.getDriverProperty()));
result = new Result(System.getenv(service.getDriverEnvironmentVariable()));
if (result.getDriverPath() == null) {
List<String> arguments = toArguments();
result = seleniumManager.getBinaryPaths(arguments);
Require.state(options.getBrowserName(), Path.of(result.getBrowserPath()))
.isExecutable();
} else {
LOG.fine(
String.format(
"Skipping Selenium Manager, path to %s found in system property: %s",
driverName, result.getDriverPath()));
result = new Result(System.getProperty(service.getDriverProperty()));
if (result.getDriverPath() == null) {
List<String> arguments = toArguments();
result = seleniumManager.getBinaryPaths(arguments);
Require.state(options.getBrowserName(), Path.of(result.getBrowserPath()))
.isExecutable();
} else {
LOG.fine(
String.format(
"Skipping Selenium Manager, path to %s found in system property: %s",
driverName, result.getDriverPath()));
}
}
} else {
LOG.fine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public String getDriverProperty() {
return null;
}

public String getDriverEnvironmentVariable() {
return null;
}

protected File getDriverExecutable() {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions java/src/org/openqa/selenium/safari/SafariDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class SafariDriverService extends DriverService {
*/
public static final String SAFARI_DRIVER_EXE_PROPERTY = "webdriver.safari.driver";

public static final String SAFARI_DRIVER_EXE_ENVIRONMENT_VARIABLE = "SE_SAFARIDRIVER";

public static final String SAFARI_DRIVER_LOGGING = "webdriver.safari.logging";

private static final File SAFARI_DRIVER_EXECUTABLE = new File("/usr/bin/safaridriver");
Expand Down Expand Up @@ -84,6 +86,10 @@ public String getDriverProperty() {
return SAFARI_DRIVER_EXE_PROPERTY;
}

public String getDriverEnvironmentVariable() {
return SAFARI_DRIVER_EXE_ENVIRONMENT_VARIABLE;
}

public File getDriverExecutable() {
return SAFARI_DRIVER_EXECUTABLE;
}
Expand Down
2 changes: 2 additions & 0 deletions java/test/org/openqa/selenium/remote/service/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ java_test_suite(
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
artifact("org.mockito:mockito-core"),
artifact("uk.org.webcompere:system-stubs-jupiter"),
artifact("uk.org.webcompere:system-stubs-core"),
] + JUNIT5_DEPS,
)
68 changes: 68 additions & 0 deletions java/test/org/openqa/selenium/remote/service/DriverFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.manager.SeleniumManager;
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

@Tag("UnitTests")
@ExtendWith(SystemStubsExtension.class)
class DriverFinderTest {
private final DriverService service = mock(DriverService.class);
private final SeleniumManager seleniumManager = mock(SeleniumManager.class);
Path driverFile;
Path browserFile;

@SystemStub private EnvironmentVariables environment;

@BeforeEach
void createMocks() {
driverFile = createExecutableFile("testDriver");
Expand All @@ -73,6 +80,63 @@ void serviceValueIgnoresSeleniumManager() {
void systemPropertyIgnoresSeleniumManager() throws IOException {
when(service.getExecutable()).thenReturn(null);
when(service.getDriverProperty()).thenReturn("property.ignores.selenium.manager");
when(service.getDriverEnvironmentVariable())
.thenReturn("ENVIRONMENT_VARIABLE_IGNORES_SELENIUM_MANAGER");
System.setProperty("property.ignores.selenium.manager", driverFile.toString());

Capabilities capabilities = new ImmutableCapabilities("browserName", "chrome");
DriverFinder finder = new DriverFinder(service, capabilities);

assertThat(finder.getDriverPath()).isEqualTo(driverFile.toString());
assertThat(finder.getBrowserPath()).isNull();
verify(service, times(1)).getExecutable();
verify(service, times(1)).getDriverName();
verify(service, times(1)).getDriverProperty();
}

@Test
void environmentVariableIgnoresSeleniumManager() throws IOException {
environment.set("ENVIRONMENT_VARIABLE_DRIVER_PATH", driverFile.toString());
when(service.getExecutable()).thenReturn(null);
when(service.getDriverProperty()).thenReturn("property.ignores.selenium.manager");
when(service.getDriverEnvironmentVariable()).thenReturn("ENVIRONMENT_VARIABLE_DRIVER_PATH");

Capabilities capabilities = new ImmutableCapabilities("browserName", "chrome");
DriverFinder finder = new DriverFinder(service, capabilities);

assertThat(finder.getDriverPath()).isEqualTo(driverFile.toString());
assertThat(finder.getBrowserPath()).isNull();
verify(service, times(1)).getExecutable();
verify(service, times(1)).getDriverName();
verify(service, times(1)).getDriverEnvironmentVariable();
}

@Test
void environmentVariableTakePriorityOverSystemProperty() throws IOException {
environment.set("ENVIRONMENT_VARIABLE_DRIVER_PATH", driverFile.toString());
when(service.getExecutable()).thenReturn(null);
when(service.getDriverProperty()).thenReturn("property.ignores.selenium.manager");
when(service.getDriverEnvironmentVariable()).thenReturn("ENVIRONMENT_VARIABLE_DRIVER_PATH");

System.setProperty("property.ignores.selenium.manager", "path");

Capabilities capabilities = new ImmutableCapabilities("browserName", "chrome");
DriverFinder finder = new DriverFinder(service, capabilities);

assertThat(finder.getDriverPath()).isEqualTo(driverFile.toString());
assertThat(finder.getBrowserPath()).isNull();
verify(service, times(1)).getExecutable();
verify(service, times(1)).getDriverName();
verify(service, times(1)).getDriverEnvironmentVariable();
}

@Test
void systemPropertyIsUsedIfEnvironmentVariableIsNotSet() throws IOException {
when(service.getExecutable()).thenReturn(null);
when(service.getDriverProperty()).thenReturn("property.ignores.selenium.manager");
when(service.getDriverEnvironmentVariable())
.thenReturn("ENVIRONMENT_VARIABLE_IGNORES_SELENIUM_MANAGER");

System.setProperty("property.ignores.selenium.manager", driverFile.toString());

Capabilities capabilities = new ImmutableCapabilities("browserName", "chrome");
Expand All @@ -82,13 +146,16 @@ void systemPropertyIgnoresSeleniumManager() throws IOException {
assertThat(finder.getBrowserPath()).isNull();
verify(service, times(1)).getExecutable();
verify(service, times(1)).getDriverName();
verify(service, times(1)).getDriverEnvironmentVariable();
verify(service, times(1)).getDriverProperty();
}

@Test
void createsArgumentsForSeleniumManager() throws IOException {
when(service.getExecutable()).thenReturn(null);
when(service.getDriverProperty()).thenReturn("property.selenium.manager.empty");
when(service.getDriverEnvironmentVariable())
.thenReturn("ENVIRONMENT_VARIABLE_IGNORES_SELENIUM_MANAGER");

Proxy proxy = new Proxy().setHttpProxy("https://localhost:1234");
Capabilities capabilities =
Expand Down Expand Up @@ -120,6 +187,7 @@ void createsArgumentsForSeleniumManager() throws IOException {
verify(service, times(1)).getExecutable();
verify(service, times(1)).getDriverName();
verify(service, times(1)).getDriverProperty();
verify(service, times(1)).getDriverEnvironmentVariable();
verifyNoMoreInteractions(service);
verify(seleniumManager, times(1)).getBinaryPaths(arguments);
verifyNoMoreInteractions(seleniumManager);
Expand Down
Loading
Loading