docs: unify XYZPlaywright api and docs (#443)

This commit is contained in:
Dmitry Gozman 2020-01-09 14:49:22 -08:00 committed by GitHub
parent fea801659d
commit 350452c31d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 563 additions and 400 deletions

View file

@ -5,6 +5,11 @@
##### Table of Contents
<!-- GEN:toc -->
- [class: Playwright](#class-playwright)
* [playwright.createBrowserFetcher([options])](#playwrightcreatebrowserfetcheroptions)
* [playwright.devices](#playwrightdevices)
* [playwright.errors](#playwrighterrors)
* [playwright.executablePath()](#playwrightexecutablepath)
- [class: Browser](#class-browser)
* [event: 'disconnected'](#event-disconnected)
* [browser.browserContexts()](#browserbrowsercontexts)
@ -24,17 +29,11 @@
* [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation)
* [browserContext.setPermissions(origin, permissions[])](#browsercontextsetpermissionsorigin-permissions)
- [class: BrowserFetcher](#class-browserfetcher)
* [browserFetcher.canDownload(revision)](#browserfetchercandownloadrevision)
* [browserFetcher.download(revision[, progressCallback])](#browserfetcherdownloadrevision-progresscallback)
* [browserFetcher.canDownload([revision])](#browserfetchercandownloadrevision)
* [browserFetcher.download([revision, progressCallback])](#browserfetcherdownloadrevision-progresscallback)
* [browserFetcher.localRevisions()](#browserfetcherlocalrevisions)
* [browserFetcher.remove(revision)](#browserfetcherremoverevision)
* [browserFetcher.revisionInfo(revision)](#browserfetcherrevisioninforevision)
- [class: ChromiumBrowserServer](#class-chromiumbrowserserver)
* [chromiumBrowserServer.close()](#chromiumbrowserserverclose)
* [chromiumBrowserServer.connect()](#chromiumbrowserserverconnect)
* [chromiumBrowserServer.connectOptions()](#chromiumbrowserserverconnectoptions)
* [chromiumBrowserServer.process()](#chromiumbrowserserverprocess)
* [chromiumBrowserServer.wsEndpoint()](#chromiumbrowserserverwsendpoint)
* [browserFetcher.remove([revision])](#browserfetcherremoverevision)
* [browserFetcher.revisionInfo([revision])](#browserfetcherrevisioninforevision)
- [class: ConsoleMessage](#class-consolemessage)
* [consoleMessage.args()](#consolemessageargs)
* [consoleMessage.location()](#consolemessagelocation)
@ -228,6 +227,20 @@
- [class: TimeoutError](#class-timeouterror)
- [class: Accessibility](#class-accessibility)
* [accessibility.snapshot([options])](#accessibilitysnapshotoptions)
- [class: Coverage](#class-coverage)
* [coverage.startCSSCoverage([options])](#coveragestartcsscoverageoptions)
* [coverage.startJSCoverage([options])](#coveragestartjscoverageoptions)
* [coverage.stopCSSCoverage()](#coveragestopcsscoverage)
* [coverage.stopJSCoverage()](#coveragestopjscoverage)
- [class: Worker](#class-worker)
* [worker.evaluate(pageFunction[, ...args])](#workerevaluatepagefunction-args)
* [worker.evaluateHandle(pageFunction[, ...args])](#workerevaluatehandlepagefunction-args)
* [worker.url()](#workerurl)
- [class: ChromiumPlaywright](#class-chromiumplaywright)
* [chromiumPlaywright.connect(options)](#chromiumplaywrightconnectoptions)
* [chromiumPlaywright.defaultArgs([options])](#chromiumplaywrightdefaultargsoptions)
* [chromiumPlaywright.launch([options])](#chromiumplaywrightlaunchoptions)
* [chromiumPlaywright.launchServer([options])](#chromiumplaywrightlaunchserveroptions)
- [class: ChromiumBrowser](#class-chromiumbrowser)
* [event: 'targetchanged'](#event-targetchanged)
* [event: 'targetcreated'](#event-targetcreated)
@ -239,21 +252,12 @@
* [chromiumBrowser.stopTracing()](#chromiumbrowserstoptracing)
* [chromiumBrowser.targets(context)](#chromiumbrowsertargetscontext)
* [chromiumBrowser.waitForTarget(predicate[, options])](#chromiumbrowserwaitfortargetpredicate-options)
- [class: Coverage](#class-coverage)
* [coverage.startCSSCoverage([options])](#coveragestartcsscoverageoptions)
* [coverage.startJSCoverage([options])](#coveragestartjscoverageoptions)
* [coverage.stopCSSCoverage()](#coveragestopcsscoverage)
* [coverage.stopJSCoverage()](#coveragestopjscoverage)
- [class: ChromiumPlaywright](#class-chromiumplaywright)
* [chromiumPlaywright.connect(options)](#chromiumplaywrightconnectoptions)
* [chromiumPlaywright.createBrowserFetcher([options])](#chromiumplaywrightcreatebrowserfetcheroptions)
* [chromiumPlaywright.defaultArgs([options])](#chromiumplaywrightdefaultargsoptions)
* [chromiumPlaywright.devices](#chromiumplaywrightdevices)
* [chromiumPlaywright.downloadBrowser([options])](#chromiumplaywrightdownloadbrowseroptions)
* [chromiumPlaywright.errors](#chromiumplaywrighterrors)
* [chromiumPlaywright.executablePath()](#chromiumplaywrightexecutablepath)
* [chromiumPlaywright.launch([options])](#chromiumplaywrightlaunchoptions)
* [chromiumPlaywright.launchServer([options])](#chromiumplaywrightlaunchserveroptions)
- [class: ChromiumBrowserServer](#class-chromiumbrowserserver)
* [chromiumBrowserServer.close()](#chromiumbrowserserverclose)
* [chromiumBrowserServer.connect()](#chromiumbrowserserverconnect)
* [chromiumBrowserServer.connectOptions()](#chromiumbrowserserverconnectoptions)
* [chromiumBrowserServer.process()](#chromiumbrowserserverprocess)
* [chromiumBrowserServer.wsEndpoint()](#chromiumbrowserserverwsendpoint)
- [class: ChromiumSession](#class-chromiumsession)
* [chromiumSession.detach()](#chromiumsessiondetach)
* [chromiumSession.send(method[, params])](#chromiumsessionsendmethod-params)
@ -264,27 +268,106 @@
* [chromiumTarget.page()](#chromiumtargetpage)
* [chromiumTarget.type()](#chromiumtargettype)
* [chromiumTarget.url()](#chromiumtargeturl)
- [class: FirefoxPlaywright](#class-firefoxplaywright)
* [firefoxPlaywright.connect(options)](#firefoxplaywrightconnectoptions)
* [firefoxPlaywright.defaultArgs([options])](#firefoxplaywrightdefaultargsoptions)
* [firefoxPlaywright.launch([options])](#firefoxplaywrightlaunchoptions)
* [firefoxPlaywright.launchServer([options])](#firefoxplaywrightlaunchserveroptions)
- [class: FirefoxBrowser](#class-firefoxbrowser)
- [class: FirefoxBrowserServer](#class-firefoxbrowserserver)
* [firefoxBrowserServer.close()](#firefoxbrowserserverclose)
* [firefoxBrowserServer.connect()](#firefoxbrowserserverconnect)
* [firefoxBrowserServer.connectOptions()](#firefoxbrowserserverconnectoptions)
* [firefoxBrowserServer.process()](#firefoxbrowserserverprocess)
* [firefoxBrowserServer.wsEndpoint()](#firefoxbrowserserverwsendpoint)
- [class: WebKitPlaywright](#class-webkitplaywright)
* [webkitPlaywright.defaultArgs([options])](#webkitplaywrightdefaultargsoptions)
* [webkitPlaywright.launch([options])](#webkitplaywrightlaunchoptions)
* [webkitPlaywright.launchServer([options])](#webkitplaywrightlaunchserveroptions)
- [class: WebKitBrowser](#class-webkitbrowser)
- [class: WebKitBrowserServer](#class-webkitbrowserserver)
* [webKitBrowserServer.close()](#webkitbrowserserverclose)
* [webKitBrowserServer.connect()](#webkitbrowserserverconnect)
* [webKitBrowserServer.connectOptions()](#webkitbrowserserverconnectoptions)
* [webKitBrowserServer.process()](#webkitbrowserserverprocess)
- [class: Worker](#class-worker)
* [worker.evaluate(pageFunction[, ...args])](#workerevaluatepagefunction-args)
* [worker.evaluateHandle(pageFunction[, ...args])](#workerevaluatehandlepagefunction-args)
* [worker.url()](#workerurl)
- [class: FirefoxBrowser](#class-firefoxbrowser)
- [class: WebKitBrowser](#class-webkitbrowser)
- [Working with selectors](#working-with-selectors)
- [Working with Chrome Extensions](#working-with-chrome-extensions)
- [Download defaults](#download-defaults)
<!-- GEN:stop -->
### class: Playwright
Playwright module provides a method to launch a browser instance.
The following is a typical example of using Playwright to drive automation:
```js
const playwright = require('playwright')('chromium'); // Or 'firefox' or 'webkit'.
(async () => {
const browser = await playwright.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('http://example.com');
// other actions...
await browser.close();
})();
```
See [chromiumPlaywright.launch([options])](#chromiumplaywrightlaunchoptions), [firefoxPlaywright.launch([options])](#firefoxplaywrightlaunchoptions) or [webkitPlaywright.launch([options])](#webkitplaywrightlaunchoptions) for browser-specific launch methods.
#### playwright.createBrowserFetcher([options])
- `options` <[Object]>
- `host` <[string]> A download host to be used.
- `path` <[string]> A path for the downloads folder.
- `platform` <[string]> Defaults to the current platform.
- returns: <[BrowserFetcher]>
See [Download defaults](#download-defaults) for default values.
#### playwright.devices
- returns: <[Object]>
Returns a list of devices to be used with [`page.emulate(options)`](#pageemulateoptions). Actual list of
devices can be found in [lib/deviceDescriptors.js](https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts).
```js
const playwright = require('playwright')('firefox'); // Or 'chromium' or 'webkit'.
const iPhone = playwright.devices['iPhone 6'];
(async () => {
const browser = await playwright.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.emulate(iPhone);
await page.goto('http://example.com');
// other actions...
await browser.close();
})();
```
#### playwright.errors
- returns: <[Object]>
- `TimeoutError` <[function]> A class of [TimeoutError].
Playwright methods might throw errors if they are unable to fulfill a request. For example, [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options)
might fail if the selector doesn't match any nodes during the given timeframe.
For certain types of errors Playwright uses specific error classes.
These classes are available via [`playwright.errors`](#playwrighterrors)
An example of handling a timeout error:
```js
try {
await page.waitForSelector('.foo');
} catch (e) {
if (e instanceof playwright.errors.TimeoutError) {
// Do something if this is a timeout.
}
}
```
#### playwright.executablePath()
- returns: <[string]> A path where Playwright expects to find bundled browser.
### class: Browser
* extends: [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
@ -531,20 +614,20 @@ Playwright against it:
```js
const browserFetcher = playwright.createBrowserFetcher();
const revisionInfo = await browserFetcher.download('533271');
const browser = await playwright.launch({executablePath: revisionInfo.executablePath})
const browser = await playwright.launch({executablePath: revisionInfo.executablePath});
```
> **NOTE** BrowserFetcher is not designed to work concurrently with other
> instances of BrowserFetcher that share the same downloads directory.
#### browserFetcher.canDownload(revision)
- `revision` <[string]> a revision to check availability.
#### browserFetcher.canDownload([revision])
- `revision` <[string]> a revision to check availability. Defaults to preferred revision.
- returns: <[Promise]<[boolean]>> returns `true` if the revision could be downloaded from the host.
The method initiates a HEAD request to check if the revision is available.
#### browserFetcher.download(revision[, progressCallback])
- `revision` <[string]> a revision to download.
#### browserFetcher.download([revision, progressCallback])
- `revision` <[string]> a revision to download. Defaults to preferred revision.
- `progressCallback` <[function]([number], [number])> A function that will be called with two arguments:
- `downloadedBytes` <[number]> how many bytes have been downloaded
- `totalBytes` <[number]> how large is the total download.
@ -555,17 +638,17 @@ The method initiates a HEAD request to check if the revision is available.
- `url` <[string]> URL this revision can be downloaded from
- `local` <[boolean]> whether the revision is locally available on disk
The method initiates a GET request to download the revision from the host.
The method initiates a GET request to download the revision from the host. See [Download defaults](#download-defaults) for approximate download size.
#### browserFetcher.localRevisions()
- returns: <[Promise]<[Array]<[string]>>> A list of all revisions available locally on disk.
#### browserFetcher.remove(revision)
- `revision` <[string]> a revision to remove. The method will throw if the revision has not been downloaded.
#### browserFetcher.remove([revision])
- `revision` <[string]> a revision to remove. Defaults to preferred revision. The method will throw if the revision has not been downloaded.
- returns: <[Promise]> Resolves when the revision has been removed.
#### browserFetcher.revisionInfo(revision)
- `revision` <[string]> a revision to get info for.
#### browserFetcher.revisionInfo([revision])
- `revision` <[string]> a revision to get info for. Defaults to preferred revision.
- returns: <[Object]>
- `revision` <[string]> the revision the info was created from
- `folderPath` <[string]> path to the extracted revision folder
@ -573,37 +656,6 @@ The method initiates a GET request to download the revision from the host.
- `url` <[string]> URL this revision can be downloaded from
- `local` <[boolean]> whether the revision is locally available on disk
### class: ChromiumBrowserServer
#### chromiumBrowserServer.close()
- returns: <[Promise]>
Closes the browser gracefully and makes sure the process is terminated.
#### chromiumBrowserServer.connect()
- returns: <[Promise]<[Browser]>>
Connects to the browser server and returns a <[Browser]> object.
#### chromiumBrowserServer.connectOptions()
- returns: <[Object]>
- `browserWSEndpoint` <?[string]> a [browser websocket endpoint](#browserwsendpoint) to connect to.
- `browserURL` <?[string]> a browser url to connect to, in format `http://${host}:${port}`. Use interchangeably with `browserWSEndpoint` to let Playwright fetch it from [metadata endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
- `slowMo` <[number]>
- `transport` <[ConnectionTransport]> **Experimental** A custom transport object which should be used to connect.
This options object can be passed to [chromiumPlaywright.connect(options)](#chromiumplaywrightconnectoptions) to establish connection to the browser.
#### chromiumBrowserServer.process()
- returns: <?[ChildProcess]> Spawned browser server process.
#### chromiumBrowserServer.wsEndpoint()
- returns: <?[string]> Browser websocket url.
Browser websocket endpoint which can be used as an argument to `playwright.connect`.
Learn more about the [devtools protocol](https://chromedevtools.github.io/devtools-protocol) and the [browser endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
### class: ConsoleMessage
[ConsoleMessage] objects are dispatched by page via the ['console'](#event-console) event.
@ -3132,6 +3184,185 @@ function findFocusedNode(node) {
}
```
### class: Coverage
Coverage gathers information about parts of JavaScript and CSS that were used by the page.
An example of using JavaScript and CSS coverage to get percentage of initially
executed code:
```js
// Enable both JavaScript and CSS coverage
await Promise.all([
page.coverage.startJSCoverage(),
page.coverage.startCSSCoverage()
]);
// Navigate to page
await page.goto('https://example.com');
// Disable both JavaScript and CSS coverage
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
let totalBytes = 0;
let usedBytes = 0;
const coverage = [...jsCoverage, ...cssCoverage];
for (const entry of coverage) {
totalBytes += entry.text.length;
for (const range of entry.ranges)
usedBytes += range.end - range.start - 1;
}
console.log(`Bytes used: ${usedBytes / totalBytes * 100}%`);
```
_To output coverage in a form consumable by [Istanbul](https://github.com/istanbuljs),
see [playwright-to-istanbul](https://github.com/istanbuljs/playwright-to-istanbul)._
#### coverage.startCSSCoverage([options])
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- returns: <[Promise]> Promise that resolves when coverage is started
#### coverage.startJSCoverage([options])
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- `reportAnonymousScripts` <[boolean]> Whether anonymous scripts generated by the page should be reported. Defaults to `false`.
- returns: <[Promise]> Promise that resolves when coverage is started
> **NOTE** Anonymous scripts are ones that don't have an associated url. These are scripts that are dynamically created on the page using `eval` or `new Function`. If `reportAnonymousScripts` is set to `true`, anonymous scripts will have `__playwright_evaluation_script__` as their URL.
#### coverage.stopCSSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all stylesheets
- `url` <[string]> StyleSheet URL
- `text` <[string]> StyleSheet content
- `ranges` <[Array]<[Object]>> StyleSheet ranges that were used. Ranges are sorted and non-overlapping.
- `start` <[number]> A start offset in text, inclusive
- `end` <[number]> An end offset in text, exclusive
> **NOTE** CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
#### coverage.stopJSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all scripts
- `url` <[string]> Script URL
- `text` <[string]> Script content
- `ranges` <[Array]<[Object]>> Script ranges that were executed. Ranges are sorted and non-overlapping.
- `start` <[number]> A start offset in text, inclusive
- `end` <[number]> An end offset in text, exclusive
> **NOTE** JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are
reported.
### class: Worker
The Worker class represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
The events `workercreated` and `workerdestroyed` are emitted on the page object to signal the worker lifecycle.
```js
page.on('workercreated', worker => console.log('Worker created: ' + worker.url()));
page.on('workerdestroyed', worker => console.log('Worker destroyed: ' + worker.url()));
console.log('Current workers:');
for (const worker of page.workers())
console.log(' ' + worker.url());
```
#### worker.evaluate(pageFunction[, ...args])
- `pageFunction` <[function]|[string]> Function to be evaluated in the worker context
- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction`
- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of `pageFunction`
If the function passed to the `worker.evaluate` returns a [Promise], then `worker.evaluate` would wait for the promise to resolve and return its value.
If the function passed to the `worker.evaluate` returns a non-[Serializable] value, then `worker.evaluate` resolves to `undefined`. DevTools Protocol also supports transferring some additional values that are not serializable by `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`, and bigint literals.
#### worker.evaluateHandle(pageFunction[, ...args])
- `pageFunction` <[function]|[string]> Function to be evaluated in the page context
- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction`
- returns: <[Promise]<[JSHandle]>> Promise which resolves to the return value of `pageFunction` as in-page object (JSHandle)
The only difference between `worker.evaluate` and `worker.evaluateHandle` is that `worker.evaluateHandle` returns in-page object (JSHandle).
If the function passed to the `worker.evaluateHandle` returns a [Promise], then `worker.evaluateHandle` would wait for the promise to resolve and return its value.
#### worker.url()
- returns: <[string]>
### class: ChromiumPlaywright
* extends: [Playwright]
#### chromiumPlaywright.connect(options)
- `options` <[Object]>
- `browserWSEndpoint` <?[string]> a [browser websocket endpoint](#browserwsendpoint) to connect to.
- `browserURL` <?[string]> a browser url to connect to, in format `http://${host}:${port}`. Use interchangeably with `browserWSEndpoint` to let Playwright fetch it from [metadata endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `transport` <[ConnectionTransport]> **Experimental** Specify a custom transport object for Playwright to use.
- returns: <[Promise]<[ChromiumBrowser]>>
This methods attaches Playwright to an existing Chromium instance.
#### chromiumPlaywright.defaultArgs([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- returns: <[Array]<[string]>>
The default flags that Chromium will be launched with.
#### chromiumPlaywright.launch([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `executablePath` <[string]> Path to a Chromium or Chrome executable to run instead of the bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only [guaranteed to work](https://github.com/Microsoft/playwright/#q-why-doesnt-playwright-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`chromiumPlaywright.defaultArgs()`](#chromiumplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
- returns: <[Promise]<[ChromiumBrowser]>> Promise which resolves to browser instance.
You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
```js
const browser = await playwright.launch({
ignoreDefaultArgs: ['--mute-audio']
});
```
> **NOTE** Playwright can also be used to control the Chrome browser, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use `executablePath` option with extreme caution.
>
> If Google Chrome (rather than Chromium) is preferred, a [Chrome Canary](https://www.google.com/chrome/browser/canary.html) or [Dev Channel](https://www.chromium.org/getting-involved/dev-channel) build is suggested.
>
> In [chromiumPlaywright.launch([options])](#chromiumplaywrightlaunchoptions) above, any mention of Chromium also applies to Chrome.
>
> See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description of the differences between Chromium and Chrome. [`This article`](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
#### chromiumPlaywright.launchServer([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `executablePath` <[string]> Path to a Chromium or Chrome executable to run instead of the bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only [guaranteed to work](https://github.com/Microsoft/playwright/#q-why-doesnt-playwright-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`chromiumPlaywright.defaultArgs()`](#chromiumplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
- returns: <[Promise]<[ChromiumBrowserServer]>> Promise which resolves to browser server instance.
### class: ChromiumBrowser
* extends: [Browser]
@ -3216,235 +3447,36 @@ await page.evaluate(() => window.open('https://www.example.com/'));
const newWindowTarget = await browser.chromium.waitForTarget(target => target.url() === 'https://www.example.com/');
```
### class: Coverage
### class: ChromiumBrowserServer
Coverage gathers information about parts of JavaScript and CSS that were used by the page.
#### chromiumBrowserServer.close()
- returns: <[Promise]>
An example of using JavaScript and CSS coverage to get percentage of initially
executed code:
Closes the browser gracefully and makes sure the process is terminated.
```js
// Enable both JavaScript and CSS coverage
await Promise.all([
page.coverage.startJSCoverage(),
page.coverage.startCSSCoverage()
]);
// Navigate to page
await page.goto('https://example.com');
// Disable both JavaScript and CSS coverage
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
let totalBytes = 0;
let usedBytes = 0;
const coverage = [...jsCoverage, ...cssCoverage];
for (const entry of coverage) {
totalBytes += entry.text.length;
for (const range of entry.ranges)
usedBytes += range.end - range.start - 1;
}
console.log(`Bytes used: ${usedBytes / totalBytes * 100}%`);
```
#### chromiumBrowserServer.connect()
- returns: <[Promise]<[ChromiumBrowser]>>
_To output coverage in a form consumable by [Istanbul](https://github.com/istanbuljs),
see [playwright-to-istanbul](https://github.com/istanbuljs/playwright-to-istanbul)._
Connects to the browser server and returns a <[ChromiumBrowser]> object.
#### coverage.startCSSCoverage([options])
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- returns: <[Promise]> Promise that resolves when coverage is started
#### coverage.startJSCoverage([options])
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- `reportAnonymousScripts` <[boolean]> Whether anonymous scripts generated by the page should be reported. Defaults to `false`.
- returns: <[Promise]> Promise that resolves when coverage is started
> **NOTE** Anonymous scripts are ones that don't have an associated url. These are scripts that are dynamically created on the page using `eval` or `new Function`. If `reportAnonymousScripts` is set to `true`, anonymous scripts will have `__playwright_evaluation_script__` as their URL.
#### coverage.stopCSSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all stylesheets
- `url` <[string]> StyleSheet URL
- `text` <[string]> StyleSheet content
- `ranges` <[Array]<[Object]>> StyleSheet ranges that were used. Ranges are sorted and non-overlapping.
- `start` <[number]> A start offset in text, inclusive
- `end` <[number]> An end offset in text, exclusive
> **NOTE** CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
#### coverage.stopJSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all scripts
- `url` <[string]> Script URL
- `text` <[string]> Script content
- `ranges` <[Array]<[Object]>> Script ranges that were executed. Ranges are sorted and non-overlapping.
- `start` <[number]> A start offset in text, inclusive
- `end` <[number]> An end offset in text, exclusive
> **NOTE** JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are
reported.
### class: ChromiumPlaywright
Playwright module provides a method to launch a Chromium instance.
The following is a typical example of using Playwright to drive automation:
```js
const playwright = require('playwright');
(async () => {
const browser = await playwright.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.google.com');
// other actions...
await browser.close();
})();
```
#### chromiumPlaywright.connect(options)
- `options` <[Object]>
#### chromiumBrowserServer.connectOptions()
- returns: <[Object]>
- `browserWSEndpoint` <?[string]> a [browser websocket endpoint](#browserwsendpoint) to connect to.
- `browserURL` <?[string]> a browser url to connect to, in format `http://${host}:${port}`. Use interchangeably with `browserWSEndpoint` to let Playwright fetch it from [metadata endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `transport` <[ConnectionTransport]> **Experimental** Specify a custom transport object for Playwright to use.
- returns: <[Promise]<[Browser]>>
- `slowMo` <[number]>
- `transport` <[ConnectionTransport]> **Experimental** A custom transport object which should be used to connect.
This methods attaches Playwright to an existing Chromium instance.
This options object can be passed to [chromiumPlaywright.connect(options)](#chromiumplaywrightconnectoptions) to establish connection to the browser.
#### chromiumPlaywright.createBrowserFetcher([options])
- `options` <[Object]>
- `host` <[string]> A download host to be used. Defaults to `https://storage.googleapis.com`.
- `path` <[string]> A path for the downloads folder. Defaults to `<root>/.local-chromium`, where `<root>` is playwright's package root.
- `platform` <[string]> Possible values are: `mac`, `win32`, `win64`, `linux`. Defaults to the current platform.
- returns: <[BrowserFetcher]>
#### chromiumBrowserServer.process()
- returns: <?[ChildProcess]> Spawned browser server process.
#### chromiumPlaywright.defaultArgs([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- returns: <[Array]<[string]>>
#### chromiumBrowserServer.wsEndpoint()
- returns: <?[string]> Browser websocket url.
The default flags that Chromium will be launched with.
Browser websocket endpoint which can be used as an argument to [chromiumPlaywright.connect(options)](#chromiumplaywrightconnectoptions).
#### chromiumPlaywright.devices
- returns: <[Object]>
Returns a list of devices to be used with [`page.emulate(options)`](#pageemulateoptions). Actual list of
devices can be found in [lib/DeviceDescriptors.js](https://github.com/Microsoft/playwright/blob/master/lib/DeviceDescriptors.js).
```js
const playwright = require('playwright');
const iPhone = playwright.devices['iPhone 6'];
(async () => {
const browser = await playwright.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.emulate(iPhone);
await page.goto('https://www.google.com');
// other actions...
await browser.close();
})();
```
> **NOTE** The old way (Playwright versions <= v1.14.0) devices can be obtained with `require('playwright/DeviceDescriptors')`.
#### chromiumPlaywright.downloadBrowser([options])
- `options` <[Object]>
- `host` <[string]> A download host to be used. Defaults to `https://storage.googleapis.com`.
- `path` <[string]> A path for the downloads folder. Defaults to `<root>/.local-chromium`, where `<root>` is playwright's package root.
- `platform` <[string]> Possible values are: `mac`, `win32`, `win64`, `linux`. Defaults to the current platform.
- `onProgress` <[function]([number], [number])> A function that will be called with two arguments:
- `downloadedBytes` <[number]> how many bytes have been downloaded
- `totalBytes` <[number]> how large is the total download.
- returns: <[Promise]<[Object]>> Resolves with revision information when the revision is downloaded and extracted
- `revision` <[string]> the revision the info was created from
- `folderPath` <[string]> path to the extracted revision folder
- `executablePath` <[string]> path to the revision executable
- `url` <[string]> URL this revision can be downloaded from
- `local` <[boolean]> whether the revision is locally available on disk
Downloads the default browser that Playwright controls. The browser is usually around 100mb.
#### chromiumPlaywright.errors
- returns: <[Object]>
- `TimeoutError` <[function]> A class of [TimeoutError].
Playwright methods might throw errors if they are unable to fulfill a request. For example, [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options)
might fail if the selector doesn't match any nodes during the given timeframe.
For certain types of errors Playwright uses specific error classes.
These classes are available via [`playwright.errors`](#playwrighterrors)
An example of handling a timeout error:
```js
try {
await page.waitForSelector('.foo');
} catch (e) {
if (e instanceof playwright.errors.TimeoutError) {
// Do something if this is a timeout.
}
}
```
> **NOTE** The old way (Playwright versions <= v1.14.0) errors can be obtained with `require('playwright/Errors')`.
#### chromiumPlaywright.executablePath()
- returns: <[string]> A path where Playwright expects to find bundled Chromium.
#### chromiumPlaywright.launch([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `executablePath` <[string]> Path to a Chromium or Chrome executable to run instead of the bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only [guaranteed to work](https://github.com/Microsoft/playwright/#q-why-doesnt-playwright-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`playwright.defaultArgs()`](#playwrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
- returns: <[Promise]<[Browser]>> Promise which resolves to browser instance.
You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
```js
const browser = await playwright.launch({
ignoreDefaultArgs: ['--mute-audio']
});
```
> **NOTE** Playwright can also be used to control the Chrome browser, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use `executablePath` option with extreme caution.
>
> If Google Chrome (rather than Chromium) is preferred, a [Chrome Canary](https://www.google.com/chrome/browser/canary.html) or [Dev Channel](https://www.chromium.org/getting-involved/dev-channel) build is suggested.
>
> In [playwright.launch([options])](#playwrightlaunchoptions) above, any mention of Chromium also applies to Chrome.
>
> See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description of the differences between Chromium and Chrome. [`This article`](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
#### chromiumPlaywright.launchServer([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run browser in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). Defaults to `true` unless the `devtools` option is `true`.
- `executablePath` <[string]> Path to a Chromium or Chrome executable to run instead of the bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only [guaranteed to work](https://github.com/Microsoft/playwright/#q-why-doesnt-playwright-vxxx-work-with-chromium-vyyy) with the bundled Chromium, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`playwright.defaultArgs()`](#playwrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
- returns: <[Promise]<[BrowserServer]>> Promise which resolves to browser server instance.
Learn more about the [devtools protocol](https://chromedevtools.github.io/devtools-protocol) and the [browser endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target).
### class: ChromiumSession
@ -3511,6 +3543,74 @@ Identifies what kind of target this is. Can be `"page"`, [`"background_page"`](h
#### chromiumTarget.url()
- returns: <[string]>
### class: FirefoxPlaywright
* extends: [Playwright]
#### firefoxPlaywright.connect(options)
- `options` <[Object]>
- `browserWSEndpoint` <?[string]> a [browser websocket endpoint](#browserwsendpoint) to connect to.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `transport` <[ConnectionTransport]> **Experimental** Specify a custom transport object for Playwright to use.
- returns: <[Promise]<[FirefoxBrowser]>>
This methods attaches Playwright to an existing Firefox instance.
#### firefoxPlaywright.defaultArgs([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Firefox in [headless mode](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true`.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Firefox flags can be found [here](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options).
- `userDataDir` <[string]> Path to a [User Data Directory](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
- returns: <[Array]<[string]>>
The default flags that Firefox will be launched with.
#### firefoxPlaywright.launch([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Firefox in [headless mode](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true`.
- `executablePath` <[string]> Path to a Firefox executable to run instead of the bundled Firefox. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only guaranteed to work with the bundled Firefox, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Firefox flags can be found [here](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`firefoxPlaywright.defaultArgs()`](#firefoxplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- returns: <[Promise]<[FirefoxBrowser]>> Promise which resolves to browser instance.
You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
```js
const browser = await playwright.launch({
ignoreDefaultArgs: ['--mute-audio']
});
```
#### firefoxPlaywright.launchServer([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run Firefox in [headless mode](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true`.
- `executablePath` <[string]> Path to a Firefox executable to run instead of the bundled Firefox. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only guaranteed to work with the bundled Firefox, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Firefox flags can be found [here](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options).
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`firefoxPlaywright.defaultArgs()`](#firefoxplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- returns: <[Promise]<[FirefoxBrowserServer]>> Promise which resolves to browser server instance.
### class: FirefoxBrowser
* extends: [Browser]
Firefox browser instance does not expose Firefox-specific features.
### class: FirefoxBrowserServer
#### firefoxBrowserServer.close()
@ -3519,9 +3619,9 @@ Identifies what kind of target this is. Can be `"page"`, [`"background_page"`](h
Closes the browser gracefully and makes sure the process is terminated.
#### firefoxBrowserServer.connect()
- returns: <[Promise]<[Browser]>>
- returns: <[Promise]<[FirefoxBrowser]>>
Connects to the browser server and returns a <[Browser]> object.
Connects to the browser server and returns a <[FirefoxBrowser]> object.
#### firefoxBrowserServer.connectOptions()
- returns: <[Object]>
@ -3537,7 +3637,62 @@ This options object can be passed to [firefoxPlaywright.connect(options)](#firef
#### firefoxBrowserServer.wsEndpoint()
- returns: <?[string]> Browser websocket url.
Browser websocket endpoint which can be used as an argument to `playwright.connect`.
Browser websocket endpoint which can be used as an argument to [firefoxPlaywright.connect(options)](#firefoxplaywrightconnectoptions).
### class: WebKitPlaywright
* extends: [Playwright]
#### webkitPlaywright.defaultArgs([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance.
- returns: <[Array]<[string]>>
The default flags that WebKit will be launched with.
#### webkitPlaywright.launch([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run WebKit in headless mode. Defaults to `true`.
- `executablePath` <[string]> Path to a WebKit executable to run instead of the bundled WebKit. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only guaranteed to work with the bundled WebKit, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance.
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`webKitPlaywright.defaultArgs()`](#webkitplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- returns: <[Promise]<[WebKitBrowser]>> Promise which resolves to browser instance.
You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
```js
const browser = await playwright.launch({
ignoreDefaultArgs: ['--mute-audio']
});
```
#### webkitPlaywright.launchServer([options])
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Whether to run WebKit in headless mode. Defaults to `true`.
- `executablePath` <[string]> Path to a WebKit executable to run instead of the bundled WebKit. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). **BEWARE**: Playwright is only guaranteed to work with the bundled WebKit, use at your own risk.
- `slowMo` <[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance.
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, then do not use [`webKitPlaywright.defaultArgs()`](#webkitplaywrightdefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- returns: <[Promise]<[WebKitBrowserServer]>> Promise which resolves to browser server instance.
### class: WebKitBrowser
* extends: [Browser]
WebKit browser instance does not expose WebKit-specific features.
### class: WebKitBrowserServer
@ -3547,9 +3702,9 @@ Browser websocket endpoint which can be used as an argument to `playwright.conne
Closes the browser gracefully and makes sure the process is terminated.
#### webKitBrowserServer.connect()
- returns: <[Promise]<[Browser]>>
- returns: <[Promise]<[WebKitBrowser]>>
Connects to the browser server and returns a <[Browser]> object.
Connects to the browser server and returns a <[WebKitBrowser]> object.
#### webKitBrowserServer.connectOptions()
- returns: <[Object]>
@ -3561,53 +3716,6 @@ This options object can be passed to [webKitPlaywright.connect(options)](#webkit
#### webKitBrowserServer.process()
- returns: <?[ChildProcess]> Spawned browser server process.
### class: Worker
The Worker class represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
The events `workercreated` and `workerdestroyed` are emitted on the page object to signal the worker lifecycle.
```js
page.on('workercreated', worker => console.log('Worker created: ' + worker.url()));
page.on('workerdestroyed', worker => console.log('Worker destroyed: ' + worker.url()));
console.log('Current workers:');
for (const worker of page.workers())
console.log(' ' + worker.url());
```
#### worker.evaluate(pageFunction[, ...args])
- `pageFunction` <[function]|[string]> Function to be evaluated in the worker context
- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction`
- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of `pageFunction`
If the function passed to the `worker.evaluate` returns a [Promise], then `worker.evaluate` would wait for the promise to resolve and return its value.
If the function passed to the `worker.evaluate` returns a non-[Serializable] value, then `worker.evaluate` resolves to `undefined`. DevTools Protocol also supports transferring some additional values that are not serializable by `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`, and bigint literals.
#### worker.evaluateHandle(pageFunction[, ...args])
- `pageFunction` <[function]|[string]> Function to be evaluated in the page context
- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction`
- returns: <[Promise]<[JSHandle]>> Promise which resolves to the return value of `pageFunction` as in-page object (JSHandle)
The only difference between `worker.evaluate` and `worker.evaluateHandle` is that `worker.evaluateHandle` returns in-page object (JSHandle).
If the function passed to the `worker.evaluateHandle` returns a [Promise], then `worker.evaluateHandle` would wait for the promise to resolve and return its value.
#### worker.url()
- returns: <[string]>
### class: FirefoxBrowser
* extends: [Browser]
Firefox-specific features.
### class: WebKitBrowser
* extends: [Browser]
WebKit-specific features.
### 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).
@ -3674,6 +3782,17 @@ const playwright = require('playwright');
> **NOTE** It is not yet possible to test extension popups or content scripts.
### Download defaults
See [playwright.createBrowserFetcher([options])](#playwrightcreatebrowserfetcheroptions) for explanation. Below `<root>` is playwright's package root.
| | Chromium | Firefox | WebKit |
| --- | --- | --- | --- |
| Download host | `https://storage.googleapis.com` | `https://playwrightaccount.blob.core.windows.net` | `https://playwrightaccount.blob.core.windows.net` |
| Download path | `<root>/.local-chromium` | `<root>/.local-firefox` | `<root>/.local-webkit` |
| Supported platforms | `mac`, `win32`, `win64`, `linux` | `mac`, `win32`, `win64`, `linux` | `mac`, `linux`; linux binaries are run with WSL on Windows |
| Approximate download size | 120mb | 70mb | 50mb |
[AXNode]: #accessibilitysnapshotoptions "AXNode"
[Accessibility]: #class-accessibility "Accessibility"
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array"
@ -3682,9 +3801,11 @@ const playwright = require('playwright');
[BrowserFetcher]: #class-browserfetcher "BrowserFetcher"
[Browser]: #class-browser "Browser"
[Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer "Buffer"
[CDPSession]: #class-cdpsession "CDPSession"
[ChildProcess]: https://nodejs.org/api/child_process.html "ChildProcess"
[Chromium]: #class-chromium "Chromium"
[ChromiumBrowser]: #class-chromiumbrowser "ChromiumBrowser"
[ChromiumBrowserServer]: #class-chromiumbrowserserver "ChromiumBrowserServer"
[ChromiumPlaywright]: #class-chromiumplaywright "ChromiumPlaywright"
[ChromiumSession]: #class-chromiumsession "ChromiumSession"
[ChromiumTarget]: #class-chromiumtarget "ChromiumTarget"
[ConnectionTransport]: ../lib/WebSocketTransport.js "ConnectionTransport"
[ConsoleMessage]: #class-consolemessage "ConsoleMessage"
@ -3695,6 +3816,9 @@ const playwright = require('playwright');
[Error]: https://nodejs.org/api/errors.html#errors_class_error "Error"
[File]: #class-file "https://developer.mozilla.org/en-US/docs/Web/API/File"
[FileChooser]: #class-filechooser "FileChooser"
[FirefoxBrowser]: #class-firefoxbrowser "FirefoxBrowser"
[FirefoxBrowserServer]: #class-firefoxbrowserserver "FirefoxBrowserServer"
[FirefoxPlaywright]: #class-firefoxplaywright "FirefoxPlaywright"
[Frame]: #class-frame "Frame"
[JSHandle]: #class-jshandle "JSHandle"
[Keyboard]: #class-keyboard "Keyboard"
@ -3702,6 +3826,7 @@ const playwright = require('playwright');
[Mouse]: #class-mouse "Mouse"
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object"
[Page]: #class-page "Page"
[Playwright]: #class-playwright "Playwright"
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"
[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[Request]: #class-request "Request"
@ -3713,6 +3838,9 @@ const playwright = require('playwright');
[URL]: https://nodejs.org/api/url.html
[USKeyboardLayout]: ../lib/USKeyboardLayout.js "USKeyboardLayout"
[UnixTime]: https://en.wikipedia.org/wiki/Unix_time "Unix Time"
[WebKitBrowser]: #class-webkitbrowser "WebKitBrowser"
[WebKitBrowserServer]: #class-webkitbrowserserver "WebKitBrowserServer"
[WebKitPlaywright]: #class-webkitplaywright "WebKitPlaywright"
[Worker]: #class-worker "Worker"
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean"
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function"

View file

@ -16,7 +16,7 @@
*/
// This file is only run when someone clones the github repo for development
try {
require('child_process').execSync('npm run build', {
stdio: 'ignore'
@ -73,14 +73,16 @@ async function downloadBrowser(browser, playwright) {
progressBar.tick(delta);
}
const revisionInfo = await playwright.downloadBrowser({onProgress});
const fetcher = playwright.createBrowserFetcher();
const revisionInfo = fetcher.revisionInfo();
// Do nothing if the revision is already downloaded.
if (revisionInfo.local)
return revisionInfo;
await fetcher.download(revisionInfo.revision, onProgress);
logPolitely(`${browser} downloaded to ${revisionInfo.folderPath}`);
const browserFetcher = playwright.createBrowserFetcher();
const localRevisions = await browserFetcher.localRevisions();
// Remove previous chromium revisions.
// Remove previous revisions.
const cleanupOldVersions = localRevisions.filter(revision => revision !== revisionInfo.revision).map(revision => browserFetcher.remove(revision));
await Promise.all([...cleanupOldVersions]);
return revisionInfo;

View file

@ -36,6 +36,7 @@ export { FFBrowser as FirefoxBrowser } from './firefox/ffBrowser';
export { WKBrowser as WebKitBrowser } from './webkit/wkBrowser';
export { BrowserFetcher } from './server/browserFetcher';
export { Playwright } from './server/playwright';
export { CRPlaywright as ChromiumPlaywright, CRBrowserServer as ChromiumBrowserServer } from './server/crPlaywright';
export { FFPlaywright as FirefoxPlaywright, FFBrowserServer as FirefoxBrowserServer } from './server/ffPlaywright';
export { WKPlaywright as WebKitPlaywright, WKBrowserServer as WebKitBrowserServer } from './server/wkPlaywright';

View file

@ -221,7 +221,7 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
}
}
export class Target {
class Target {
_pagePromise?: Promise<Page>;
_ffPage: FFPage | null = null;
private readonly _browser: FFBrowser;

View file

@ -44,15 +44,17 @@ export type OnProgressCallback = (downloadedBytes: number, totalBytes: number) =
export class BrowserFetcher {
private _downloadsFolder: string;
private _platform: string;
private _preferredRevision: string;
private _params: ParamsGetter;
constructor(downloadsFolder: string, platform: string, params: ParamsGetter) {
constructor(downloadsFolder: string, platform: string, preferredRevision: string, params: ParamsGetter) {
this._downloadsFolder = downloadsFolder;
this._platform = platform;
this._preferredRevision = preferredRevision;
this._params = params;
}
canDownload(revision: string): Promise<boolean> {
canDownload(revision: string = this._preferredRevision): Promise<boolean> {
const url = this._params(this._platform, revision).downloadUrl;
let resolve;
const promise = new Promise<boolean>(x => resolve = x);
@ -66,7 +68,7 @@ export class BrowserFetcher {
return promise;
}
async download(revision: string, progressCallback: OnProgressCallback | null): Promise<BrowserFetcherRevisionInfo> {
async download(revision: string = this._preferredRevision, progressCallback?: OnProgressCallback): Promise<BrowserFetcherRevisionInfo> {
const url = this._params(this._platform, revision).downloadUrl;
const zipPath = path.join(this._downloadsFolder, `download-${this._platform}-${revision}.zip`);
const folderPath = this._getFolderPath(revision);
@ -94,13 +96,13 @@ export class BrowserFetcher {
return fileNames.map(fileName => parseFolderPath(fileName)).filter(entry => entry && entry.platform === this._platform).map(entry => entry.revision);
}
async remove(revision: string) {
async remove(revision: string = this._preferredRevision) {
const folderPath = this._getFolderPath(revision);
assert(await existsAsync(folderPath), `Failed to remove: revision ${revision} is not downloaded`);
await new Promise(fulfill => removeRecursive(folderPath, fulfill));
}
revisionInfo(revision: string): BrowserFetcherRevisionInfo {
revisionInfo(revision: string = this._preferredRevision): BrowserFetcherRevisionInfo {
const folderPath = this._getFolderPath(revision);
const params = this._params(this._platform, revision);
const local = fs.existsSync(folderPath);

View file

@ -19,9 +19,8 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import { BrowserFetcher, BrowserFetcherOptions, BrowserFetcherRevisionInfo, OnProgressCallback } from '../server/browserFetcher';
import { BrowserFetcher, BrowserFetcherOptions } from '../server/browserFetcher';
import { DeviceDescriptors } from '../deviceDescriptors';
import * as Errors from '../errors';
import * as types from '../types';
import { assert } from '../helper';
import { CRBrowser, CRConnectOptions, createTransport } from '../chromium/crBrowser';
@ -31,6 +30,7 @@ import { launchProcess, waitForLine } from '../server/processLauncher';
import { ChildProcess } from 'child_process';
import { CRConnection } from '../chromium/crConnection';
import { PipeTransport } from './pipeTransport';
import { Playwright } from './playwright';
export type SlowMoOptions = {
slowMo?: number,
@ -45,7 +45,7 @@ export type ChromeArgOptions = {
export type LaunchOptions = ChromeArgOptions & SlowMoOptions & {
executablePath?: string,
ignoreDefaultArgs?: boolean|string[],
ignoreDefaultArgs?: boolean | string[],
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
@ -87,7 +87,7 @@ export class CRBrowserServer {
}
}
export class CRPlaywright {
export class CRPlaywright implements Playwright {
private _projectRoot: string;
readonly _revision: string;
@ -96,13 +96,6 @@ export class CRPlaywright {
this._revision = preferredRevision;
}
async downloadBrowser(options?: BrowserFetcherOptions & { onProgress?: OnProgressCallback }): Promise<BrowserFetcherRevisionInfo> {
const fetcher = this.createBrowserFetcher(options);
const revisionInfo = fetcher.revisionInfo(this._revision);
await fetcher.download(this._revision, options ? options.onProgress : undefined);
return revisionInfo;
}
async launch(options?: LaunchOptions): Promise<CRBrowser> {
const server = await this.launchServer(options);
return server.connect();
@ -198,8 +191,8 @@ export class CRPlaywright {
return DeviceDescriptors;
}
get errors(): any {
return Errors;
get errors(): { TimeoutError: typeof TimeoutError } {
return { TimeoutError };
}
defaultArgs(options: ChromeArgOptions = {}): string[] {
@ -255,7 +248,7 @@ export class CRPlaywright {
};
assert(!!(downloadURLs as any)[options.platform], 'Unsupported platform: ' + options.platform);
return new BrowserFetcher(options.path, options.platform, (platform: string, revision: string) => {
return new BrowserFetcher(options.path, options.platform, this._revision, (platform: string, revision: string) => {
let archiveName = '';
let executablePath = '';
if (platform === 'linux') {
@ -278,7 +271,7 @@ export class CRPlaywright {
_resolveExecutablePath(): { executablePath: string; missingText: string | null; } {
const browserFetcher = this.createBrowserFetcher();
const revisionInfo = browserFetcher.revisionInfo(this._revision);
const revisionInfo = browserFetcher.revisionInfo();
const missingText = !revisionInfo.local ? `Chromium revision is not downloaded. Run "npm install" or "yarn install"` : null;
return { executablePath: revisionInfo.executablePath, missingText };
}

View file

@ -16,10 +16,9 @@
*/
import { FFBrowser, FFConnectOptions, createTransport } from '../firefox/ffBrowser';
import { BrowserFetcher, BrowserFetcherOptions, OnProgressCallback, BrowserFetcherRevisionInfo } from './browserFetcher';
import { BrowserFetcher, BrowserFetcherOptions } from './browserFetcher';
import { DeviceDescriptors } from '../deviceDescriptors';
import { launchProcess, waitForLine } from './processLauncher';
import * as Errors from '../errors';
import * as types from '../types';
import * as platform from '../platform';
import { FFConnection } from '../firefox/ffConnection';
@ -30,6 +29,28 @@ import * as path from 'path';
import * as util from 'util';
import { TimeoutError } from '../errors';
import { assert } from '../helper';
import { Playwright } from './playwright';
export type SlowMoOptions = {
slowMo?: number,
};
export type FirefoxArgOptions = {
headless?: boolean,
args?: string[],
userDataDir?: string,
};
export type LaunchOptions = FirefoxArgOptions & SlowMoOptions & {
executablePath?: string,
ignoreDefaultArgs?: boolean | string[],
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
timeout?: number,
dumpio?: boolean,
env?: {[key: string]: string} | undefined,
};
export class FFBrowserServer {
private _process: ChildProcess;
@ -63,7 +84,7 @@ export class FFBrowserServer {
}
}
export class FFPlaywright {
export class FFPlaywright implements Playwright {
private _projectRoot: string;
readonly _revision: string;
@ -72,19 +93,12 @@ export class FFPlaywright {
this._revision = preferredRevision;
}
async downloadBrowser(options?: BrowserFetcherOptions & { onProgress?: OnProgressCallback }): Promise<BrowserFetcherRevisionInfo> {
const fetcher = this.createBrowserFetcher(options);
const revisionInfo = fetcher.revisionInfo(this._revision);
await fetcher.download(this._revision, options ? options.onProgress : undefined);
return revisionInfo;
}
async launch(options: any): Promise<FFBrowser> {
async launch(options: LaunchOptions): Promise<FFBrowser> {
const server = await this.launchServer(options);
return server.connect();
}
async launchServer(options: any = {}): Promise<FFBrowserServer> {
async launchServer(options: LaunchOptions = {}): Promise<FFBrowserServer> {
const {
ignoreDefaultArgs = false,
args = [],
@ -170,11 +184,12 @@ export class FFPlaywright {
return DeviceDescriptors;
}
get errors(): any {
return Errors;
get errors(): { TimeoutError: typeof TimeoutError } {
return { TimeoutError };
}
defaultArgs(options: any = {}): string[] {
// TODO: rename userDataDir to profile?
defaultArgs(options: FirefoxArgOptions = {}): string[] {
const {
headless = true,
args = [],
@ -219,7 +234,7 @@ export class FFPlaywright {
};
assert(!!downloadURLs[options.platform], 'Unsupported platform: ' + options.platform);
return new BrowserFetcher(options.path, options.platform, (platform: string, revision: string) => {
return new BrowserFetcher(options.path, options.platform, this._revision, (platform: string, revision: string) => {
let executablePath = '';
if (platform === 'linux')
executablePath = path.join('firefox', 'firefox');
@ -236,7 +251,7 @@ export class FFPlaywright {
_resolveExecutablePath() {
const browserFetcher = this.createBrowserFetcher();
const revisionInfo = browserFetcher.revisionInfo(this._revision);
const revisionInfo = browserFetcher.revisionInfo();
const missingText = !revisionInfo.local ? `Firefox revision is not downloaded. Run "npm install" or "yarn install"` : null;
return { executablePath: revisionInfo.executablePath, missingText };
}

26
src/server/playwright.ts Normal file
View file

@ -0,0 +1,26 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BrowserFetcherOptions, BrowserFetcher } from './browserFetcher';
import * as types from '../types';
import { TimeoutError } from '../errors';
export interface Playwright {
createBrowserFetcher(options?: BrowserFetcherOptions): BrowserFetcher;
executablePath(): string;
devices: types.Devices;
errors: { TimeoutError: typeof TimeoutError };
}

View file

@ -15,9 +15,9 @@
* limitations under the License.
*/
import { BrowserFetcher, BrowserFetcherOptions, OnProgressCallback, BrowserFetcherRevisionInfo } from './browserFetcher';
import { BrowserFetcher, BrowserFetcherOptions } from './browserFetcher';
import { DeviceDescriptors } from '../deviceDescriptors';
import * as Errors from '../errors';
import { TimeoutError } from '../errors';
import * as types from '../types';
import { WKBrowser, createTransport } from '../webkit/wkBrowser';
import { WKConnectOptions } from '../webkit/wkBrowser';
@ -29,9 +29,10 @@ import * as util from 'util';
import * as os from 'os';
import { assert } from '../helper';
import { kBrowserCloseMessageId } from '../webkit/wkConnection';
import { Playwright } from './playwright';
export type LaunchOptions = {
ignoreDefaultArgs?: boolean,
ignoreDefaultArgs?: boolean | string[],
args?: string[],
executablePath?: string,
handleSIGINT?: boolean,
@ -72,7 +73,7 @@ export class WKBrowserServer {
}
}
export class WKPlaywright {
export class WKPlaywright implements Playwright {
private _projectRoot: string;
readonly _revision: string;
@ -81,13 +82,6 @@ export class WKPlaywright {
this._revision = preferredRevision;
}
async downloadBrowser(options?: BrowserFetcherOptions & { onProgress?: OnProgressCallback }): Promise<BrowserFetcherRevisionInfo> {
const fetcher = this.createBrowserFetcher(options);
const revisionInfo = fetcher.revisionInfo(this._revision);
await fetcher.download(this._revision, options ? options.onProgress : undefined);
return revisionInfo;
}
async launch(options?: LaunchOptions): Promise<WKBrowser> {
const server = await this.launchServer(options);
return server.connect();
@ -109,6 +103,8 @@ export class WKPlaywright {
const webkitArguments = [];
if (!ignoreDefaultArgs)
webkitArguments.push(...this.defaultArgs(options));
else if (Array.isArray(ignoreDefaultArgs))
webkitArguments.push(...this.defaultArgs(options).filter(arg => ignoreDefaultArgs.indexOf(arg) === -1));
else
webkitArguments.push(...args);
@ -158,11 +154,11 @@ export class WKPlaywright {
return DeviceDescriptors;
}
get errors(): any {
return Errors;
get errors(): { TimeoutError: typeof TimeoutError } {
return { TimeoutError };
}
defaultArgs(options: any = {}): string[] {
defaultArgs(options: { args?: string[] } = {}): string[] {
const {
args = [],
} = options;
@ -197,7 +193,7 @@ export class WKPlaywright {
};
assert(!!downloadURLs[options.platform], 'Unsupported platform: ' + options.platform);
return new BrowserFetcher(options.path, options.platform, (platform: string, revision: string) => {
return new BrowserFetcher(options.path, options.platform, this._revision, (platform: string, revision: string) => {
return {
downloadUrl: (platform === 'mac') ?
util.format(downloadURLs[platform], options.host, revision, getMacVersion()) :
@ -209,7 +205,7 @@ export class WKPlaywright {
_resolveExecutablePath(): { executablePath: string; missingText: string | null; } {
const browserFetcher = this.createBrowserFetcher();
const revisionInfo = browserFetcher.revisionInfo(this._revision);
const revisionInfo = browserFetcher.revisionInfo();
const missingText = !revisionInfo.local ? `WebKit revision is not downloaded. Run "npm install" or "yarn install"` : null;
return { executablePath: revisionInfo.executablePath, missingText };
}