Skip to content

Commit

Permalink
feat: re-make global browser installation (microsoft#1506)
Browse files Browse the repository at this point in the history
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 microsoft#1102
  • Loading branch information
aslushnikov authored Mar 24, 2020
1 parent 7ef394b commit b778789
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 119 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
.local-browsers/
/.dev_profile*
.DS_Store
.downloaded-browsers.json
*.swp
*.pyc
.vscode
Expand Down
12 changes: 8 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

- `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:
* MacOS: `$HOME/Library/Caches/playwright-nodejs`
* Linux: `$HOME/.cache/playwright-nodejs`
* Windows: `$HOME/AppData/Local/playwright-nodejs/Cache`
- `PLAYWRIGHT_BROWSERS_PATH` - specify a shared folder that playwright will use to download browsers and to look for browsers when launching browser instances.

```sh
# 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
Expand Down
51 changes: 31 additions & 20 deletions download-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,40 @@ const path = require('path');
const browserFetcher = require('./lib/server/browserFetcher.js');
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) {
const PLAYWRIGHT_GLOBAL_INSTALL = respectGlobalInstall ? getFromENV('PLAYWRIGHT_GLOBAL_INSTALL') : false;
if (!!PLAYWRIGHT_GLOBAL_INSTALL && !FALSY_VALUES.includes(PLAYWRIGHT_GLOBAL_INSTALL.toLowerCase().trim())) {
const envPaths = require('env-paths');
const appPaths = envPaths('playwright');
downloadPath = path.join(appPaths.cache, `playwright-${packageJSON.version}-${browser}`);
}
function downloadOptionsFromENV(packagePath, browserName) {
const browsersPath = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
const downloadPath = browsersPath ?
path.join(browsersPath, 'v' + packageJSON.version, browserName) :
path.join(packagePath, '.local-browsers', browserName);
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 lastDownloadedBytes = 0;
const revision = packageJSON.playwright[`${browser}_revision`];
function progress(downloadedBytes, totalBytes) {
if (!progressBar) {
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: '=',
incomplete: ' ',
width: 20,
Expand All @@ -44,15 +62,8 @@ async function downloadBrowserWithProgressBar(downloadPath, browser, respectGlob
lastDownloadedBytes = downloadedBytes;
progressBar.tick(delta);
}
const executablePath = await browserFetcher.downloadBrowser({
downloadPath,
browser,
revision,
progress,
host: getFromENV('PLAYWRIGHT_DOWNLOAD_HOST'),
});
logPolitely(`${browser} downloaded to ${downloadPath}`);
return executablePath;
await browserFetcher.downloadBrowser({...options, progress});
logPolitely(`${options.progressBarBrowserName} downloaded to ${options.downloadPath}`);
}

function toMegabytes(bytes) {
Expand All @@ -75,4 +86,4 @@ function getFromENV(name) {
return value;
}

module.exports = {downloadBrowserWithProgressBar};
module.exports = {downloadBrowserWithProgressBar, downloadOptionsFromENV, localDownloadOptions};
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const {Playwright} = require('./lib/server/playwright.js');
const {localDownloadOptions} = require('./download-browser.js');

const playwright = new Playwright({
browsers: ['webkit', 'chromium', 'firefox'],
});

try {
const downloadedBrowsers = require('./.downloaded-browsers.json');
playwright.chromium._executablePath = downloadedBrowsers.crExecutablePath;
playwright.firefox._executablePath = downloadedBrowsers.ffExecutablePath;
playwright.webkit._executablePath = downloadedBrowsers.wkExecutablePath;
} catch (e) {
if (fs.existsSync(path.join(__dirname, '.local-browsers'))) {
playwright.chromium._executablePath = localDownloadOptions('chromium').executablePath;
playwright.firefox._executablePath = localDownloadOptions('firefox').executablePath;
playwright.webkit._executablePath = localDownloadOptions('webkit').executablePath;
}

module.exports = playwright;
Expand Down
56 changes: 16 additions & 40 deletions install-from-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,31 @@ const fs = require('fs');
const util = require('util');
const rmAsync = util.promisify(require('rimraf'));
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 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() {
const downloadedBrowsersJSON = await fs.promises.readFile(DOWNLOADED_BROWSERS_JSON_PATH, 'utf8').then(json => JSON.parse(json)).catch(() => ({}));
try {
if (!(await existsAsync(DOWNLOAD_PATHS.chromium))) {
const crExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.chromium, 'chromium', false /* respectGlobalInstall */);
downloadedBrowsersJSON.crExecutablePath = crExecutablePath;
await protocolGenerator.generateChromiumProtocol(crExecutablePath);
await fs.promises.writeFile(DOWNLOADED_BROWSERS_JSON_PATH, JSON.stringify(downloadedBrowsersJSON));
}
} catch (e) {
console.warn(e.message);
const chromiumOptions = localDownloadOptions('chromium');
const firefoxOptions = localDownloadOptions('firefox');
const webkitOptions = localDownloadOptions('webkit');
if (!(await existsAsync(chromiumOptions.downloadPath))) {
await downloadBrowserWithProgressBar(chromiumOptions);
await protocolGenerator.generateChromiumProtocol(chromiumOptions.executablePath).catch(console.warn);
}
try {
if (!(await existsAsync(DOWNLOAD_PATHS.firefox))) {
const ffExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.firefox, 'firefox', false /* respectGlobalInstall */);
downloadedBrowsersJSON.ffExecutablePath = ffExecutablePath;
await protocolGenerator.generateFirefoxProtocol(ffExecutablePath);
await fs.promises.writeFile(DOWNLOADED_BROWSERS_JSON_PATH, JSON.stringify(downloadedBrowsersJSON));
}
} catch (e) {
console.warn(e.message);
if (!(await existsAsync(firefoxOptions.downloadPath))) {
await downloadBrowserWithProgressBar(firefoxOptions);
await protocolGenerator.generateFirefoxProtocol(firefoxOptions.executablePath).catch(console.warn);
}
try {
if (!(await existsAsync(DOWNLOAD_PATHS.webkit))) {
const wkExecutablePath = await downloadBrowserWithProgressBar(DOWNLOAD_PATHS.webkit, 'webkit', false /* respectGlobalInstall */);
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);
if (!(await existsAsync(webkitOptions.downloadPath))) {
await downloadBrowserWithProgressBar(webkitOptions);
await protocolGenerator.generateWebKitProtocol(webkitOptions.downloadPath).catch(console.warn);
}

// Cleanup stale revisions.
const directories = new Set(await readdirAsync(path.join(__dirname, '.local-browsers')));
directories.delete(DOWNLOAD_PATHS.chromium);
directories.delete(DOWNLOAD_PATHS.firefox);
directories.delete(DOWNLOAD_PATHS.webkit);
directories.delete(chromiumOptions.downloadPath);
directories.delete(firefoxOptions.downloadPath);
directories.delete(webkitOptions.downloadPath);
// cleanup old browser directories.
directories.add(path.join(__dirname, '.local-chromium'));
directories.add(path.join(__dirname, '.local-firefox'));
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.1.0",
"env-paths": "^2.2.0",
"extract-zip": "^1.6.6",
"https-proxy-agent": "^3.0.0",
"jpeg-js": "^0.3.6",
Expand Down
10 changes: 4 additions & 6 deletions packages/playwright-chromium/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['chromium'],
});

playwright.chromium._executablePath = downloadOptionsFromENV(__dirname, 'chromium').executablePath;

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.');
}
6 changes: 3 additions & 3 deletions packages/playwright-chromium/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser');
const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');

(async function() {
const crExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'chromium'), 'chromium', true /* respectGlobalInstall */);
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({crExecutablePath}));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'chromium'));
})();
11 changes: 4 additions & 7 deletions packages/playwright-firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['firefox'],
});
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.');
}
playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;

