Skip to content

Commit 4d74cf4

Browse files
committed
[bazel] Add a flag to allow us to run tests headlessly
1 parent 42da7d5 commit 4d74cf4

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
try-import .bazelrc.local
22

33
build --flag_alias=pin_browsers=//common:pin_browsers
4+
build --flag_alias=headless=//common:headless
45

56
# Set the default java toolchain
67
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11

common/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ config_setting(
1414
},
1515
)
1616

17+
bool_flag(
18+
name = "headless",
19+
build_setting_default = False,
20+
)
21+
22+
config_setting(
23+
name = "use_headless_browser",
24+
flag_values = {
25+
":headless": "true",
26+
},
27+
)
28+
29+
1730
config_setting(
1831
name = "linux",
1932
constraint_values = ["@platforms//os:linux"],

java/browsers.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ chrome_jvm_flags = select({
2626
"//conditions:default": [
2727
"-Dselenium.skiptest=true",
2828
],
29+
}) + select({
30+
"//common:use_headless_browser": [
31+
"-Dwebdriver.headless=true",
32+
],
33+
"//conditions:default": [],
2934
})
3035

3136
edge_data = select({
@@ -48,6 +53,11 @@ edge_jvm_flags = select({
4853
"//conditions:default": [
4954
"-Dselenium.skiptest=true",
5055
],
56+
}) + select({
57+
"//common:use_headless_browser": [
58+
"-Dwebdriver.headless=true",
59+
],
60+
"//conditions:default": [],
5161
})
5262

5363
firefox_data = select({
@@ -78,4 +88,9 @@ firefox_jvm_flags = select({
7888
"//conditions:default": [
7989
"-Dselenium.skiptest=true",
8090
],
91+
}) + select({
92+
"//common:use_headless_browser": [
93+
"-Dwebdriver.headless=true",
94+
],
95+
"//conditions:default": [],
8196
})

java/client/test/org/openqa/selenium/testing/drivers/Browser.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,35 @@ public enum Browser {
4343
CHROME(new ChromeOptions(), new ChromeDriverInfo().getDisplayName(), true) {
4444
@Override
4545
public Capabilities getCapabilities() {
46+
ChromeOptions options = new ChromeOptions();
47+
4648
String binary = System.getProperty("webdriver.chrome.binary");
49+
if (binary != null) {
50+
options.setBinary(binary);
51+
}
52+
53+
if (Boolean.getBoolean("webdriver.headless")) {
54+
options.setHeadless(true);
55+
}
4756

48-
return binary == null ? new ChromeOptions() : new ChromeOptions().setBinary(binary);
57+
return options;
4958
}
5059
},
5160
EDGE(new EdgeOptions(), new EdgeDriverInfo().getDisplayName(), true) {
5261
@Override
5362
public Capabilities getCapabilities() {
63+
EdgeOptions options = new EdgeOptions();
64+
5465
String binary = System.getProperty("webdriver.edge.binary");
66+
if (binary != null) {
67+
options.setBinary(binary);
68+
}
5569

56-
return binary == null ? new EdgeOptions() : new EdgeOptions().setBinary(binary);
70+
if (Boolean.getBoolean("webdriver.headless")) {
71+
options.setHeadless(true);
72+
}
73+
74+
return options;
5775
}
5876
},
5977
HTMLUNIT(new ImmutableCapabilities(BROWSER_NAME, BrowserType.HTMLUNIT), "HtmlUnit", false),
@@ -62,9 +80,18 @@ public Capabilities getCapabilities() {
6280
FIREFOX(new FirefoxOptions(), new GeckoDriverInfo().getDisplayName(), false) {
6381
@Override
6482
public Capabilities getCapabilities() {
83+
FirefoxOptions options = new FirefoxOptions();
84+
6585
String binary = System.getProperty("webdriver.firefox.bin");
86+
if (binary != null) {
87+
options.setBinary(binary);
88+
}
89+
90+
if (Boolean.getBoolean("webdriver.headless")) {
91+
options.setHeadless(true);
92+
}
6693

67-
return binary == null ? new FirefoxOptions() : new FirefoxOptions().setBinary(binary);
94+
return options;
6895
}
6996
},
7097
LEGACY_OPERA(new OperaOptions(), new OperaDriverInfo().getDisplayName(), false),

0 commit comments

Comments
 (0)