api(review): misc changes to API. (#1356)
This commit is contained in:
parent
7fe5656257
commit
b43f33f4d3
85
docs/api.md
85
docs/api.md
|
|
@ -192,15 +192,15 @@ Indicates that the browser is connected.
|
||||||
|
|
||||||
#### browser.newContext([options])
|
#### browser.newContext([options])
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
|
- `ignoreHTTPSErrors` <[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
|
||||||
- `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy.
|
- `bypassCSP` <[boolean]> Toggles bypassing page's Content-Security-Policy.
|
||||||
- `viewport` <?[Object]> Sets a consistent viewport for each page. Defaults to an 1280x720 viewport. `null` disables the default viewport.
|
- `viewport` <[Object]> Sets a consistent viewport for each page. Defaults to an 1280x720 viewport. `null` disables the default viewport.
|
||||||
- `width` <[number]> page width in pixels.
|
- `width` <[number]> page width in pixels.
|
||||||
- `height` <[number]> page height in pixels.
|
- `height` <[number]> page height in pixels.
|
||||||
- `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`.
|
- `deviceScaleFactor` <[number]> Specify device scale factor (can be thought of as dpr). Defaults to `1`.
|
||||||
- `isMobile` <[boolean]> Whether the `meta viewport` tag is taken into account and touch events are enabled. Defaults to `false`. Not supported in Firefox.
|
- `isMobile` <[boolean]> Whether the `meta viewport` tag is taken into account and touch events are enabled. Defaults to `false`. Not supported in Firefox.
|
||||||
- `userAgent` <?[string]> Specific user agent to use in this context.
|
- `userAgent` <[string]> Specific user agent to use in this context.
|
||||||
- `javaScriptEnabled` <?[boolean]> Whether or not to enable or disable JavaScript in the context. Defaults to true.
|
- `javaScriptEnabled` <[boolean]> Whether or not to enable or disable JavaScript in the context. Defaults to true.
|
||||||
- `timezoneId` <?[string]> Changes the timezone of the context. See [ICU’s `metaZones.txt`](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs.
|
- `timezoneId` <?[string]> Changes the timezone of the context. See [ICU’s `metaZones.txt`](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs.
|
||||||
- `geolocation` <[Object]>
|
- `geolocation` <[Object]>
|
||||||
- `latitude` <[number]> Latitude between -90 and 90.
|
- `latitude` <[number]> Latitude between -90 and 90.
|
||||||
|
|
@ -341,10 +341,13 @@ An example of overriding `Math.random` before the page loads:
|
||||||
```js
|
```js
|
||||||
// preload.js
|
// preload.js
|
||||||
Math.random = () => 42;
|
Math.random = () => 42;
|
||||||
|
```
|
||||||
|
|
||||||
// In your playwright script, assuming the preload.js file is in same folder
|
```js
|
||||||
const preloadFile = fs.readFileSync('./preload.js', 'utf8');
|
// In your playwright script, assuming the preload.js file is in same folder.
|
||||||
await browserContext.addInitScript(preloadFile);
|
await browserContext.addInitScript({
|
||||||
|
path: 'preload.js'
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args) and [page.addInitScript(script[, ...args])](#pageaddinitscriptscript-args) is not defined.
|
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args) and [page.addInitScript(script[, ...args])](#pageaddinitscriptscript-args) is not defined.
|
||||||
|
|
@ -391,7 +394,7 @@ If URLs are specified, only cookies that affect those URLs are returned.
|
||||||
|
|
||||||
#### browserContext.exposeFunction(name, playwrightFunction)
|
#### browserContext.exposeFunction(name, playwrightFunction)
|
||||||
- `name` <[string]> Name of the function on the window object.
|
- `name` <[string]> Name of the function on the window object.
|
||||||
- `playwrightFunction` <[function]> Callback function which will be called in Playwright's context.
|
- `playwrightFunction` <[function]> Callback function that will be called in the Playwright's context.
|
||||||
- returns: <[Promise]>
|
- returns: <[Promise]>
|
||||||
|
|
||||||
The method adds a function called `name` on the `window` object of every frame in every page in the context.
|
The method adds a function called `name` on the `window` object of every frame in every page in the context.
|
||||||
|
|
@ -426,36 +429,6 @@ const crypto = require('crypto');
|
||||||
})();
|
})();
|
||||||
```
|
```
|
||||||
|
|
||||||
An example of adding a `window.readfile` function to all pages in the context:
|
|
||||||
|
|
||||||
```js
|
|
||||||
const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
|
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const browser = await chromium.launch();
|
|
||||||
const context = await browser.newContext();
|
|
||||||
await context.exposeFunction('readfile', async filePath => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fs.readFile(filePath, 'utf8', (err, text) => {
|
|
||||||
if (err)
|
|
||||||
reject(err);
|
|
||||||
else
|
|
||||||
resolve(text);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const page = await context.newPage();
|
|
||||||
page.on('console', msg => console.log(msg.text()));
|
|
||||||
await page.evaluate(async () => {
|
|
||||||
// use window.readfile to read contents of a file
|
|
||||||
const content = await window.readfile('/etc/hosts');
|
|
||||||
console.log(content);
|
|
||||||
});
|
|
||||||
await browser.close();
|
|
||||||
})();
|
|
||||||
```
|
|
||||||
|
|
||||||
#### browserContext.newPage()
|
#### browserContext.newPage()
|
||||||
- returns: <[Promise]<[Page]>>
|
- returns: <[Promise]<[Page]>>
|
||||||
|
|
||||||
|
|
@ -551,13 +524,13 @@ The extra HTTP headers will be sent with every request initiated by any page in
|
||||||
- `accuracy` <[number]> Optional non-negative accuracy value.
|
- `accuracy` <[number]> Optional non-negative accuracy value.
|
||||||
- returns: <[Promise]>
|
- returns: <[Promise]>
|
||||||
|
|
||||||
Sets the page's geolocation. Passing null or undefined emulates position unavailable.
|
Sets the contexts's geolocation. Passing null or undefined emulates position unavailable.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
await browserContext.setGeolocation({latitude: 59.95, longitude: 30.31667});
|
await browserContext.setGeolocation({latitude: 59.95, longitude: 30.31667});
|
||||||
```
|
```
|
||||||
|
|
||||||
> **NOTE** Consider using [browserContext.setPermissions](#browsercontextsetpermissions-permissions) to grant permissions for the page to read its geolocation.
|
> **NOTE** Consider using [browserContext.setPermissions](#browsercontextsetpermissions-permissions) to grant permissions for the browser context pages to read its geolocation.
|
||||||
|
|
||||||
#### browserContext.setHTTPCredentials(httpCredentials)
|
#### browserContext.setHTTPCredentials(httpCredentials)
|
||||||
- `httpCredentials` <?[Object]>
|
- `httpCredentials` <?[Object]>
|
||||||
|
|
@ -3346,7 +3319,7 @@ Aborts request. To use this, request interception should be enabled with `page.r
|
||||||
Exception is immediately thrown if the request interception is not enabled.
|
Exception is immediately thrown if the request interception is not enabled.
|
||||||
|
|
||||||
#### request.continue([overrides])
|
#### request.continue([overrides])
|
||||||
- `overrides` <[Object]> Optional request overwrites, which can be one of the following:
|
- `overrides` <[Object]> Optional request overrides, which can be one of the following:
|
||||||
- `method` <[string]> If set changes the request method (e.g. GET or POST)
|
- `method` <[string]> If set changes the request method (e.g. GET or POST)
|
||||||
- `postData` <[string]> If set changes the post data of request
|
- `postData` <[string]> If set changes the post data of request
|
||||||
- `headers` <[Object]> If set changes the request HTTP headers. Header values will be converted to a string.
|
- `headers` <[Object]> If set changes the request HTTP headers. Header values will be converted to a string.
|
||||||
|
|
@ -3370,10 +3343,10 @@ await page.route('**/*', request => {
|
||||||
- returns: <?[Object]> Object describing request failure, if any
|
- returns: <?[Object]> Object describing request failure, if any
|
||||||
- `errorText` <[string]> Human-readable error message, e.g. `'net::ERR_FAILED'`.
|
- `errorText` <[string]> Human-readable error message, e.g. `'net::ERR_FAILED'`.
|
||||||
|
|
||||||
The method returns `null` unless this request was failed, as reported by
|
The method returns `null` unless this request has failed, as reported by
|
||||||
`requestfailed` event.
|
`requestfailed` event.
|
||||||
|
|
||||||
Example of logging all failed requests:
|
Example of logging of all the failed requests:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
page.on('requestfailed', request => {
|
page.on('requestfailed', request => {
|
||||||
|
|
@ -3473,7 +3446,7 @@ ResourceType will be one of the following: `document`, `stylesheet`, `image`, `m
|
||||||
[Response] class represents responses which are received by page.
|
[Response] class represents responses which are received by page.
|
||||||
|
|
||||||
<!-- GEN:toc -->
|
<!-- GEN:toc -->
|
||||||
- [response.buffer()](#responsebuffer)
|
- [response.body()](#responsebody)
|
||||||
- [response.finished()](#responsefinished)
|
- [response.finished()](#responsefinished)
|
||||||
- [response.frame()](#responseframe)
|
- [response.frame()](#responseframe)
|
||||||
- [response.headers()](#responseheaders)
|
- [response.headers()](#responseheaders)
|
||||||
|
|
@ -3486,11 +3459,11 @@ ResourceType will be one of the following: `document`, `stylesheet`, `image`, `m
|
||||||
- [response.url()](#responseurl)
|
- [response.url()](#responseurl)
|
||||||
<!-- GEN:stop -->
|
<!-- GEN:stop -->
|
||||||
|
|
||||||
#### response.buffer()
|
#### response.body()
|
||||||
- returns: <Promise<[Buffer]>> Promise which resolves to a buffer with response body.
|
- returns: <Promise<[Buffer]>> Promise which resolves to a buffer with response body.
|
||||||
|
|
||||||
#### response.finished()
|
#### response.finished()
|
||||||
- returns: <Promise[?string]> Waits for this response to finish, throws when corresponding request failed.
|
- returns: <Promise<?[Error]>> Waits for this response to finish, returns failure error if request failed.
|
||||||
|
|
||||||
#### response.frame()
|
#### response.frame()
|
||||||
- returns: <[Frame]> A [Frame] that initiated this response.
|
- returns: <[Frame]> A [Frame] that initiated this response.
|
||||||
|
|
@ -3743,7 +3716,7 @@ Closes the browser gracefully and makes sure the process is terminated.
|
||||||
Kills the browser process.
|
Kills the browser process.
|
||||||
|
|
||||||
#### browserServer.process()
|
#### browserServer.process()
|
||||||
- returns: <?[ChildProcess]> Spawned browser application process.
|
- returns: <[ChildProcess]> Spawned browser application process.
|
||||||
|
|
||||||
#### browserServer.wsEndpoint()
|
#### browserServer.wsEndpoint()
|
||||||
- returns: <[string]> Browser websocket url.
|
- returns: <[string]> Browser websocket url.
|
||||||
|
|
@ -3773,15 +3746,15 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
|
||||||
- [browserType.errors](#browsertypeerrors)
|
- [browserType.errors](#browsertypeerrors)
|
||||||
- [browserType.executablePath()](#browsertypeexecutablepath)
|
- [browserType.executablePath()](#browsertypeexecutablepath)
|
||||||
- [browserType.launch([options])](#browsertypelaunchoptions)
|
- [browserType.launch([options])](#browsertypelaunchoptions)
|
||||||
- [browserType.launchPersistent(userDataDir, [options])](#browsertypelaunchpersistentuserdatadir-options)
|
- [browserType.launchPersistentContext(userDataDir, [options])](#browsertypelaunchpersistentcontextuserdatadir-options)
|
||||||
- [browserType.launchServer([options])](#browsertypelaunchserveroptions)
|
- [browserType.launchServer([options])](#browsertypelaunchserveroptions)
|
||||||
- [browserType.name()](#browsertypename)
|
- [browserType.name()](#browsertypename)
|
||||||
<!-- GEN:stop -->
|
<!-- GEN:stop -->
|
||||||
|
|
||||||
#### browserType.connect(options)
|
#### browserType.connect(options)
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `wsEndpoint` <?[string]> A browser websocket endpoint to connect to.
|
- `wsEndpoint` <[string]> A browser websocket endpoint 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.
|
- `slowMo` <?[number]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.
|
||||||
- returns: <[Promise]<[Browser]>>
|
- returns: <[Promise]<[Browser]>>
|
||||||
|
|
||||||
This methods attaches Playwright to an existing browser instance.
|
This methods attaches Playwright to an existing browser instance.
|
||||||
|
|
@ -3842,9 +3815,9 @@ try {
|
||||||
#### browserType.launch([options])
|
#### browserType.launch([options])
|
||||||
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
|
- `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. More details for [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the `devtools` option is `true`.
|
- `headless` <[boolean]> Whether to run browser in headless mode. More details for [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the `devtools` option is `true`.
|
||||||
- `executablePath` <[string]> Path to a browser executable to run instead of the bundled one. 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, Firefox or WebKit, use at your own risk.
|
- `executablePath` <[string]> Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). Note that Playwright [only works](https://github.com/Microsoft/playwright/#q-why-doesnt-playwright-vxxx-work-with-chromium-vyyy) with the bundled Chromium, Firefox or WebKit, use at your own risk.
|
||||||
- `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/).
|
- `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 [`browserType.defaultArgs()`](#browsertypedefaultargsoptions). If an array is given, then filter out the given default arguments. Dangerous option; use with care. Defaults to `false`.
|
- `ignoreDefaultArgs` <[boolean]|[Array]<[string]>> If `true`, Playwright does not pass its own configurations args and only uses the ones from `args`. If an array is given, then filters 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`.
|
- `handleSIGINT` <[boolean]> Close the browser process on Ctrl-C. Defaults to `true`.
|
||||||
- `handleSIGTERM` <[boolean]> Close the browser process on SIGTERM. 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`.
|
- `handleSIGHUP` <[boolean]> Close the browser process on SIGHUP. Defaults to `true`.
|
||||||
|
|
@ -3871,7 +3844,7 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
|
||||||
>
|
>
|
||||||
> 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.
|
> 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.
|
||||||
|
|
||||||
#### browserType.launchPersistent(userDataDir, [options])
|
#### browserType.launchPersistentContext(userDataDir, [options])
|
||||||
- `userDataDir` <[string]> Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for [Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
|
- `userDataDir` <[string]> Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for [Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
|
||||||
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
|
- `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. More details for [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the `devtools` option is `true`.
|
- `headless` <[boolean]> Whether to run browser in headless mode. More details for [Chromium](https://developers.google.com/web/updates/2017/04/headless-chrome) and [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode). Defaults to `true` unless the `devtools` option is `true`.
|
||||||
|
|
@ -3887,7 +3860,7 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
|
||||||
- `devtools` <[boolean]> **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
|
- `devtools` <[boolean]> **Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
|
||||||
- returns: <[Promise]<[BrowserContext]>> Promise which resolves to the browser app instance.
|
- returns: <[Promise]<[BrowserContext]>> Promise which resolves to the browser app instance.
|
||||||
|
|
||||||
Launches browser instance that uses persistent storage located at `userDataDir`. If `userDataDir` is not specified, temporary folder is created for the persistent storage. That folder is deleted when browser closes.
|
Launches browser instance that uses persistent storage located at `userDataDir`.
|
||||||
|
|
||||||
#### browserType.launchServer([options])
|
#### browserType.launchServer([options])
|
||||||
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
|
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
|
||||||
|
|
@ -4223,7 +4196,7 @@ const { chromium } = require('playwright');
|
||||||
(async () => {
|
(async () => {
|
||||||
const pathToExtension = require('path').join(__dirname, 'my-extension');
|
const pathToExtension = require('path').join(__dirname, 'my-extension');
|
||||||
const userDataDir = '/tmp/test-user-data-dir';
|
const userDataDir = '/tmp/test-user-data-dir';
|
||||||
const browserContext = await chromium.launchPersistent(userDataDir,{
|
const browserContext = await chromium.launchPersistentContext(userDataDir,{
|
||||||
headless: false,
|
headless: false,
|
||||||
args: [
|
args: [
|
||||||
`--disable-extensions-except=${pathToExtension}`,
|
`--disable-extensions-except=${pathToExtension}`,
|
||||||
|
|
|
||||||
|
|
@ -284,7 +284,7 @@ export class Response {
|
||||||
return this._finishedPromise;
|
return this._finishedPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer(): Promise<platform.BufferType> {
|
body(): Promise<platform.BufferType> {
|
||||||
if (!this._contentPromise) {
|
if (!this._contentPromise) {
|
||||||
this._contentPromise = this._finishedPromise.then(async error => {
|
this._contentPromise = this._finishedPromise.then(async error => {
|
||||||
if (error)
|
if (error)
|
||||||
|
|
@ -296,7 +296,7 @@ export class Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
async text(): Promise<string> {
|
async text(): Promise<string> {
|
||||||
const content = await this.buffer();
|
const content = await this.body();
|
||||||
return content.toString('utf8');
|
return content.toString('utf8');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ export interface BrowserType {
|
||||||
name(): string;
|
name(): string;
|
||||||
launch(options?: LaunchOptions & { slowMo?: number }): Promise<Browser>;
|
launch(options?: LaunchOptions & { slowMo?: number }): Promise<Browser>;
|
||||||
launchServer(options?: LaunchOptions & { port?: number }): Promise<BrowserServer>;
|
launchServer(options?: LaunchOptions & { port?: number }): Promise<BrowserServer>;
|
||||||
launchPersistent(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext>;
|
launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext>;
|
||||||
connect(options: ConnectOptions): Promise<Browser>;
|
connect(options: ConnectOptions): Promise<Browser>;
|
||||||
downloadBrowserIfNeeded(progress?: OnProgressCallback): Promise<void>;
|
downloadBrowserIfNeeded(progress?: OnProgressCallback): Promise<void>;
|
||||||
devices: types.Devices;
|
devices: types.Devices;
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ export class Chromium implements BrowserType {
|
||||||
|
|
||||||
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<CRBrowser> {
|
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<CRBrowser> {
|
||||||
if (options && (options as any).userDataDir)
|
if (options && (options as any).userDataDir)
|
||||||
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistent` instead');
|
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
||||||
const { browserServer, transport } = await this._launchServer(options, 'local');
|
const { browserServer, transport } = await this._launchServer(options, 'local');
|
||||||
const browser = await CRBrowser.connect(transport!, false, options && options.slowMo);
|
const browser = await CRBrowser.connect(transport!, false, options && options.slowMo);
|
||||||
(browser as any)['__server__'] = browserServer;
|
(browser as any)['__server__'] = browserServer;
|
||||||
|
|
@ -64,7 +64,7 @@ export class Chromium implements BrowserType {
|
||||||
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
|
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
async launchPersistent(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
async launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
||||||
const { timeout = 30000 } = options || {};
|
const { timeout = 30000 } = options || {};
|
||||||
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
|
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
|
||||||
const browser = await CRBrowser.connect(transport!, true);
|
const browser = await CRBrowser.connect(transport!, true);
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ export class Firefox implements BrowserType {
|
||||||
|
|
||||||
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<FFBrowser> {
|
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<FFBrowser> {
|
||||||
if (options && (options as any).userDataDir)
|
if (options && (options as any).userDataDir)
|
||||||
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistent` instead');
|
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
||||||
const browserServer = await this._launchServer(options, 'local');
|
const browserServer = await this._launchServer(options, 'local');
|
||||||
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
||||||
return FFBrowser.connect(transport, false, options && options.slowMo);
|
return FFBrowser.connect(transport, false, options && options.slowMo);
|
||||||
|
|
@ -77,7 +77,7 @@ export class Firefox implements BrowserType {
|
||||||
return await this._launchServer(options, 'server', undefined, options && options.port);
|
return await this._launchServer(options, 'server', undefined, options && options.port);
|
||||||
}
|
}
|
||||||
|
|
||||||
async launchPersistent(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
async launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
||||||
const { timeout = 30000 } = options || {};
|
const { timeout = 30000 } = options || {};
|
||||||
const browserServer = await this._launchServer(options, 'persistent', userDataDir);
|
const browserServer = await this._launchServer(options, 'persistent', userDataDir);
|
||||||
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ export class WebKit implements BrowserType {
|
||||||
|
|
||||||
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<WKBrowser> {
|
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<WKBrowser> {
|
||||||
if (options && (options as any).userDataDir)
|
if (options && (options as any).userDataDir)
|
||||||
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistent` instead');
|
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
||||||
const { browserServer, transport } = await this._launchServer(options, 'local');
|
const { browserServer, transport } = await this._launchServer(options, 'local');
|
||||||
const browser = await WKBrowser.connect(transport!, options && options.slowMo);
|
const browser = await WKBrowser.connect(transport!, options && options.slowMo);
|
||||||
(browser as any)['__server__'] = browserServer;
|
(browser as any)['__server__'] = browserServer;
|
||||||
|
|
@ -76,7 +76,7 @@ export class WebKit implements BrowserType {
|
||||||
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
|
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
async launchPersistent(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
async launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
||||||
const { timeout = 30000 } = options || {};
|
const { timeout = 30000 } = options || {};
|
||||||
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
|
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
|
||||||
const browser = await WKBrowser.connect(transport!, undefined, true);
|
const browser = await WKBrowser.connect(transport!, undefined, true);
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
describe('extensions', () => {
|
describe('extensions', () => {
|
||||||
it('should return background pages', async() => {
|
it('should return background pages', async() => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const context = await playwright.launchPersistent(userDataDir, extensionOptions);
|
const context = await playwright.launchPersistentContext(userDataDir, extensionOptions);
|
||||||
const backgroundPages = await context.backgroundPages();
|
const backgroundPages = await context.backgroundPages();
|
||||||
let backgroundPage = backgroundPages.length
|
let backgroundPage = backgroundPages.length
|
||||||
? backgroundPages[0]
|
? backgroundPages[0]
|
||||||
|
|
|
||||||
|
|
@ -25,10 +25,10 @@ module.exports.describe = function ({ testRunner, expect, defaultBrowserOptions,
|
||||||
const {it, fit, xit, dit} = testRunner;
|
const {it, fit, xit, dit} = testRunner;
|
||||||
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
||||||
|
|
||||||
describe('launchPersistent()', function() {
|
describe('launchPersistentContext()', function() {
|
||||||
beforeEach(async state => {
|
beforeEach(async state => {
|
||||||
state.userDataDir = await makeUserDataDir();
|
state.userDataDir = await makeUserDataDir();
|
||||||
state.browserContext = await playwright.launchPersistent(state.userDataDir, defaultBrowserOptions);
|
state.browserContext = await playwright.launchPersistentContext(state.userDataDir, defaultBrowserOptions);
|
||||||
state.page = await state.browserContext.newPage();
|
state.page = await state.browserContext.newPage();
|
||||||
});
|
});
|
||||||
afterEach(async state => {
|
afterEach(async state => {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
|
||||||
describe('Headful', function() {
|
describe('Headful', function() {
|
||||||
it('should have default url when launching browser', async function() {
|
it('should have default url when launching browser', async function() {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, headfulOptions);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
|
||||||
const pages = (await browserContext.pages()).map(page => page.url());
|
const pages = (await browserContext.pages()).map(page => page.url());
|
||||||
expect(pages).toEqual(['about:blank']);
|
expect(pages).toEqual(['about:blank']);
|
||||||
await browserContext.close();
|
await browserContext.close();
|
||||||
|
|
@ -44,13 +44,13 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
|
||||||
it.fail((WIN && CHROMIUM) || FFOX)('headless should be able to read cookies written by headful', async({server}) => {
|
it.fail((WIN && CHROMIUM) || FFOX)('headless should be able to read cookies written by headful', async({server}) => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
// Write a cookie in headful chrome
|
// Write a cookie in headful chrome
|
||||||
const headfulContext = await playwright.launchPersistent(userDataDir, headfulOptions);
|
const headfulContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
|
||||||
const headfulPage = await headfulContext.newPage();
|
const headfulPage = await headfulContext.newPage();
|
||||||
await headfulPage.goto(server.EMPTY_PAGE);
|
await headfulPage.goto(server.EMPTY_PAGE);
|
||||||
await headfulPage.evaluate(() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
await headfulPage.evaluate(() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
||||||
await headfulContext.close();
|
await headfulContext.close();
|
||||||
// Read the cookie from headless chrome
|
// Read the cookie from headless chrome
|
||||||
const headlessContext = await playwright.launchPersistent(userDataDir, headlessOptions);
|
const headlessContext = await playwright.launchPersistentContext(userDataDir, headlessOptions);
|
||||||
const headlessPage = await headlessContext.newPage();
|
const headlessPage = await headlessContext.newPage();
|
||||||
await headlessPage.goto(server.EMPTY_PAGE);
|
await headlessPage.goto(server.EMPTY_PAGE);
|
||||||
const cookie = await headlessPage.evaluate(() => document.cookie);
|
const cookie = await headlessPage.evaluate(() => document.cookie);
|
||||||
|
|
@ -61,7 +61,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
|
||||||
});
|
});
|
||||||
it.fail(FFOX)('should close browser with beforeunload page', async({server}) => {
|
it.fail(FFOX)('should close browser with beforeunload page', async({server}) => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, headfulOptions);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
|
||||||
const page = await browserContext.newPage();
|
const page = await browserContext.newPage();
|
||||||
await page.goto(server.PREFIX + '/beforeunload.html');
|
await page.goto(server.PREFIX + '/beforeunload.html');
|
||||||
// We have to interact with a page so that 'beforeunload' handlers
|
// We have to interact with a page so that 'beforeunload' handlers
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
let waitError = null;
|
let waitError = null;
|
||||||
const options = Object.assign({}, defaultBrowserOptions, {userDataDir: 'random-path'});
|
const options = Object.assign({}, defaultBrowserOptions, {userDataDir: 'random-path'});
|
||||||
await playwright.launch(options).catch(e => waitError = e);
|
await playwright.launch(options).catch(e => waitError = e);
|
||||||
expect(waitError.message).toContain('launchPersistent');
|
expect(waitError.message).toContain('launchPersistentContext');
|
||||||
});
|
});
|
||||||
it('should throw if page argument is passed', async() => {
|
it('should throw if page argument is passed', async() => {
|
||||||
let waitError = null;
|
let waitError = null;
|
||||||
|
|
@ -59,10 +59,10 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Playwright.launchPersistent', function() {
|
describe('Playwright.launchPersistentContext', function() {
|
||||||
it('should have default URL when launching browser', async function() {
|
it('should have default URL when launching browser', async function() {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, defaultBrowserOptions);
|
||||||
const pages = (await browserContext.pages()).map(page => page.url());
|
const pages = (await browserContext.pages()).map(page => page.url());
|
||||||
expect(pages).toEqual(['about:blank']);
|
expect(pages).toEqual(['about:blank']);
|
||||||
await browserContext.close();
|
await browserContext.close();
|
||||||
|
|
@ -72,7 +72,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const options = Object.assign({}, defaultBrowserOptions);
|
const options = Object.assign({}, defaultBrowserOptions);
|
||||||
options.args = [server.EMPTY_PAGE].concat(options.args || []);
|
options.args = [server.EMPTY_PAGE].concat(options.args || []);
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, options);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, options);
|
||||||
const pages = await browserContext.pages();
|
const pages = await browserContext.pages();
|
||||||
expect(pages.length).toBe(1);
|
expect(pages.length).toBe(1);
|
||||||
const page = pages[0];
|
const page = pages[0];
|
||||||
|
|
@ -278,11 +278,11 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Playwright.launchPersistent', function() {
|
describe('Playwright.launchPersistentContext', function() {
|
||||||
it('userDataDir option', async({server}) => {
|
it('userDataDir option', async({server}) => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const options = Object.assign(defaultBrowserOptions);
|
const options = Object.assign(defaultBrowserOptions);
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, options);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, options);
|
||||||
// Open a page to make sure its functional.
|
// Open a page to make sure its functional.
|
||||||
await browserContext.newPage();
|
await browserContext.newPage();
|
||||||
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
||||||
|
|
@ -293,20 +293,20 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
});
|
});
|
||||||
it.fail(FFOX)('userDataDir option should restore state', async({server}) => {
|
it.fail(FFOX)('userDataDir option should restore state', async({server}) => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, defaultBrowserOptions);
|
||||||
const page = await browserContext.newPage();
|
const page = await browserContext.newPage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
await page.evaluate(() => localStorage.hey = 'hello');
|
await page.evaluate(() => localStorage.hey = 'hello');
|
||||||
await browserContext.close();
|
await browserContext.close();
|
||||||
|
|
||||||
const browserContext2 = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
|
const browserContext2 = await playwright.launchPersistentContext(userDataDir, defaultBrowserOptions);
|
||||||
const page2 = await browserContext2.newPage();
|
const page2 = await browserContext2.newPage();
|
||||||
await page2.goto(server.EMPTY_PAGE);
|
await page2.goto(server.EMPTY_PAGE);
|
||||||
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
|
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
|
||||||
await browserContext2.close();
|
await browserContext2.close();
|
||||||
|
|
||||||
const userDataDir2 = await makeUserDataDir();
|
const userDataDir2 = await makeUserDataDir();
|
||||||
const browserContext3 = await playwright.launchPersistent(userDataDir2, defaultBrowserOptions);
|
const browserContext3 = await playwright.launchPersistentContext(userDataDir2, defaultBrowserOptions);
|
||||||
const page3 = await browserContext3.newPage();
|
const page3 = await browserContext3.newPage();
|
||||||
await page3.goto(server.EMPTY_PAGE);
|
await page3.goto(server.EMPTY_PAGE);
|
||||||
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('hello');
|
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('hello');
|
||||||
|
|
@ -319,20 +319,20 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
|
||||||
// See https://github.com/microsoft/playwright/issues/717
|
// See https://github.com/microsoft/playwright/issues/717
|
||||||
it.fail(FFOX || (WIN && CHROMIUM))('userDataDir option should restore cookies', async({server}) => {
|
it.fail(FFOX || (WIN && CHROMIUM))('userDataDir option should restore cookies', async({server}) => {
|
||||||
const userDataDir = await makeUserDataDir();
|
const userDataDir = await makeUserDataDir();
|
||||||
const browserContext = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
|
const browserContext = await playwright.launchPersistentContext(userDataDir, defaultBrowserOptions);
|
||||||
const page = await browserContext.newPage();
|
const page = await browserContext.newPage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
||||||
await browserContext.close();
|
await browserContext.close();
|
||||||
|
|
||||||
const browserContext2 = await playwright.launchPersistent(userDataDir, defaultBrowserOptions);
|
const browserContext2 = await playwright.launchPersistentContext(userDataDir, defaultBrowserOptions);
|
||||||
const page2 = await browserContext2.newPage();
|
const page2 = await browserContext2.newPage();
|
||||||
await page2.goto(server.EMPTY_PAGE);
|
await page2.goto(server.EMPTY_PAGE);
|
||||||
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
|
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
|
||||||
await browserContext2.close();
|
await browserContext2.close();
|
||||||
|
|
||||||
const userDataDir2 = await makeUserDataDir();
|
const userDataDir2 = await makeUserDataDir();
|
||||||
const browserContext3 = await playwright.launchPersistent(userDataDir2, defaultBrowserOptions);
|
const browserContext3 = await playwright.launchPersistentContext(userDataDir2, defaultBrowserOptions);
|
||||||
const page3 = await browserContext3.newPage();
|
const page3 = await browserContext3.newPage();
|
||||||
await page3.goto(server.EMPTY_PAGE);
|
await page3.goto(server.EMPTY_PAGE);
|
||||||
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('doSomethingOnlyOnce=true');
|
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('doSomethingOnlyOnce=true');
|
||||||
|
|
|
||||||
|
|
@ -180,18 +180,18 @@ module.exports.describe = function({testRunner, expect, MAC, WIN, FFOX, CHROMIUM
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Response.buffer', function() {
|
describe('Response.body', function() {
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
const response = await page.goto(server.PREFIX + '/pptr.png');
|
const response = await page.goto(server.PREFIX + '/pptr.png');
|
||||||
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
||||||
const responseBuffer = await response.buffer();
|
const responseBuffer = await response.body();
|
||||||
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
||||||
});
|
});
|
||||||
it('should work with compression', async({page, server}) => {
|
it('should work with compression', async({page, server}) => {
|
||||||
server.enableGzip('/pptr.png');
|
server.enableGzip('/pptr.png');
|
||||||
const response = await page.goto(server.PREFIX + '/pptr.png');
|
const response = await page.goto(server.PREFIX + '/pptr.png');
|
||||||
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
||||||
const responseBuffer = await response.buffer();
|
const responseBuffer = await response.body();
|
||||||
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
expect(responseBuffer.equals(imageBuffer)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue