1616
1717package io .github .seleniumquery .browser .driver .builders ;
1818
19+ import org .apache .commons .logging .Log ;
20+ import org .apache .commons .logging .LogFactory ;
1921import org .openqa .selenium .WebDriver ;
22+ import org .openqa .selenium .firefox .FirefoxBinary ;
2023import org .openqa .selenium .firefox .FirefoxDriver ;
24+ import org .openqa .selenium .firefox .FirefoxOptions ;
2125import org .openqa .selenium .firefox .FirefoxProfile ;
2226import org .openqa .selenium .remote .DesiredCapabilities ;
2327
3236 */
3337public class FirefoxDriverBuilder extends DriverBuilder <FirefoxDriverBuilder > {
3438
35- private FirefoxProfile firefoxProfile ;
39+ private static final Log LOGGER = LogFactory .getLog (FirefoxDriverBuilder .class );
40+
41+ private FirefoxOptions firefoxOptions ;
42+
43+ private FirefoxOptions getInitializedFirefoxOptions () {
44+ if (this .firefoxOptions == null ) {
45+ this .firefoxOptions = new FirefoxOptions ();
46+ }
47+ return this .firefoxOptions ;
48+ }
3649
3750 /**
38- * @deprecated Firefox (geckodriver) no longer supports disabling JavaScript. Without it, geckodriver simply
39- * can't communicate with Firefox.
40- *
41- * @return A self reference.
42- * @since 0.9.0
51+ * Configures the driver with the given capabilities.
52+ * @param desiredCapabilities The capabilities to be set.
53+ * @return A self reference for further configuration.
54+ * @since 0.18.0
4355 */
44- public FirefoxDriverBuilder withoutJavaScript () {
45- throw new SeleniumQueryException ("Firefox no longer supports disabling JavaScript. Without it, " +
46- "geckodriver simply can't communicate with Firefox." );
56+ @ Override
57+ public FirefoxDriverBuilder withCapabilities (DesiredCapabilities desiredCapabilities ) {
58+ getInitializedFirefoxOptions ().merge (desiredCapabilities );
59+ return this ;
4760 }
4861
4962 /**
5063 * Sets specific {@link FirefoxProfile} to be used in the {@link FirefoxDriver}.
5164 *
5265 * @param firefoxProfile Profile to be used.
5366 * @return A self reference, allowing further configuration.
54- *
5567 * @since 0.9.0
5668 */
5769 public FirefoxDriverBuilder withProfile (FirefoxProfile firefoxProfile ) {
58- this .firefoxProfile = firefoxProfile ;
70+ getInitializedFirefoxOptions ().setProfile (firefoxProfile );
71+ return this ;
72+ }
73+
74+ /**
75+ * <p>Sets specific {@link FirefoxOptions} to be used in the {@link FirefoxDriver}.</p>
76+ * <br>
77+ * This overwrites most configuration done by other options of driverbuilder. If you want to use it, it
78+ * is best to have it as first configuration of the driver builder, e.g.:
79+ * <pre><code>
80+ * // instead of
81+ * $.driver().useFirefox().withBinary(...).withCapabilities(...)<b>.withOptions(yourCustomOptions)</b>;
82+ * // do
83+ * $.driver().useFirefox()<b>.withOptions(yourCustomOptions)</b>.withBinary(...).withCapabilities(...);
84+ * </code></pre>
85+ *
86+ * @param firefoxOptions Options to be used.
87+ * @return A self reference, allowing further configuration.
88+ * @since 0.18.0
89+ */
90+ public FirefoxDriverBuilder withOptions (FirefoxOptions firefoxOptions ) {
91+ if (this .firefoxOptions != null ) {
92+ LOGGER .warn ("FirefoxOptions has already been initialized. All previous configurations are being overwritten." );
93+ }
94+ this .firefoxOptions = firefoxOptions ;
95+ return this ;
96+ }
97+
98+ /**
99+ * Sets specific {@link FirefoxBinary} to be used in the {@link FirefoxDriver}.
100+ *
101+ * @param firefoxBinary Binary to be used.
102+ * @return A self reference, allowing further configuration.
103+ * @since 0.18.0
104+ */
105+ public FirefoxDriverBuilder withBinary (FirefoxBinary firefoxBinary ) {
106+ getInitializedFirefoxOptions ().setBinary (firefoxBinary );
107+ return this ;
108+ }
109+
110+ /**
111+ * Configures {@link FirefoxDriver} to run in headless mode.
112+ *
113+ * @return A self reference, allowing further configuration.
114+ * @since 0.18.0
115+ */
116+ public FirefoxDriverBuilder headless () {
117+ getInitializedFirefoxOptions ().setHeadless (true );
59118 return this ;
60119 }
61120
@@ -68,19 +127,19 @@ protected WebDriver build() {
68127 }
69128
70129 private WebDriver buildFirefox () {
71- DesiredCapabilities capabilities = createConfiguredCapabilities ();
72- return new FirefoxDriver (capabilities );
130+ return new FirefoxDriver (getInitializedFirefoxOptions ());
73131 }
74132
75- private DesiredCapabilities createConfiguredCapabilities () {
76- DesiredCapabilities capabilities = capabilities (DesiredCapabilities .firefox ());
77- configureFirefoxProfile (capabilities );
78- return capabilities ;
79- }
80-
81- private void configureFirefoxProfile (DesiredCapabilities capabilities ) {
82- FirefoxProfile profile = this .firefoxProfile != null ? this .firefoxProfile : new FirefoxProfile ();
83- capabilities .setCapability (FirefoxDriver .PROFILE , profile );
133+ /**
134+ * @deprecated Firefox (geckodriver) no longer supports disabling JavaScript. Without it, geckodriver simply
135+ * can't communicate with Firefox.
136+ *
137+ * @return A self reference.
138+ * @since 0.9.0
139+ */
140+ public FirefoxDriverBuilder withoutJavaScript () {
141+ throw new SeleniumQueryException ("Firefox no longer supports disabling JavaScript. Without it, " +
142+ "geckodriver simply can't communicate with Firefox." );
84143 }
85144
86145}
0 commit comments