Skip to content

Commit 08fbc92

Browse files
authored
feat: support PLAYWRIGHT_DOWNLOAD_HOST (#1179)
This patch starts respecting `PLAYWRIGHT_DOWNLOAD_HOST` env variable in `playwright` package and it's vendored flavors (`playwright-firefox`, `playwright-chromium` and `playwright-webkit`). Fixes #1045
1 parent d5951b4 commit 08fbc92

File tree

10 files changed

+47
-10
lines changed

10 files changed

+47
-10
lines changed

docs/api.md

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [class: ChromiumTarget](#class-chromiumtarget)
3232
- [class: FirefoxBrowser](#class-firefoxbrowser)
3333
- [class: WebKitBrowser](#class-webkitbrowser)
34+
- [Environment Variables](#environment-variables)
3435
- [Working with selectors](#working-with-selectors)
3536
- [Working with Chrome Extensions](#working-with-chrome-extensions)
3637
<!-- GEN:stop -->
@@ -3848,6 +3849,16 @@ WebKit browser instance does not expose WebKit-specific features.
38483849
- [browser.newPage([options])](#browsernewpageoptions)
38493850
<!-- GEN:stop -->
38503851

3852+
### Environment Variables
3853+
3854+
> **NOTE** [playwright-core](https://www.npmjs.com/package/playwright-core) **does not** respect environment variables.
3855+
3856+
Playwright looks for certain [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to aid its operations.
3857+
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).
3858+
3859+
- `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.
3860+
3861+
38513862
### Working with selectors
38523863

38533864
Selector describes an element in the page. It can be used to obtain `ElementHandle` (see [page.$()](#pageselector) for example) or shortcut element operations to avoid intermediate handle (see [page.click()](#pageclickselector-options) for example).

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ const {Playwright} = require('./lib/server/playwright.js');
1818
module.exports = new Playwright({
1919
downloadPath: __dirname,
2020
browsers: ['webkit', 'chromium', 'firefox'],
21+
respectEnvironmentVariables: false,
2122
});
2223

packages/playwright-chromium/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
1919
module.exports = new Playwright({
2020
downloadPath: __dirname,
2121
browsers: ['chromium'],
22+
respectEnvironmentVariables: true,
2223
});
2324

packages/playwright-firefox/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
1919
module.exports = new Playwright({
2020
downloadPath: __dirname,
2121
browsers: ['firefox'],
22+
respectEnvironmentVariables: true,
2223
});
2324

packages/playwright-webkit/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
1919
module.exports = new Playwright({
2020
downloadPath: __dirname,
2121
browsers: ['webkit'],
22+
respectEnvironmentVariables: true,
2223
});
2324

packages/playwright/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
1818
module.exports = new Playwright({
1919
downloadPath: __dirname,
2020
browsers: ['webkit', 'chromium', 'firefox'],
21+
respectEnvironmentVariables: true,
2122
});
2223

src/server/chromium.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ import { BrowserContext } from '../browserContext';
3838

3939
export class Chromium implements BrowserType {
4040
private _downloadPath: string;
41+
private _downloadHost: string;
4142
readonly _revision: string;
4243

43-
constructor(downloadPath: string, preferredRevision: string) {
44+
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
4445
this._downloadPath = downloadPath;
46+
this._downloadHost = downloadHost || 'https://storage.googleapis.com';
4547
this._revision = preferredRevision;
4648
}
4749

@@ -221,7 +223,7 @@ export class Chromium implements BrowserType {
221223

222224
const defaultOptions = {
223225
path: path.join(this._downloadPath, '.local-chromium'),
224-
host: 'https://storage.googleapis.com',
226+
host: this._downloadHost,
225227
platform: (() => {
226228
const platform = os.platform();
227229
if (platform === 'darwin')

src/server/firefox.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ const mkdtempAsync = platform.promisify(fs.mkdtemp);
3939

4040
export class Firefox implements BrowserType {
4141
private _downloadPath: string;
42+
private _downloadHost: string;
4243
readonly _revision: string;
4344

44-
constructor(downloadPath: string, preferredRevision: string) {
45+
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
4546
this._downloadPath = downloadPath;
47+
this._downloadHost = downloadHost || 'https://playwright.azureedge.net';
4648
this._revision = preferredRevision;
4749
}
4850

@@ -219,7 +221,7 @@ export class Firefox implements BrowserType {
219221

220222
const defaultOptions = {
221223
path: path.join(this._downloadPath, '.local-firefox'),
222-
host: 'https://playwright.azureedge.net',
224+
host: this._downloadHost,
223225
platform: (() => {
224226
const platform = os.platform();
225227
if (platform === 'darwin')

src/server/playwright.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ for (const className in api) {
3030
helper.installApiHooks(className[0].toLowerCase() + className.substring(1), (api as any)[className]);
3131
}
3232

33+
type PlaywrightOptions = {
34+
downloadPath: string,
35+
browsers: Array<('firefox'|'webkit'|'chromium')>,
36+
respectEnvironmentVariables: boolean,
37+
};
38+
3339
export class Playwright {
3440
readonly selectors = api.Selectors._instance();
3541
readonly devices: types.Devices;
@@ -38,18 +44,27 @@ export class Playwright {
3844
readonly firefox: (Firefox|undefined);
3945
readonly webkit: (WebKit|undefined);
4046

41-
constructor(options: {downloadPath: string, browsers: Array<('firefox'|'webkit'|'chromium')>}) {
47+
constructor(options: PlaywrightOptions) {
4248
const {
4349
downloadPath,
4450
browsers,
51+
respectEnvironmentVariables,
4552
} = options;
4653
this.devices = DeviceDescriptors;
4754
this.errors = { TimeoutError };
55+
const downloadHost = respectEnvironmentVariables ? getFromENV('PLAYWRIGHT_DOWNLOAD_HOST') : undefined;
4856
if (browsers.includes('chromium'))
49-
this.chromium = new Chromium(downloadPath, packageJSON.playwright.chromium_revision);
57+
this.chromium = new Chromium(downloadPath, downloadHost, packageJSON.playwright.chromium_revision);
5058
if (browsers.includes('webkit'))
51-
this.webkit = new WebKit(downloadPath, packageJSON.playwright.webkit_revision);
59+
this.webkit = new WebKit(downloadPath, downloadHost, packageJSON.playwright.webkit_revision);
5260
if (browsers.includes('firefox'))
53-
this.firefox = new Firefox(downloadPath, packageJSON.playwright.firefox_revision);
61+
this.firefox = new Firefox(downloadPath, downloadHost, packageJSON.playwright.firefox_revision);
5462
}
5563
}
64+
65+
function getFromENV(name: string): (string|undefined) {
66+
let value = process.env[name];
67+
value = value || process.env[`npm_config_${name.toLowerCase()}`];
68+
value = value || process.env[`npm_package_config_${name.toLowerCase()}`];
69+
return value;
70+
}

src/server/webkit.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ import { BrowserContext } from '../browserContext';
4141

4242
export class WebKit implements BrowserType {
4343
private _downloadPath: string;
44+
private _downloadHost: string;
4445
readonly _revision: string;
4546

46-
constructor(downloadPath: string, preferredRevision: string) {
47+
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
4748
this._downloadPath = downloadPath;
49+
this._downloadHost = downloadHost || 'https://playwright.azureedge.net';
4850
this._revision = preferredRevision;
4951
}
5052

@@ -203,7 +205,7 @@ export class WebKit implements BrowserType {
203205

204206
const defaultOptions = {
205207
path: path.join(this._downloadPath, '.local-webkit'),
206-
host: 'https://playwright.azureedge.net',
208+
host: this._downloadHost,
207209
platform: (() => {
208210
const platform = os.platform();
209211
if (platform === 'darwin')

0 commit comments

Comments
 (0)