Skip to content

Commit

Permalink
feat: support PLAYWRIGHT_DOWNLOAD_HOST
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aslushnikov committed Mar 2, 2020
1 parent e3ec6b2 commit e761e5b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 10 deletions.
10 changes: 10 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3848,6 +3848,16 @@ WebKit browser instance does not expose WebKit-specific features.
- [browser.newPage([options])](#browsernewpageoptions)
<!-- GEN:stop -->

### Environment Variables

> **NOTE** [playwright-core](https://www.npmjs.com/package/playwright-core) **does not** respect environment variables.
Playwright looks for certain [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to aid its operations.
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.


### Working with selectors

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).
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ const {Playwright} = require('./lib/server/playwright.js');
module.exports = new Playwright({
downloadPath: __dirname,
browsers: ['webkit', 'chromium', 'firefox'],
respectEnvironmentVariables: false,
});

1 change: 1 addition & 0 deletions packages/playwright-chromium/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
module.exports = new Playwright({
downloadPath: __dirname,
browsers: ['chromium'],
respectEnvironmentVariables: true,
});

1 change: 1 addition & 0 deletions packages/playwright-firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
module.exports = new Playwright({
downloadPath: __dirname,
browsers: ['firefox'],
respectEnvironmentVariables: true,
});

1 change: 1 addition & 0 deletions packages/playwright-webkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
module.exports = new Playwright({
downloadPath: __dirname,
browsers: ['webkit'],
respectEnvironmentVariables: true,
});

1 change: 1 addition & 0 deletions packages/playwright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js');
module.exports = new Playwright({
downloadPath: __dirname,
browsers: ['webkit', 'chromium', 'firefox'],
respectEnvironmentVariables: true,
});

6 changes: 4 additions & 2 deletions src/server/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ import { BrowserContext } from '../browserContext';

export class Chromium implements BrowserType {
private _downloadPath: string;
private _downloadHost: string;
readonly _revision: string;

constructor(downloadPath: string, preferredRevision: string) {
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
this._downloadPath = downloadPath;
this._downloadHost = downloadHost || 'https://storage.googleapis.com';
this._revision = preferredRevision;
}

Expand Down Expand Up @@ -221,7 +223,7 @@ export class Chromium implements BrowserType {

const defaultOptions = {
path: path.join(this._downloadPath, '.local-chromium'),
host: 'https://storage.googleapis.com',
host: this._downloadHost,
platform: (() => {
const platform = os.platform();
if (platform === 'darwin')
Expand Down
6 changes: 4 additions & 2 deletions src/server/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ const mkdtempAsync = platform.promisify(fs.mkdtemp);

export class Firefox implements BrowserType {
private _downloadPath: string;
private _downloadHost: string;
readonly _revision: string;

constructor(downloadPath: string, preferredRevision: string) {
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
this._downloadPath = downloadPath;
this._downloadHost = downloadHost || 'https://playwright.azureedge.net';
this._revision = preferredRevision;
}

Expand Down Expand Up @@ -219,7 +221,7 @@ export class Firefox implements BrowserType {

const defaultOptions = {
path: path.join(this._downloadPath, '.local-firefox'),
host: 'https://playwright.azureedge.net',
host: this._downloadHost,
platform: (() => {
const platform = os.platform();
if (platform === 'darwin')
Expand Down
23 changes: 19 additions & 4 deletions src/server/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ for (const className in api) {
helper.installApiHooks(className[0].toLowerCase() + className.substring(1), (api as any)[className]);
}

type PlaywrightOptions = {
downloadPath: string,
browsers: Array<('firefox'|'webkit'|'chromium')>,
respectEnvironmentVariables: boolean,
};

export class Playwright {
readonly selectors = api.Selectors._instance();
readonly devices: types.Devices;
Expand All @@ -38,18 +44,27 @@ export class Playwright {
readonly firefox: (Firefox|undefined);
readonly webkit: (WebKit|undefined);

constructor(options: {downloadPath: string, browsers: Array<('firefox'|'webkit'|'chromium')>}) {
constructor(options: PlaywrightOptions) {
const {
downloadPath,
browsers,
respectEnvironmentVariables,
} = options;
this.devices = DeviceDescriptors;
this.errors = { TimeoutError };
const downloadHost = respectEnvironmentVariables ? getFromENV('PLAYWRIGHT_DOWNLOAD_HOST') : undefined;
if (browsers.includes('chromium'))
this.chromium = new Chromium(downloadPath, packageJSON.playwright.chromium_revision);
this.chromium = new Chromium(downloadPath, downloadHost, packageJSON.playwright.chromium_revision);
if (browsers.includes('webkit'))
this.webkit = new WebKit(downloadPath, packageJSON.playwright.webkit_revision);
this.webkit = new WebKit(downloadPath, downloadHost, packageJSON.playwright.webkit_revision);
if (browsers.includes('firefox'))
this.firefox = new Firefox(downloadPath, packageJSON.playwright.firefox_revision);
this.firefox = new Firefox(downloadPath, downloadHost, packageJSON.playwright.firefox_revision);
}
}

function getFromENV(name: string): (string|undefined) {
let value = process.env[name];
value = value || process.env[`npm_config_${name.toLowerCase()}`];
value = value || process.env[`npm_package_config_${name.toLowerCase()}`];
return value;
}
6 changes: 4 additions & 2 deletions src/server/webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import { BrowserContext } from '../browserContext';

export class WebKit implements BrowserType {
private _downloadPath: string;
private _downloadHost: string;
readonly _revision: string;

constructor(downloadPath: string, preferredRevision: string) {
constructor(downloadPath: string, downloadHost: (string|undefined), preferredRevision: string) {
this._downloadPath = downloadPath;
this._downloadHost = downloadHost || 'https://playwright.azureedge.net';
this._revision = preferredRevision;
}

Expand Down Expand Up @@ -203,7 +205,7 @@ export class WebKit implements BrowserType {

const defaultOptions = {
path: path.join(this._downloadPath, '.local-webkit'),
host: 'https://playwright.azureedge.net',
host: this._downloadHost,
platform: (() => {
const platform = os.platform();
if (platform === 'darwin')
Expand Down

0 comments on commit e761e5b

Please sign in to comment.