Skip to content

Commit 5bf50c4

Browse files
committed
[js] Extend firefox.Options for easily configuring Firefox to run in headless
mode. Fixes #4591
1 parent e4f03b9 commit 5bf50c4

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

javascript/node/selenium-webdriver/CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
### API Changes
1010

11-
* Added `selenium-webdriver/firefox.Options#addArguments()`
11+
* Added new methods to `selenium-webdriver/firefox.Options`:
12+
- addArguments()
13+
- headless()
14+
- windowSize()
1215
* Deprecated `selenium-webdriver/firefox/binary.Binary`
1316
* Removed `selenium-webdriver/firefox.Options#useGeckoDriver()`
1417
* Removed the unused `selenium-webdriver/firefox/profile.decode()`

javascript/node/selenium-webdriver/example/chrome_headless.js renamed to javascript/node/selenium-webdriver/example/headless.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@
1616
// under the License.
1717

1818
/**
19-
* @fileoverview An example of running Chrome in headless mode.
19+
* @fileoverview An example of running Chrome or Firefox in headless mode.
2020
*
21-
* Before running this script, ensure you have Chrome 59+ installed and that
21+
* To run with Chrome, ensure you have Chrome 59+ installed and that
2222
* chromedriver 2.30+ is present on your system PATH:
2323
* <https://sites.google.com/a/chromium.org/chromedriver/downloads>
2424
*
25-
* Usage:
26-
* node selenium-webdriver/example/chrome_headless.js
25+
* SELENIUM_BROWSER=chrome node selenium-webdriver/example/headless.js
26+
*
27+
* To run with Firefox, ensure you have Firefox 57+ installed and that
28+
* geckodriver 0.19.0+ is present on your system PATH:
29+
* <https://github.com/mozilla/geckodriver/releases>
30+
*
31+
* SELENIUM_BROWSER=firefox node selenium-webdriver/example/headless.js
2732
*/
2833

2934
const chrome = require('../chrome');
35+
const firefox = require('../firefox');
3036
const {Builder, By, Key, until} = require('..');
3137

3238
const width = 640;
@@ -36,6 +42,8 @@ let driver = new Builder()
3642
.forBrowser('chrome')
3743
.setChromeOptions(
3844
new chrome.Options().headless().windowSize({width, height}))
45+
.setFirefoxOptions(
46+
new firefox.Options().headless().windowSize({width, height}))
3947
.build();
4048

4149
driver.get('http://www.google.com/ncr')

javascript/node/selenium-webdriver/firefox/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,35 @@ class Options {
170170
return this;
171171
}
172172

173+
/**
174+
* Configures the geckodriver to start Firefox in headless mode.
175+
*
176+
* @return {!Options} A self reference.
177+
*/
178+
headless() {
179+
return this.addArguments('-headless');
180+
}
181+
182+
/**
183+
* Sets the initial window size when running in
184+
* {@linkplain #headless headless} mode.
185+
*
186+
* @param {{width: number, height: number}} size The desired window size.
187+
* @return {!Options} A self reference.
188+
* @throws {TypeError} if width or height is unspecified, not a number, or
189+
* less than or equal to 0.
190+
*/
191+
windowSize({width, height}) {
192+
function checkArg(arg) {
193+
if (typeof arg !== 'number' || arg <= 0) {
194+
throw TypeError('Arguments must be {width, height} with numbers > 0');
195+
}
196+
}
197+
checkArg(width);
198+
checkArg(height);
199+
return this.addArguments(`--window-size=${width},${height}`);
200+
}
201+
173202
/**
174203
* Sets the profile to use. The profile may be specified as a
175204
* {@link Profile} object or as the path to an existing Firefox profile to use

0 commit comments

Comments
 (0)