module.exports = playwright;

5 changes: 2 additions & 3 deletions packages/playwright-firefox/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser');
const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');

(async function() {
const ffExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'firefox'), 'firefox', true /* respectGlobalInstall */);
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({ffExecutablePath, }));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'firefox'));
})();
11 changes: 4 additions & 7 deletions packages/playwright-webkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['webkit'],
});
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.');
}
playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;

module.exports = playwright;

5 changes: 2 additions & 3 deletions packages/playwright-webkit/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser');
const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');

(async function() {
const wkExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'webkit'), 'webkit', true /* respectGlobalInstall */);
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({wkExecutablePath, }));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'webkit'));
})();
15 changes: 6 additions & 9 deletions packages/playwright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/
const path = require('path');
const {Playwright} = require('playwright-core/lib/server/playwright.js');
const {downloadOptionsFromENV} = require('playwright-core/download-browser.js');

const playwright = new Playwright({
browsers: ['webkit', 'chromium', 'firefox'],
});
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');
}
playwright.chromium._executablePath = downloadOptionsFromENV(__dirname, 'chromium').executablePath;
playwright.webkit._executablePath = downloadOptionsFromENV(__dirname, 'webkit').executablePath;
playwright.firefox._executablePath = downloadOptionsFromENV(__dirname, 'firefox').executablePath;

module.exports = playwright;

9 changes: 4 additions & 5 deletions packages/playwright/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
const path = require('path');
const fs = require('fs');
const {downloadBrowserWithProgressBar} = require('playwright-core/download-browser');
const {downloadBrowserWithProgressBar, downloadOptionsFromEnv} = require('playwright-core/download-browser');

(async function() {
const crExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'chromium'), 'chromium', true /* respectGlobalInstall */);
const ffExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'firefox'), 'firefox', true /* respectGlobalInstall */);
const wkExecutablePath = await downloadBrowserWithProgressBar(path.join(__dirname, '.local-browsers', 'webkit'), 'webkit', true /* respectGlobalInstall */);
await fs.promises.writeFile(path.join(__dirname, '.downloaded-browsers.json'), JSON.stringify({crExecutablePath, ffExecutablePath, wkExecutablePath, }));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'chromium'));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'firefox'));
await downloadBrowserWithProgressBar(downloadOptionsFromEnv(__dirname, 'webkit'));
})();
15 changes: 11 additions & 4 deletions src/server/browserFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function revisionURL(options: DownloadOptions): string {
return util.format(urlTemplate, host, revision);
}

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

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> {
Expand Down

0 comments on commit b778789

Please sign in to comment.