41
41
import java .util .Objects ;
42
42
import java .util .Optional ;
43
43
import java .util .Set ;
44
+ import java .util .function .Function ;
44
45
import java .util .function .Supplier ;
45
46
import java .util .logging .Level ;
46
47
import java .util .stream .Stream ;
47
48
48
49
public class WebDriverBuilder implements Supplier <WebDriver > {
49
50
50
- private static LinkedList <Runnable > shutdownActions = new LinkedList <>();
51
- private static Set <WebDriver > managedDrivers = new HashSet <>();
51
+ private static final LinkedList <Runnable > shutdownActions = new LinkedList <>();
52
+ private static final Set <WebDriver > managedDrivers = new HashSet <>();
52
53
static {
53
54
shutdownActions .add (() -> managedDrivers .forEach (WebDriver ::quit ));
54
55
Runtime .getRuntime ().addShutdownHook (new Thread (() -> shutdownActions .forEach (Runnable ::run )));
@@ -58,16 +59,19 @@ static void addShutdownAction(Runnable action) {
58
59
shutdownActions .add (action );
59
60
}
60
61
61
- private static Map <Browser , Supplier <Capabilities >> capabilitySuppliers =
62
- new ImmutableMap .Builder <Browser , Supplier <Capabilities >>()
63
- .put (Browser .CHROME , ChromeOptions ::new )
64
- .put (Browser .FIREFOX , () -> new FirefoxOptions ()
65
- .setLegacy (true )
66
- .setHeadless (Boolean .parseBoolean (System .getProperty ("webdriver.firefox.headless" , "false" ))))
67
- .put (Browser .MARIONETTE , () -> new FirefoxOptions ()
68
- .setHeadless (Boolean .parseBoolean (System .getProperty ("webdriver.firefox.headless" , "false" ))))
69
- .put (Browser .IE , () -> {
70
- InternetExplorerOptions options = new InternetExplorerOptions ();
62
+ private static final Map <Browser , Function <Capabilities , Capabilities >> capabilitySuppliers =
63
+ new ImmutableMap .Builder <Browser , Function <Capabilities , Capabilities >>()
64
+ .put (Browser .CHROME , original -> new ChromeOptions ().merge (original ))
65
+ .put (Browser .FIREFOX , original -> new FirefoxOptions (original )
66
+ .setLegacy (true )
67
+ .setHeadless (Boolean .parseBoolean (System .getProperty ("webdriver.firefox.headless" , "false" ))))
68
+ .put (Browser .MARIONETTE , original -> new FirefoxOptions (original )
69
+ .setHeadless (
70
+ Boolean .parseBoolean (System .getProperty ("webdriver.firefox.headless" , "false" )) ||
71
+ Boolean .parseBoolean (System .getenv ("GITHUB_ACTIONS" )))
72
+ )
73
+ .put (Browser .IE , original -> {
74
+ InternetExplorerOptions options = new InternetExplorerOptions (original );
71
75
if (Boolean .getBoolean ("selenium.ie.disable_native_events" )) {
72
76
options .disableNativeEvents ();
73
77
}
@@ -76,12 +80,12 @@ static void addShutdownAction(Runnable action) {
76
80
}
77
81
return options ;
78
82
})
79
- .put (Browser .CHROMIUMEDGE , EdgeOptions :: new )
80
- .put (Browser .EDGE , EdgeHtmlOptions :: new )
81
- .put (Browser .HTMLUNIT , () -> new DesiredCapabilities (BrowserType .HTMLUNIT , "" , Platform .ANY ))
82
- .put (Browser .OPERABLINK , OperaOptions :: new )
83
- .put (Browser .SAFARI , () -> {
84
- SafariOptions options = new SafariOptions ();
83
+ .put (Browser .CHROMIUMEDGE , original -> new EdgeOptions (). merge ( original ) )
84
+ .put (Browser .EDGE , original -> new EdgeHtmlOptions (). merge ( original ) )
85
+ .put (Browser .HTMLUNIT , original -> new DesiredCapabilities (BrowserType .HTMLUNIT , "" , Platform .ANY ). merge ( original ))
86
+ .put (Browser .OPERABLINK , original -> new OperaOptions (). merge ( original ) )
87
+ .put (Browser .SAFARI , original -> {
88
+ SafariOptions options = new SafariOptions (original );
85
89
if (Boolean .getBoolean ("selenium.safari.tp" )) {
86
90
options .setUseTechnologyPreview (true );
87
91
}
@@ -90,7 +94,7 @@ static void addShutdownAction(Runnable action) {
90
94
.build ();
91
95
92
96
public static Capabilities getStandardCapabilitiesFor (Browser toBuild ) {
93
- return capabilitySuppliers .getOrDefault (toBuild , ImmutableCapabilities ::new ).get ( );
97
+ return capabilitySuppliers .getOrDefault (toBuild , ImmutableCapabilities ::new ).apply ( new ImmutableCapabilities () );
94
98
}
95
99
96
100
private final Browser toBuild ;
@@ -109,15 +113,16 @@ public WebDriver get() {
109
113
}
110
114
111
115
public WebDriver get (Capabilities desiredCapabilities ) {
112
- Capabilities desiredCaps = getStandardCapabilitiesFor (toBuild ).merge (desiredCapabilities );
116
+ Capabilities desiredCaps = capabilitySuppliers
117
+ .getOrDefault (toBuild , ImmutableCapabilities ::new )
118
+ .apply (desiredCapabilities );
113
119
114
120
WebDriver driver =
115
- Stream .of (
116
- new ExternalDriverSupplier (desiredCaps ),
117
- new GridSupplier (desiredCaps ),
118
- new RemoteSupplier (desiredCaps ),
119
- new TestInternetExplorerSupplier (desiredCaps ),
120
- new DefaultDriverSupplier (desiredCaps ))
121
+ Stream .of (new ExternalDriverSupplier (desiredCaps ),
122
+ new GridSupplier (desiredCaps ),
123
+ new RemoteSupplier (desiredCaps ),
124
+ new TestInternetExplorerSupplier (desiredCaps ),
125
+ new DefaultDriverSupplier (desiredCaps ))
121
126
.map (Supplier ::get )
122
127
.filter (Objects ::nonNull )
123
128
.findFirst ()
0 commit comments