diff --git a/docs/api.md b/docs/api.md index 5096ee31423abc..fa7869eb0f00f0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -3848,6 +3848,16 @@ WebKit browser instance does not expose WebKit-specific features. - [browser.newPage([options])](#browsernewpageoptions) +### 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). diff --git a/index.js b/index.js index 04fcd82b9cc48e..4dd03dabdf75f9 100644 --- a/index.js +++ b/index.js @@ -18,5 +18,6 @@ const {Playwright} = require('./lib/server/playwright.js'); module.exports = new Playwright({ downloadPath: __dirname, browsers: ['webkit', 'chromium', 'firefox'], + respectEnvironmentVariables: false, }); diff --git a/packages/playwright-chromium/index.js b/packages/playwright-chromium/index.js index 83f7137e5b0651..4901c679e72d1a 100644 --- a/packages/playwright-chromium/index.js +++ b/packages/playwright-chromium/index.js @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js'); module.exports = new Playwright({ downloadPath: __dirname, browsers: ['chromium'], + respectEnvironmentVariables: true, }); diff --git a/packages/playwright-firefox/index.js b/packages/playwright-firefox/index.js index 62632ed2009ae0..7dbf16eac61012 100644 --- a/packages/playwright-firefox/index.js +++ b/packages/playwright-firefox/index.js @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js'); module.exports = new Playwright({ downloadPath: __dirname, browsers: ['firefox'], + respectEnvironmentVariables: true, }); diff --git a/packages/playwright-webkit/index.js b/packages/playwright-webkit/index.js index 2a952c11d2428a..0e15ee1b52063c 100644 --- a/packages/playwright-webkit/index.js +++ b/packages/playwright-webkit/index.js @@ -19,5 +19,6 @@ const {Playwright} = require('playwright-core/lib/server/playwright.js'); module.exports = new Playwright({ downloadPath: __dirname, browsers: ['webkit'], + respectEnvironmentVariables: true, }); diff --git a/packages/playwright/index.js b/packages/playwright/index.js index a7a64cbc656736..5866f0899d9ab2 100644 --- a/packages/playwright/index.js +++ b/packages/playwright/index.js @@ -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, }); diff --git a/src/server/chromium.ts b/src/server/chromium.ts index 16bc78ba658d76..14ba188770e75f 100644 --- a/src/server/chromium.ts +++ b/src/server/chromium.ts @@ -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; } @@ -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') diff --git a/src/server/firefox.ts b/src/server/firefox.ts index adbd6601b939a4..2a43a4f26f5f48 100644 --- a/src/server/firefox.ts +++ b/src/server/firefox.ts @@ -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; } @@ -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') diff --git a/src/server/playwright.ts b/src/server/playwright.ts index 3295b7e460b3a4..3a3d96d73ccef9 100644 --- a/src/server/playwright.ts +++ b/src/server/playwright.ts @@ -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; @@ -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; +} diff --git a/src/server/webkit.ts b/src/server/webkit.ts index b93e380b35fc6b..f04383d8035ed0 100644 --- a/src/server/webkit.ts +++ b/src/server/webkit.ts @@ -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; } @@ -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')