Skip to content

Commit 1308d9a

Browse files
OrKoNergunsh
andauthored
feat: support chrome headless mode (#8260)
* feat: support chrome headless mode Co-authored-by: Ergün Erdoğmuş <[email protected]>
1 parent 260ad5d commit 1308d9a

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

.github/workflows/main.yml

+33-3
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,14 @@ jobs:
201201
uses: actions/[email protected]
202202
with:
203203
node-version: ${{ matrix.node }}
204-
205204
- name: Install dependencies
206205
run: |
207206
sudo apt-get install xvfb
208207
npm install
209208
ls .local-chromium
210-
211209
- name: Build
212210
run: |
213211
npm run build
214-
215212
- name: Run unit tests in headful mode
216213
uses: nick-invision/retry@v2
217214
continue-on-error: true
@@ -222,3 +219,36 @@ jobs:
222219
max_attempts: 1
223220
command: xvfb-run --auto-servernum npm run unit
224221
timeout_minutes: 10
222+
223+
chrome-headless-checks:
224+
runs-on: ${{ matrix.os }}
225+
strategy:
226+
matrix:
227+
# https://github.com/actions/virtual-environments#available-environments
228+
os: [ubuntu-latest, macos-latest, windows-latest]
229+
node: [16]
230+
steps:
231+
- name: Checkout
232+
uses: actions/checkout@v3
233+
with:
234+
fetch-depth: 2
235+
- name: Set up Node.js
236+
uses: actions/[email protected]
237+
with:
238+
node-version: ${{ matrix.node }}
239+
- name: Install dependencies
240+
run: |
241+
npm install
242+
ls .local-chromium
243+
- name: Build
244+
run: |
245+
npm run build
246+
- name: Run unit tests
247+
uses: nick-invision/retry@v2
248+
continue-on-error: true
249+
env:
250+
CHROMIUM: true
251+
with:
252+
max_attempts: 1
253+
command: npm run chrome-headless-unit
254+
timeout_minutes: 30

docs/api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ This methods attaches Puppeteer to an existing browser instance.
580580
#### puppeteer.defaultArgs([options])
581581

582582
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
583-
- `headless` <[boolean]> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
583+
- `headless` <[boolean]|"chrome"> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`. "chrome" is a new experimental headless mode (use at your own risk).
584584
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
585585
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/user_data_dir.md).
586586
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
@@ -646,7 +646,7 @@ try {
646646
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
647647
- `product` <[string]> Which browser to launch. At this time, this is either `chrome` or `firefox`. See also `PUPPETEER_PRODUCT`.
648648
- `ignoreHTTPSErrors` <[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
649-
- `headless` <[boolean]> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
649+
- `headless` <[boolean]|"chrome"> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`. "chrome" is a new experimental headless mode (use at your own risk).
650650
- `channel` <[string]> When specified, Puppeteer will search for the locally installed release channel of Google Chrome and use it to launch. Available values are `chrome`, `chrome-beta`, `chrome-canary`, `chrome-dev`. When channel is specified, `executablePath` cannot be specified.
651651
- `executablePath` <[string]> Path to a browser executable to run instead of the bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Puppeteer is only [guaranteed to work](https://github.com/puppeteer/puppeteer/#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk.
652652
- `slowMo` <[number]> Slows down Puppeteer operations by the specified amount of milliseconds. Useful so that you can see what is going on.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test-browser": "wtr",
1919
"test-browser-watch": "wtr --watch",
2020
"unit": "npm run tsc-cjs && mocha --config mocha-config/puppeteer-unit-tests.js",
21+
"chrome-headless-unit": "cross-env HEADLESS=chrome npm run unit",
2122
"unit-debug": "npm run tsc-cjs && mocha --inspect-brk --config mocha-config/puppeteer-unit-tests.js",
2223
"unit-with-coverage": "cross-env COVERAGE=1 npm run unit",
2324
"assert-unit-coverage": "cross-env COVERAGE=1 mocha --config mocha-config/coverage-tests.js",

src/node/LaunchOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface BrowserLaunchArgumentOptions {
2727
* Whether to run the browser in headless mode.
2828
* @defaultValue true
2929
*/
30-
headless?: boolean;
30+
headless?: boolean | 'chrome';
3131
/**
3232
* Path to a user data directory.
3333
* {@link https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/user_data_dir.md | see the Chromium docs}

src/node/Launcher.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ class ChromeLauncher implements ProductLauncher {
241241
chromeArguments.push(`--user-data-dir=${path.resolve(userDataDir)}`);
242242
if (devtools) chromeArguments.push('--auto-open-devtools-for-tabs');
243243
if (headless) {
244-
chromeArguments.push('--headless', '--hide-scrollbars', '--mute-audio');
244+
chromeArguments.push(
245+
headless === 'chrome' ? '--headless=chrome' : '--headless',
246+
'--hide-scrollbars',
247+
'--mute-audio'
248+
);
245249
}
246250
if (args.every((arg) => arg.startsWith('-')))
247251
chromeArguments.push('about:blank');

test/mocha-utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ const product =
6464

6565
const alternativeInstall = process.env.PUPPETEER_ALT_INSTALL || false;
6666

67-
const isHeadless =
68-
(process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
67+
const headless = (process.env.HEADLESS || 'true').trim().toLowerCase();
68+
const isHeadless = headless === 'true' || headless === 'chrome';
6969
const isFirefox = product === 'firefox';
7070
const isChrome = product === 'Chromium';
7171

@@ -82,7 +82,7 @@ const defaultBrowserOptions = Object.assign(
8282
{
8383
handleSIGINT: true,
8484
executablePath: process.env.BINARY,
85-
headless: isHeadless,
85+
headless: headless === 'chrome' ? ('chrome' as const) : isHeadless,
8686
dumpio: !!process.env.DUMPIO,
8787
},
8888
extraLaunchOptions

0 commit comments

Comments
 (0)