feat(install): use shared installation folder by default (#2044)
This commit is contained in:
parent
c55db6d315
commit
9f62f29946
|
|
@ -3,12 +3,13 @@
|
||||||
<!-- GEN:toc -->
|
<!-- GEN:toc -->
|
||||||
- [System requirements](#system-requirements)
|
- [System requirements](#system-requirements)
|
||||||
- [Managing browser binaries](#managing-browser-binaries)
|
- [Managing browser binaries](#managing-browser-binaries)
|
||||||
* [Download from artifact repository](#download-from-artifact-repository)
|
- [Download from artifact repository](#download-from-artifact-repository)
|
||||||
* [Share browser binaries across projects](#share-browser-binaries-across-projects)
|
- [Skip browser downloads](#skip-browser-downloads)
|
||||||
* [Skip browser downloads](#skip-browser-downloads)
|
|
||||||
- [Download single browser binary](#download-single-browser-binary)
|
- [Download single browser binary](#download-single-browser-binary)
|
||||||
<!-- GEN:stop -->
|
<!-- GEN:stop -->
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
## System requirements
|
## System requirements
|
||||||
|
|
||||||
Playwright requires Node.js version 10.15 or above. The browser binaries for Chromium,
|
Playwright requires Node.js version 10.15 or above. The browser binaries for Chromium,
|
||||||
|
|
@ -21,28 +22,56 @@ Firefox and WebKit work across the 3 platforms (Windows, macOS, Linux):
|
||||||
* For Ubuntu 18.04, the additional dependencies are defined in [our Docker image](docker/Dockerfile.bionic),
|
* For Ubuntu 18.04, the additional dependencies are defined in [our Docker image](docker/Dockerfile.bionic),
|
||||||
which is based on Ubuntu.
|
which is based on Ubuntu.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
## Managing browser binaries
|
## Managing browser binaries
|
||||||
|
|
||||||
Each version of Playwright needs specific versions of browser binaries to operate.
|
Each version of Playwright needs specific versions of browser binaries to operate. By default Playwright downloads Chromium, WebKit and Firefox browsers into the OS-specific cache folders:
|
||||||
|
|
||||||
By default it downloads Chromium, WebKit and Firefox browsers into the `node_modules/` folder. This way no extra steps are needed to get playwright up and running:
|
- `HOME\AppData\Local\ms-playwright` on Windows
|
||||||
|
- `~/Library/Caches/ms-playwright` on MacOS
|
||||||
|
- `~/.cache/playwright/ms-playwright` on Linux
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm i playwright
|
npm i playwright
|
||||||
```
|
```
|
||||||
|
|
||||||
These browsers will take hundreds of megabytes of the disk space when installed:
|
These browsers will take few hundreds of megabytes of the disk space when installed:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
du -hs ./node_modules/playwright/.local-browsers/*
|
du -hs ./Library/Caches/ms-playwright/*
|
||||||
281M .local-browsers/chromium-XXXXXX
|
281M chromium-XXXXXX
|
||||||
187M .local-browsers/firefox-XXXX
|
187M firefox-XXXX
|
||||||
180M .local-browsers/webkit-XXXX
|
180M webkit-XXXX
|
||||||
```
|
```
|
||||||
|
|
||||||
To mitigate that, Playwright has a rich set of options to control browser management.
|
You can override default behavior using environment variables. When installing Playwright, ask it to download browsers into a specific location:
|
||||||
|
|
||||||
### Download from artifact repository
|
```sh
|
||||||
|
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers npm i playwright
|
||||||
|
```
|
||||||
|
|
||||||
|
When running Playwright scripts, ask it to search for browsers in a shared location:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers node playwright-script.js
|
||||||
|
```
|
||||||
|
|
||||||
|
Or you can opt into the hermetic install and place binaries under the `node_modules/` folder:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ PLAYWRIGHT_BROWSERS_PATH=0 node playwright-script.js
|
||||||
|
```
|
||||||
|
|
||||||
|
Playwright keeps track of packages that need those browsers and will garbage collect them as you update Playwright to the newer versions.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
> **NOTE** Developers can opt-in in this mode via exporting `PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers` in their `.bashrc`.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
## Download from artifact repository
|
||||||
|
|
||||||
By default, Playwright downloads browsers from Microsoft and Google public CDNs.
|
By default, Playwright downloads browsers from Microsoft and Google public CDNs.
|
||||||
|
|
||||||
|
|
@ -54,30 +83,9 @@ location using the `PLAYWRIGHT_DOWNLOAD_HOST` env variable.
|
||||||
$ PLAYWRIGHT_DOWNLOAD_HOST=192.168.1.78 npm i playwright
|
$ PLAYWRIGHT_DOWNLOAD_HOST=192.168.1.78 npm i playwright
|
||||||
```
|
```
|
||||||
|
|
||||||
### Share browser binaries across projects
|
<br>
|
||||||
|
|
||||||
Often times, developers work with multiple NPM projects that all use Playwright.
|
## Skip browser downloads
|
||||||
By default, every project will have browser binaries in its own `node_modules/` folder.
|
|
||||||
To save the disk space and to speedup installation, Playwright can re-use
|
|
||||||
these binaries.
|
|
||||||
|
|
||||||
Sharing browser binaries is a two-step process:
|
|
||||||
|
|
||||||
1. When installing Playwright, ask it to download browsers into a shared location:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers npm i playwright
|
|
||||||
```
|
|
||||||
|
|
||||||
2. When running Playwright scripts, ask it to search for browsers in a shared location:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers node playwright-script.js
|
|
||||||
```
|
|
||||||
|
|
||||||
> **NOTE** Developers can opt-in in this mode via exporting `PLAYWRIGHT_BROWSERS_PATH=$HOME/pw-browsers` in their `.bashrc`.
|
|
||||||
|
|
||||||
### Skip browser downloads
|
|
||||||
|
|
||||||
In certain cases, it is desired to avoid browser downloads altogether because
|
In certain cases, it is desired to avoid browser downloads altogether because
|
||||||
browser binaries are managed separately.
|
browser binaries are managed separately.
|
||||||
|
|
@ -88,6 +96,8 @@ This can be done by setting `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` variable before i
|
||||||
$ PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i playwright
|
$ PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i playwright
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
## Download single browser binary
|
## Download single browser binary
|
||||||
|
|
||||||
Playwright ships three packages that bundle only a single browser:
|
Playwright ships three packages that bundle only a single browser:
|
||||||
|
|
@ -99,13 +109,13 @@ Playwright ships three packages that bundle only a single browser:
|
||||||
|
|
||||||
Using these packages is as easy as using a regular Playwright:
|
Using these packages is as easy as using a regular Playwright:
|
||||||
|
|
||||||
1. Install a specific package
|
Install a specific package
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ npm i playwright-webkit
|
$ npm i playwright-webkit
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Require package
|
Require package
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Notice a proper package name in require
|
// Notice a proper package name in require
|
||||||
|
|
|
||||||
|
|
@ -77,9 +77,27 @@ export function executablePath(browserPath: string, browser: BrowserDescriptor):
|
||||||
return tokens ? path.join(browserPath, ...tokens) : undefined;
|
return tokens ? path.join(browserPath, ...tokens) : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cacheDirectory() {
|
||||||
|
if (process.platform === 'linux')
|
||||||
|
return process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
||||||
|
|
||||||
|
if (process.platform === 'darwin')
|
||||||
|
return path.join(os.homedir(), 'Library', 'Caches');
|
||||||
|
|
||||||
|
if (process.platform === 'win32')
|
||||||
|
return process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
||||||
|
throw new Error('Unsupported platform: ' + process.platform);
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultBrowsersPath = ((): string | undefined => {
|
||||||
|
const envDefined = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
|
||||||
|
if (envDefined === '0')
|
||||||
|
return undefined;
|
||||||
|
return envDefined || path.join(cacheDirectory(), 'ms-playwright');
|
||||||
|
})();
|
||||||
|
|
||||||
export function browsersPath(packagePath: string): string {
|
export function browsersPath(packagePath: string): string {
|
||||||
const result = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
|
return defaultBrowsersPath || path.join(packagePath, '.local-browsers');
|
||||||
return result || path.join(packagePath, '.local-browsers');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function browserDirectory(browsersPath: string, browser: BrowserDescriptor): string {
|
export function browserDirectory(browsersPath: string, browser: BrowserDescriptor): string {
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ import * as browserPaths from '../install/browserPaths';
|
||||||
import * as browserFetcher from '../install/browserFetcher';
|
import * as browserFetcher from '../install/browserFetcher';
|
||||||
|
|
||||||
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
|
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
|
||||||
const fsExistsAsync = (path: string) => new Promise(f => fs.exists(path, f));
|
|
||||||
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
|
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
|
||||||
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
|
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
|
||||||
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
|
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
|
||||||
|
|
@ -39,11 +38,7 @@ export async function installBrowsersWithProgressBar(packagePath: string) {
|
||||||
logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set');
|
logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!await fsExistsAsync(browsersPath))
|
await fsMkdirAsync(linksDir, { recursive: true });
|
||||||
await fsMkdirAsync(browsersPath);
|
|
||||||
if (!await fsExistsAsync(linksDir))
|
|
||||||
await fsMkdirAsync(linksDir);
|
|
||||||
|
|
||||||
await fsWriteFileAsync(path.join(linksDir, sha1(packagePath)), packagePath);
|
await fsWriteFileAsync(path.join(linksDir, sha1(packagePath)), packagePath);
|
||||||
await validateCache(browsersPath, linksDir);
|
await validateCache(browsersPath, linksDir);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ npm pack ../../../packages/playwright-firefox
|
||||||
|
|
||||||
# cleanup environment
|
# cleanup environment
|
||||||
unset PLAYWRIGHT_DOWNLOAD_HOST
|
unset PLAYWRIGHT_DOWNLOAD_HOST
|
||||||
unset PLAYWRIGHT_BROWSERS_PATH
|
|
||||||
unset PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD
|
unset PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD
|
||||||
|
export PLAYWRIGHT_BROWSERS_PATH=0
|
||||||
|
|
||||||
# There is no option to specify output for `npm pack`, but the format is
|
# There is no option to specify output for `npm pack`, but the format is
|
||||||
# fixed.
|
# fixed.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue