feat: re-make global browser installation (#1506)

This patch removes the `PLAYWRIGHT_GLOBAL_INSTALL=1` variable
and instead introduces a new var - `PLAYWRIGHT_BROWSERS_PATH`.

You can specify `PLAYWRIGHT_BROWSERS_PATH` to affect where playwright
installs browsers and where it looks for browsers.

Fixes #1102
This commit is contained in:
Andrey Lushnikov 2020-03-24 00:08:00 -07:00 committed by GitHub
parent 7ef394b345
commit b778789ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 102 additions and 119 deletions

1
.gitignore vendored
View File

@ -6,7 +6,6 @@
.local-browsers/ .local-browsers/
/.dev_profile* /.dev_profile*
.DS_Store .DS_Store
.downloaded-browsers.json
*.swp *.swp
*.pyc *.pyc
.vscode .vscode

View File

@ -3980,10 +3980,14 @@ Playwright looks for certain [environment variables](https://en.wikipedia.org/wi
If Playwright doesn't find them in the environment, a lowercased variant of these variables will be used from the [npm config](https://docs.npmjs.com/cli/config). If Playwright doesn't find them in the environment, a lowercased variant of these variables will be used from the [npm config](https://docs.npmjs.com/cli/config).
- `PLAYWRIGHT_DOWNLOAD_HOST` - overwrite URL prefix that is used to download browsers. Note: this includes protocol and might even include path prefix. By default, Playwright uses `https://storage.googleapis.com` to download Chromium and `https://playwright.azureedge.net` to download Webkit & Firefox. - `PLAYWRIGHT_DOWNLOAD_HOST` - overwrite URL prefix that is used to download browsers. Note: this includes protocol and might even include path prefix. By default, Playwright uses `https://storage.googleapis.com` to download Chromium and `https://playwright.azureedge.net` to download Webkit & Firefox.
- `PLAYWRIGHT_GLOBAL_INSTALL` - install Playwright browsers in a single global location. Locations are different on different platforms: - `PLAYWRIGHT_BROWSERS_PATH` - specify a shared folder that playwright will use to download browsers and to look for browsers when launching browser instances.
* MacOS: `$HOME/Library/Caches/playwright-nodejs`
* Linux: `$HOME/.cache/playwright-nodejs` ```sh
* Windows: `$HOME/AppData/Local/playwright-nodejs/Cache` # Install browsers to the shared location.
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/playwright-browsers npm install playwright
# Use shared location to find browsers.
$ PLAYWRIGHT_BROWSERS_PATH=$HOME/playwright-browsers node playwright-script.js
```
### Working with selectors ### Working with selectors

View File

@ -18,22 +18,40 @@ const path = require('path');
const browserFetcher = require('./lib/server/browserFetcher.js'); const browserFetcher = require('./lib/server/browserFetcher.js');
const packageJSON = require('./package.json'); const packageJSON = require('./package.json');
const FALSY_VALUES = ['0', 'false']; function localDownloadOptions(browserName) {
const revision = packageJSON.playwright[`${browserName}_revision`];
const downloadPath = path.join(__dirname, '.local-browsers', `${browserName}-${revision}`);
return {
browser: browserName,
progressBarBrowserName: `${browserName} r${revision}`,
revision,
downloadPath,
executablePath: browserFetcher.executablePath({browser: browserName, downloadPath}),
};
}
async function downloadBrowserWithProgressBar(downloadPath, browser, respectGlobalInstall = false) { function downloadOptionsFromENV(packagePath, browserName) {
const PLAYWRIGHT_GLOBAL_INSTALL = respectGlobalInstall ? getFromENV('PLAYWRIGHT_GLOBAL_INSTALL') : false; const browsersPath = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
if (!!PLAYWRIGHT_GLOBAL_INSTALL && !FALSY_VALUES.includes(PLAYWRIGHT_GLOBAL_INSTALL.toLowerCase().trim())) { const downloadPath = browsersPath ?
const envPaths = require('env-paths'); path.join(browsersPath, 'v' + packageJSON.version, browserName) :
const appPaths = envPaths('playwright'); path.join(packagePath, '.local-browsers', browserName);
downloadPath = path.join(appPaths.cache, `playwright-${packageJSON.version}-${browser}`); return {
} downloadPath,
progressBarBrowserName: `${browserName} for playwright v${packageJSON.version}`,
revision: packageJSON.playwright[`${browserName}_revision`],
browser: browserName,
host: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'),
executablePath: browserFetcher.executablePath({browser: browserName, downloadPath}),
};
}
async function downloadBrowserWithProgressBar(options) {
let progressBar = null; let progressBar = null;
let lastDownloadedBytes = 0; let lastDownloadedBytes = 0;
const revision = packageJSON.playwright[`${browser}_revision`];
function progress(downloadedBytes, totalBytes) { function progress(downloadedBytes, totalBytes) {
if (!progressBar) { if (!progressBar) {
const ProgressBar = require('progress'); const ProgressBar = require('progress');
progressBar = new ProgressBar(`Downloading ${browser} r${revision} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, { progressBar = new ProgressBar(`Downloading ${options.progressBarBrowserName} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
complete: '=', complete: '=',
incomplete: ' ', incomplete: ' ',
width: 20, width: 20,
@ -44,15 +62,8 @@ async function downloadBrowserWithProgressBar(downloadPath, browser, respectGlob
lastDownloadedBytes = downloadedBytes; lastDownloadedBytes = downloadedBytes;
progressBar.tick(delta); progressBar.tick(delta);
} }
const executablePath = await browserFetcher.downloadBrowser({ await browserFetcher.downloadBrowser({...options, progress});
downloadPath, logPolitely(`${options.progressBarBrowserName} downloaded to ${options.downloadPath}`);
browser,
revision,
progress,
host: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'),
});
logPolitely(`${browser} downloaded to ${downloadPath}`);
return executablePath;
} }
function toMegabytes(bytes) { function toMegabytes(bytes) {
@ -75,4 +86,4 @@ function getFromENV(name) {
return value; return value;
} }
module.exports = {downloadBrowserWithProgressBar}; module.exports = {downloadBrowserWithProgressBar, downloadOptionsFromENV, localDownloadOptions};

View File

@ -13,18 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
const fs = require('fs');
const path = require('path');
const {Playwright} = require('./lib/server/playwright.js'); const {Playwright} = require('./lib/server/playwright.js');
const {localDownloadOptions} = require('./download-browser.js');
const playwright = new Playwright({ const playwright = new Playwright({
browsers: ['webkit', 'chromium', 'firefox'], browsers: ['webkit', 'chromium', 'firefox'],
}); });
try { if (fs.existsSync(path.join(__dirname, '.local-browsers'))) {
const downloadedBrowsers = require('./.downloaded-browsers.json'); playwright.chromium._executablePath = localDownloadOptions('chromium').executablePath;
playwright.chromium._executablePath = downloadedBrowsers.crExecutablePath; playwright.firefox._executablePath = localDownloadOptions('firefox').executablePath;
playwright.firefox._executablePath = downloadedBrowsers.ffExecutablePath; playwright.webkit._executablePath = localDownloadOptions('webkit').executablePath;
playwright.webkit._executablePath = downloadedBrowsers.wkExecutablePath;
} catch (e) {
} }
module.exports = playwright; module.exports = playwright;

View File

@ -32,55 +32,31 @@ const fs = require('fs');
const util = require('util'); const util = require('util');
const rmAsync = util.promisify(require('rimraf')); const rmAsync = util.promisify(require('rimraf'));
const existsAsync = path => fs.promises.access(path).then(() => true, e => false); const existsAsync = path => fs.promises.access(path).then(() => true, e => false);
const {downloadBrowserWithProgressBar} = require('./download-browser'); const {downloadBrowserWithProgressBar, localDownloadOptions} = require('./download-browser');
const protocolGenerator = require('./utils/protocol-types-generator'); const protocolGenerator = require('./utils/protocol-types-generator');
const packageJSON = require('./package.json');
const DOWNLOADED_BROWSERS_JSON_PATH = path.join(__dirname, '.downloaded-browsers.json');
const DOWNLOAD_PATHS = {
chromium: path.join(__dirname, '.local-browsers', `chromium-${packageJSON.playwright.chromium_revision}`),
firefox: path.join(__dirname, '.local-browsers', `firefox-${packageJSON.playwright.firefox_revision}`),
webkit: path.join(__dirname, '.local-browsers', `webkit-${packageJSON.playwright.webkit_revision}`),
};
(async function() { (async function() {
const downloadedBrowsersJSON = await fs.promises.readFile(DOWNLOADED_BROWSERS_JSON_PATH, 'utf8').then(json => JSON.parse(json)).catch(() => ({})); const chromiumOptions = localDownloadOptions('chromium');
try { const firefoxOptions = localDownloadOptions('firefox');
if (!(await existsAsync(DOWNLOAD_PATHS.chromium))) { const webkitOptions = localDownloadOptions('webkit');
const crExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.chromium, 'chromium', false /* respectGlobalInstall */); if (!(await existsAsync(chromiumOptions.downloadPath))) {
downloadedBrowsersJSON.crExecutablePath = crExecutablePath; await downloadBrowserWithProgressBar(chromiumOptions);
await protocolGenerator.generateChromiumProtocol(crExecutablePath); await protocolGenerator.generateChromiumProtocol(chromiumOptions.executablePath).catch(console.warn);
await fs.promises.writeFile(DOWNLOADED_BROWSERS_JSON_PATH, JSON.stringify(downloadedBrowsersJSON));
}
} catch (e) {
console.warn(e.message);
} }
try { if (!(await existsAsync(firefoxOptions.downloadPath))) {
if (!(await existsAsync(DOWNLOAD_PATHS.firefox))) { await downloadBrowserWithProgressBar(firefoxOptions);
const ffExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.firefox, 'firefox', false /* respectGlobalInstall */); await protocolGenerator.generateFirefoxProtocol(firefoxOptions.executablePath).catch(console.warn);
downloadedBrowsersJSON.ffExecutablePath = ffExecutablePath;
await protocolGenerator.generateFirefoxProtocol(ffExecutablePath);
await fs.promises.writeFile(DOWNLOADED_BROWSERS_JSON_PATH, JSON.stringify(downloadedBrowsersJSON));
}
} catch (e) {
console.warn(e.message);
} }
try { if (!(await existsAsync(webkitOptions.downloadPath))) {
if (!(await existsAsync(DOWNLOAD_PATHS.webkit))) { await downloadBrowserWithProgressBar(webkitOptions);
const wkExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.webkit, 'webkit', false /* respectGlobalInstall */); await protocolGenerator.generateWebKitProtocol(webkitOptions.downloadPath).catch(console.warn);
downloadedBrowsersJSON.wkExecutablePath = wkExecutablePath;
await protocolGenerator.generateWebKitProtocol(path.dirname(wkExecutablePath));
await fs.promises.writeFile(DOWNLOADED_BROWSERS_JSON_PATH, JSON.stringify(downloadedBrowsersJSON));
}
} catch (e) {
console.warn(e.message);
} }
// Cleanup stale revisions. // Cleanup stale revisions.
const directories = new Set(await readdirAsync(path.join(__dirname, '.local-browsers'))); const directories = new Set(await readdirAsync(path.join(__dirname, '.local-browsers')));
directories.delete(DOWNLOAD_PATHS.chromium); directories.delete(chromiumOptions.downloadPath);
directories.delete(DOWNLOAD_PATHS.firefox); directories.delete(firefoxOptions.downloadPath);
directories.delete(DOWNLOAD_PATHS.webkit); directories.delete(webkitOptions.downloadPath);
// cleanup old browser directories. // cleanup old browser directories.
directories.add(path.join(__dirname, '.local-chromium')); directories.add(path.join(__dirname, '.local-chromium'));
directories.add(path.join(__dirname, '.local-firefox')); directories.add(path.join(__dirname, '.local-firefox'));

View File

@ -45,7 +45,6 @@
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"debug": "^4.1.0", "debug": "^4.1.0",
"env-paths": "^2.2.0",
"extract-zip": "^1.6.6", "extract-zip": "^1.6.6",
"https-proxy-agent": "^3.0.0", "https-proxy-agent": "^3.0.0",
"jpeg-js": "^0.3.6", "jpeg-js": "^0.3.6",

View File

@ -15,15 +15,13 @@
*/ */
const path = require('path'); const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js'); const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');
const playwright = new Playwright({ const playwright = new Playwright({
browsers: ['chromium'], browsers: ['chromium'],
}); });
playwright.chromium._executablePath = downloadOptionsFromENV(__dirname, 'chromium').executablePath;
module.exports = playwright; module.exports = playwright;
try {
const downloadedBrowsers = require('./.downloaded-browsers.json');
playwright.chromium._executablePath = downloadedBrowsers.crExecutablePath;
} catch (e) {
throw new Error('playwright-chromium has not downloaded Chromium.');
}

View File

@ -15,8 +15,8 @@
*/ */
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser'); const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');
(async function() { (async function() {
const crExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'chromium'), 'chromium', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'chromium'));
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({crExecutablePath}));
})(); })();

View File

@ -15,16 +15,13 @@
*/ */
const path = require('path'); const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js'); const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');
const playwright = new Playwright({ const playwright = new Playwright({
browsers: ['firefox'], browsers: ['firefox'],
}); });
playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;
module.exports = playwright; module.exports = playwright;
try {
const downloadedBrowsers = require(path.join(__dirname, '.downloaded-browsers.json'));
playwright.firefox._executablePath = downloadedBrowsers.ffExecutablePath;
} catch (e) {
throw new Error('playwright-firefox has not downloaded Firefox.');
}

View File

@ -15,9 +15,8 @@
*/ */
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser'); const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');
(async function() { (async function() {
const ffExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'firefox'), 'firefox', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'firefox'));
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({ffExecutablePath, }));
})(); })();

View File

@ -15,16 +15,13 @@
*/ */
const path = require('path'); const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js'); const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');
const playwright = new Playwright({ const playwright = new Playwright({
browsers: ['webkit'], browsers: ['webkit'],
}); });
playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;
module.exports = playwright; module.exports = playwright;
try {
const downloadedBrowsers = require(path.join(__dirname, '.downloaded-browsers.json'));
playwright.webkit._executablePath = downloadedBrowsers.wkExecutablePath;
} catch (e) {
throw new Error('playwright-webkit has not downloaded WebKit.');
}

View File

@ -15,9 +15,8 @@
*/ */
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser'); const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');
(async function() { (async function() {
const wkExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'webkit'), 'webkit', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'webkit'));
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({wkExecutablePath, }));
})(); })();

View File

@ -15,18 +15,15 @@
*/ */
const path = require('path'); const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js'); const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');
const playwright = new Playwright({ const playwright = new Playwright({
browsers: ['webkit', 'chromium', 'firefox'], browsers: ['webkit', 'chromium', 'firefox'],
}); });
playwright.chromium._executablePath = downloadOptionsFromENV(__dirname, 'chromium').executablePath;
playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;
playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;
module.exports = playwright; module.exports = playwright;
try {
const downloadedBrowsers = require(path.join(__dirname, '.downloaded-browsers.json'));
playwright.chromium._executablePath = downloadedBrowsers.crExecutablePath;
playwright.firefox._executablePath = downloadedBrowsers.ffExecutablePath;
playwright.webkit._executablePath = downloadedBrowsers.wkExecutablePath;
} catch (e) {
throw new Error('ERROR: Playwright did not download browsers');
}

View File

@ -15,11 +15,10 @@
*/ */
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser'); const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');
(async function() { (async function() {
const crExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'chromium'), 'chromium', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'chromium'));
const ffExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'firefox'), 'firefox', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'firefox'));
const wkExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'webkit'), 'webkit', true /* respectGlobalInstall */); await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'webkit'));
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({crExecutablePath, ffExecutablePath, wkExecutablePath, }));
})(); })();

View File

@ -125,7 +125,7 @@ function revisionURL(options: DownloadOptions): string {
return util.format(urlTemplate, host, revision); return util.format(urlTemplate, host, revision);
} }
export async function downloadBrowser(options: DownloadOptions): Promise<string> { export async function downloadBrowser(options: DownloadOptions): Promise<void> {
const { const {
browser, browser,
revision, revision,
@ -146,9 +146,16 @@ export async function downloadBrowser(options: DownloadOptions): Promise<string>
if (await existsAsync(zipPath)) if (await existsAsync(zipPath))
await unlinkAsync(zipPath); await unlinkAsync(zipPath);
} }
const executablePath = path.join(downloadPath, ...RELATIVE_EXECUTABLE_PATHS[browser][platform as BrowserPlatform]); await chmodAsync(executablePath(options), 0o755);
await chmodAsync(executablePath, 0o755); }
return executablePath;
export function executablePath(options: DownloadOptions): string {
const {
browser,
downloadPath,
platform = CURRENT_HOST_PLATFORM,
} = options;
return path.join(downloadPath, ...RELATIVE_EXECUTABLE_PATHS[browser][platform as BrowserPlatform]);
} }
export async function canDownload(options: DownloadOptions): Promise<boolean> { export async function canDownload(options: DownloadOptions): Promise<boolean> {