本文翻譯自:Chrome Launcher
原文更新時間:July 21,2017
譯者:Pandorymnode
從 Node 輕鬆啟動 Google Chrome。linux
禁用了許多 Chrome 服務,他們對於自動化情景是無用的。git
在一個可用的端口上,打開瀏覽器的remote-debugging-port
。github
自動定位 Chrome 二進制文件的位置進行啟動。chrome
每次啟動都使用一個新的 Chrome profile,並在kill()
中清除它。npm
綁定Ctrl-C
(默認的)終止該 Chrome 進程。bash
對於可配置性的細節,提供一些設置選項。less
yarn add chrome-launcher # or with npm: npm install chrome-launcher
.launch([opts])
啟動選項curl
{ // (optional) remote debugging port number to use. If provided port is already busy, launch() will reject // Default: an available port is autoselected port: number; // (optional) Additional flags to pass to Chrome, for example: ['--headless', '--disable-gpu'] // See all flags here: http://peter.sh/experiments/chromium-command-line-switches/ // Do note, many flags are set by default: https://github.com/GoogleChrome/lighthouse/blob/master/chrome-launcher/flags.ts chromeFlags: Array<string>; // (optional) Close the Chrome process on `Ctrl-C` // Default: true handleSIGINT: boolean; // (optional) Explicit path of intended Chrome binary // If the `CHROME_PATH` env variable is set, that will be used // Usage of `LIGHTHOUSE_CHROMIUM_PATH` env variable is deprecated // By default, any detected Chrome Canary or Chrome (stable) will be launched chromePath: string; // (optional) Chrome profile path to use // By default, a fresh Chrome profile will be created userDataDir: string; // (optional) Starting URL to open the browser with // Default: `about:blank` startingUrl: string; // (optional) Logging level: verbose, info, error, silent // Default: 'info' logLevel: string; // (optional) Enable extension loading // Default: false enableExtensions: boolean };
.launch().then(chrome => ...
ide
// The remote debugging port exposed by the launched chrome chrome.port: number; // Method kill Chrome (and cleanup the profile folder) chrome.kill: () => Promise<{}>; // The process id chrome.pid: number;
const chromeLauncher = require('chrome-launcher'); chromeLauncher.launch({ startingUrl: 'https://google.com' }).then(chrome => { console.log(`Chrome debugging port running on ${chrome.port}`); });
在一個想 Travis 這樣的 CI 環境,可能沒有安裝 Chrome。若是你想要使用chrome-launcher
,你能夠使用 Lighthouse 的download-chrome.sh
安裝 Chrome。
curl -L https://raw.githubusercontent.com/GoogleChrome/lighthouse/v2.1.0/lighthouse-core/scripts/download-chrome.sh | bash
然後在.travis.yml
中,像這樣使用它:
language: node_js install: - yarn install before_script: - export DISPLAY=:99.0 - export LIGHTHOUSE_CHROMIUM_PATH="$(pwd)/chrome-linux/chrome" - sh -e /etc/init.d/xvfb start - curl -L https://raw.githubusercontent.com/GoogleChrome/lighthouse/v2.1.0/lighthouse-core/scripts/download-chrome.sh | bash