diff --git a/index.js b/index.js index 7ab40e1018..04fcd82b9c 100644 --- a/index.js +++ b/index.js @@ -13,26 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +const {Playwright} = require('./lib/server/playwright.js'); -const { helper } = require('./lib/helper'); -const api = require('./lib/api'); -const packageJson = require('./package.json'); -const { DeviceDescriptors } = require('./lib/deviceDescriptors'); -const { TimeoutError } = require('./lib/errors'); -const { Chromium } = require('./lib/server/chromium'); -const { Firefox } = require('./lib/server/firefox'); -const { WebKit } = require('./lib/server/webkit'); +module.exports = new Playwright({ + downloadPath: __dirname, + browsers: ['webkit', 'chromium', 'firefox'], +}); -for (const className in api) { - if (typeof api[className] === 'function') - helper.installApiHooks(className[0].toLowerCase() + className.substring(1), api[className]); -} - -module.exports = { - devices: DeviceDescriptors, - errors: { TimeoutError }, - selectors: api.Selectors._instance(), - chromium: new Chromium(__dirname, packageJson.playwright.chromium_revision), - firefox: new Firefox(__dirname, packageJson.playwright.firefox_revision), - webkit: new WebKit(__dirname, packageJson.playwright.webkit_revision), -} diff --git a/packages/playwright-chromium/index.js b/packages/playwright-chromium/index.js index a784afc3bd..83f7137e5b 100644 --- a/packages/playwright-chromium/index.js +++ b/packages/playwright-chromium/index.js @@ -14,9 +14,10 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting Chromium and nullify other browsers. - webkit: undefined, - firefox: undefined, -} +const {Playwright} = require('playwright-core/lib/server/playwright.js'); + +module.exports = new Playwright({ + downloadPath: __dirname, + browsers: ['chromium'], +}); + diff --git a/packages/playwright-firefox/index.js b/packages/playwright-firefox/index.js index 5eca62b209..62632ed200 100644 --- a/packages/playwright-firefox/index.js +++ b/packages/playwright-firefox/index.js @@ -14,9 +14,10 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting firefox and nullify other browsers. - chromium: undefined, - webkit: undefined, -} +const {Playwright} = require('playwright-core/lib/server/playwright.js'); + +module.exports = new Playwright({ + downloadPath: __dirname, + browsers: ['firefox'], +}); + diff --git a/packages/playwright-webkit/index.js b/packages/playwright-webkit/index.js index 530904cd89..2a952c11d2 100644 --- a/packages/playwright-webkit/index.js +++ b/packages/playwright-webkit/index.js @@ -14,9 +14,10 @@ * limitations under the License. */ -module.exports = { - ...require('playwright-core'), - // Keep exporting webkit and nullify other browsers. - chromium: undefined, - firefox: undefined, -} +const {Playwright} = require('playwright-core/lib/server/playwright.js'); + +module.exports = new Playwright({ + downloadPath: __dirname, + browsers: ['webkit'], +}); + diff --git a/packages/playwright/index.js b/packages/playwright/index.js index a85e0cb7f7..a7a64cbc65 100644 --- a/packages/playwright/index.js +++ b/packages/playwright/index.js @@ -13,4 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core'); +const {Playwright} = require('playwright-core/lib/server/playwright.js'); + +module.exports = new Playwright({ + downloadPath: __dirname, + browsers: ['webkit', 'chromium', 'firefox'], +}); + diff --git a/src/server/chromium.ts b/src/server/chromium.ts index bd49b97097..d03b22d66d 100644 --- a/src/server/chromium.ts +++ b/src/server/chromium.ts @@ -37,11 +37,11 @@ import { ConnectionTransport } from '../transport'; import { BrowserContext } from '../browserContext'; export class Chromium implements BrowserType { - private _projectRoot: string; + private _downloadPath: string; readonly _revision: string; - constructor(projectRoot: string, preferredRevision: string) { - this._projectRoot = projectRoot; + constructor(downloadPath: string, preferredRevision: string) { + this._downloadPath = downloadPath; this._revision = preferredRevision; } @@ -218,7 +218,7 @@ export class Chromium implements BrowserType { }; const defaultOptions = { - path: path.join(this._projectRoot, '.local-chromium'), + path: path.join(this._downloadPath, '.local-chromium'), host: 'https://storage.googleapis.com', platform: (() => { const platform = os.platform(); diff --git a/src/server/firefox.ts b/src/server/firefox.ts index a05d50e058..0de94a8d0a 100644 --- a/src/server/firefox.ts +++ b/src/server/firefox.ts @@ -38,11 +38,11 @@ import { BrowserContext } from '../browserContext'; const mkdtempAsync = platform.promisify(fs.mkdtemp); export class Firefox implements BrowserType { - private _projectRoot: string; + private _downloadPath: string; readonly _revision: string; - constructor(projectRoot: string, preferredRevision: string) { - this._projectRoot = projectRoot; + constructor(downloadPath: string, preferredRevision: string) { + this._downloadPath = downloadPath; this._revision = preferredRevision; } @@ -216,7 +216,7 @@ export class Firefox implements BrowserType { }; const defaultOptions = { - path: path.join(this._projectRoot, '.local-firefox'), + path: path.join(this._downloadPath, '.local-firefox'), host: 'https://playwright.azureedge.net', platform: (() => { const platform = os.platform(); diff --git a/src/server/playwright.ts b/src/server/playwright.ts index 6547196c62..3295b7e460 100644 --- a/src/server/playwright.ts +++ b/src/server/playwright.ts @@ -15,24 +15,41 @@ */ import * as types from '../types'; +import * as api from '../api'; +import { helper } from '../helper'; import { TimeoutError } from '../errors'; import { DeviceDescriptors } from '../deviceDescriptors'; import { Chromium } from './chromium'; import { WebKit } from './webkit'; import { Firefox } from './firefox'; +const packageJSON = require('../../package.json'); + +for (const className in api) { + if (typeof (api as any)[className] === 'function') + helper.installApiHooks(className[0].toLowerCase() + className.substring(1), (api as any)[className]); +} + export class Playwright { + readonly selectors = api.Selectors._instance(); readonly devices: types.Devices; readonly errors: { TimeoutError: typeof TimeoutError }; - readonly chromium: Chromium; - readonly firefox: Firefox; - readonly webkit: WebKit; + readonly chromium: (Chromium|undefined); + readonly firefox: (Firefox|undefined); + readonly webkit: (WebKit|undefined); - constructor(projectRoot: string, revisions: { chromium_revision: string, firefox_revision: string, webkit_revision: string }) { + constructor(options: {downloadPath: string, browsers: Array<('firefox'|'webkit'|'chromium')>}) { + const { + downloadPath, + browsers, + } = options; this.devices = DeviceDescriptors; this.errors = { TimeoutError }; - this.chromium = new Chromium(projectRoot, revisions.chromium_revision); - this.firefox = new Firefox(projectRoot, revisions.firefox_revision); - this.webkit = new WebKit(projectRoot, revisions.webkit_revision); + if (browsers.includes('chromium')) + this.chromium = new Chromium(downloadPath, packageJSON.playwright.chromium_revision); + if (browsers.includes('webkit')) + this.webkit = new WebKit(downloadPath, packageJSON.playwright.webkit_revision); + if (browsers.includes('firefox')) + this.firefox = new Firefox(downloadPath, packageJSON.playwright.firefox_revision); } } diff --git a/src/server/webkit.ts b/src/server/webkit.ts index 38beb55f67..19228f2a42 100644 --- a/src/server/webkit.ts +++ b/src/server/webkit.ts @@ -40,11 +40,11 @@ import { Events } from '../events'; import { BrowserContext } from '../browserContext'; export class WebKit implements BrowserType { - private _projectRoot: string; + private _downloadPath: string; readonly _revision: string; - constructor(projectRoot: string, preferredRevision: string) { - this._projectRoot = projectRoot; + constructor(downloadPath: string, preferredRevision: string) { + this._downloadPath = downloadPath; this._revision = preferredRevision; } @@ -200,7 +200,7 @@ export class WebKit implements BrowserType { }; const defaultOptions = { - path: path.join(this._projectRoot, '.local-webkit'), + path: path.join(this._downloadPath, '.local-webkit'), host: 'https://playwright.azureedge.net', platform: (() => { const platform = os.platform();