Skip to content

Commit 9aa56a6

Browse files
authored
api(browserType): remove devices, errors (#1368)
1 parent 0d7cb29 commit 9aa56a6

17 files changed

+122
-203
lines changed

docs/api.md

-46
Original file line numberDiff line numberDiff line change
@@ -3706,9 +3706,7 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
37063706

37073707
<!-- GEN:toc -->
37083708
- [browserType.connect(options)](#browsertypeconnectoptions)
3709-
- [browserType.devices](#browsertypedevices)
37103709
- [browserType.downloadBrowserIfNeeded([progress])](#browsertypedownloadbrowserifneededprogress)
3711-
- [browserType.errors](#browsertypeerrors)
37123710
- [browserType.executablePath()](#browsertypeexecutablepath)
37133711
- [browserType.launch([options])](#browsertypelaunchoptions)
37143712
- [browserType.launchPersistentContext(userDataDir, [options])](#browsertypelaunchpersistentcontextuserdatadir-options)
@@ -3724,56 +3722,12 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
37243722

37253723
This methods attaches Playwright to an existing browser instance.
37263724

3727-
#### browserType.devices
3728-
- returns: <[Object]>
3729-
3730-
Returns a list of devices to be used with [`browser.newContext([options])`](#browsernewcontextoptions) and [`browser.newPage([options])`](#browsernewpageoptions). Actual list of devices can be found in [src/deviceDescriptors.ts](https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts).
3731-
3732-
```js
3733-
const { webkit } = require('playwright');
3734-
const iPhone = webkit.devices['iPhone 6'];
3735-
3736-
(async () => {
3737-
const browser = await webkit.launch();
3738-
const context = await browser.newContext({
3739-
viewport: iPhone.viewport,
3740-
userAgent: iPhone.userAgent
3741-
});
3742-
const page = await context.newPage();
3743-
await page.goto('https://example.com');
3744-
// other actions...
3745-
await browser.close();
3746-
})();
3747-
```
3748-
37493725
#### browserType.downloadBrowserIfNeeded([progress])
37503726
- `progress` <[function]> If download is initiated, this function is called with two parameters: `downloadedBytes` and `totalBytes`.
37513727
- returns: <[Promise]> promise that resolves when browser is successfully downloaded.
37523728

37533729
Download browser binary if it is missing.
37543730

3755-
#### browserType.errors
3756-
- returns: <[Object]>
3757-
- `TimeoutError` <[function]> A class of [TimeoutError].
3758-
3759-
Playwright methods might throw errors if they are unable to fulfill a request. For example, [page.waitForSelector(selector[, options])](#pagewaitforelementselector-options)
3760-
might fail if the selector doesn't match any nodes during the given timeframe.
3761-
3762-
For certain types of errors Playwright uses specific error classes.
3763-
These classes are available via [`browserType.errors`](#browsertypeerrors) or [`playwright.errors`](#playwrighterrors).
3764-
3765-
An example of handling a timeout error:
3766-
```js
3767-
const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
3768-
try {
3769-
await page.waitForSelector('.foo');
3770-
} catch (e) {
3771-
if (e instanceof webkit.errors.TimeoutError) {
3772-
// Do something if this is a timeout.
3773-
}
3774-
}
3775-
```
3776-
37773731
#### browserType.executablePath()
37783732
- returns: <[string]> A path where Playwright expects to find a bundled browser.
37793733

src/server/browserType.ts

-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as types from '../types';
18-
import { TimeoutError } from '../errors';
1917
import { Browser, ConnectOptions } from '../browser';
2018
import { BrowserContext } from '../browserContext';
2119
import { BrowserServer } from './browserServer';
@@ -50,6 +48,4 @@ export interface BrowserType {
5048
launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext>;
5149
connect(options: ConnectOptions): Promise<Browser>;
5250
downloadBrowserIfNeeded(progress?: OnProgressCallback): Promise<void>;
53-
devices: types.Devices;
54-
errors: { TimeoutError: typeof TimeoutError };
5551
}

src/server/chromium.ts

-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import * as os from 'os';
2020
import * as path from 'path';
2121
import * as util from 'util';
2222
import { BrowserFetcher, OnProgressCallback, BrowserFetcherOptions } from '../server/browserFetcher';
23-
import { DeviceDescriptors } from '../deviceDescriptors';
24-
import * as types from '../types';
2523
import { assert, helper } from '../helper';
2624
import { CRBrowser } from '../chromium/crBrowser';
2725
import * as platform from '../platform';
@@ -166,14 +164,6 @@ export class Chromium implements BrowserType {
166164
return this._resolveExecutablePath().executablePath;
167165
}
168166

169-
get devices(): types.Devices {
170-
return DeviceDescriptors;
171-
}
172-
173-
get errors(): { TimeoutError: typeof TimeoutError } {
174-
return { TimeoutError };
175-
}
176-
177167
private _defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] {
178168
const {
179169
devtools = false,

src/server/firefox.ts

-10
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import * as path from 'path';
2121
import * as util from 'util';
2222
import { ConnectOptions, LaunchType } from '../browser';
2323
import { BrowserContext } from '../browserContext';
24-
import { DeviceDescriptors } from '../deviceDescriptors';
2524
import { TimeoutError } from '../errors';
2625
import { Events } from '../events';
2726
import { FFBrowser } from '../firefox/ffBrowser';
2827
import { kBrowserCloseMessageId } from '../firefox/ffConnection';
2928
import { assert, helper } from '../helper';
3029
import * as platform from '../platform';
31-
import * as types from '../types';
3230
import { BrowserFetcher, BrowserFetcherOptions, OnProgressCallback } from './browserFetcher';
3331
import { BrowserServer } from './browserServer';
3432
import { BrowserArgOptions, BrowserType, LaunchOptions } from './browserType';
@@ -174,14 +172,6 @@ export class Firefox implements BrowserType {
174172
return this._resolveExecutablePath().executablePath;
175173
}
176174

177-
get devices(): types.Devices {
178-
return DeviceDescriptors;
179-
}
180-
181-
get errors(): { TimeoutError: typeof TimeoutError } {
182-
return { TimeoutError };
183-
}
184-
185175
private _defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] {
186176
const {
187177
devtools = false,

src/server/webkit.ts

-11
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
*/
1717

1818
import { BrowserFetcher, OnProgressCallback, BrowserFetcherOptions } from './browserFetcher';
19-
import { DeviceDescriptors } from '../deviceDescriptors';
20-
import { TimeoutError } from '../errors';
21-
import * as types from '../types';
2219
import { WKBrowser } from '../webkit/wkBrowser';
2320
import { execSync } from 'child_process';
2421
import { PipeTransport } from './pipeTransport';
@@ -163,14 +160,6 @@ export class WebKit implements BrowserType {
163160
return this._resolveExecutablePath().executablePath;
164161
}
165162

166-
get devices(): types.Devices {
167-
return DeviceDescriptors;
168-
}
169-
170-
get errors(): { TimeoutError: typeof TimeoutError } {
171-
return { TimeoutError };
172-
}
173-
174163
_defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] {
175164
const {
176165
devtools = false,

test/chromium/launcher.spec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const TMP_FOLDER = path.join(os.tmpdir(), 'pw_tmp_folder-');
2929
/**
3030
* @type {TestSuite}
3131
*/
32-
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, WIN}) {
32+
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, browserType, WIN}) {
3333
const {describe, xdescribe, fdescribe} = testRunner;
3434
const {it, fit, xit, dit} = testRunner;
3535
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -50,17 +50,17 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
5050
it('should throw with remote-debugging-pipe argument', async() => {
5151
const options = Object.assign({}, defaultBrowserOptions);
5252
options.args = ['--remote-debugging-pipe'].concat(options.args || []);
53-
const error = await playwright.launchServer(options).catch(e => e);
53+
const error = await browserType.launchServer(options).catch(e => e);
5454
expect(error.message).toContain('Playwright manages remote debugging connection itself');
5555
});
5656
it('should throw with remote-debugging-port argument', async() => {
5757
const options = Object.assign({}, defaultBrowserOptions);
5858
options.args = ['--remote-debugging-port=9222'].concat(options.args || []);
59-
const error = await playwright.launchServer(options).catch(e => e);
59+
const error = await browserType.launchServer(options).catch(e => e);
6060
expect(error.message).toContain('Playwright manages remote debugging connection itself');
6161
});
6262
it('should open devtools when "devtools: true" option is given', async({server}) => {
63-
const browser = await playwright.launch(Object.assign({devtools: true}, headfulOptions));
63+
const browser = await browserType.launch(Object.assign({devtools: true}, headfulOptions));
6464
const context = await browser.newContext();
6565
const browserSession = await browser.createBrowserSession();
6666
await browserSession.send('Target.setDiscoverTargets', { discover: true });
@@ -79,7 +79,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
7979
describe('extensions', () => {
8080
it('should return background pages', async() => {
8181
const userDataDir = await makeUserDataDir();
82-
const context = await playwright.launchPersistentContext(userDataDir, extensionOptions);
82+
const context = await browserType.launchPersistentContext(userDataDir, extensionOptions);
8383
const backgroundPages = await context.backgroundPages();
8484
let backgroundPage = backgroundPages.length
8585
? backgroundPages[0]
@@ -94,7 +94,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
9494
describe('BrowserFetcher', function() {
9595
it('should download and extract linux binary', async({server}) => {
9696
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
97-
const browserFetcher = playwright._createBrowserFetcher({
97+
const browserFetcher = browserType._createBrowserFetcher({
9898
platform: 'linux',
9999
path: downloadsFolder,
100100
host: server.PREFIX
@@ -123,7 +123,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
123123

124124
describe('BrowserContext', function() {
125125
it('should not create pages automatically', async function() {
126-
const browser = await playwright.launch();
126+
const browser = await browserType.launch();
127127
const browserSession = await browser.createBrowserSession();
128128
const targets = [];
129129
browserSession.on('Target.targetCreated', async ({targetInfo}) => {

test/chromium/oopif.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @type {ChromiumTestSuite}
1919
*/
20-
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, FFOX, CHROMIUM, WEBKIT}) {
20+
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, browserType, FFOX, CHROMIUM, WEBKIT}) {
2121
const {describe, xdescribe, fdescribe} = testRunner;
2222
const {it, fit, xit, dit} = testRunner;
2323
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -28,7 +28,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
2828

2929
describe('OOPIF', function() {
3030
beforeAll(async function(state) {
31-
state.browser = await playwright.launch(Object.assign({}, defaultBrowserOptions, {
31+
state.browser = await browserType.launch(Object.assign({}, defaultBrowserOptions, {
3232
args: (defaultBrowserOptions.args || []).concat(['--site-per-process']),
3333
}));
3434
});
@@ -65,7 +65,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
6565
it.fail(true)('should report google.com frame with headful', async({server}) => {
6666
// TODO: Support OOOPIF. @see https://github.com/GoogleChrome/puppeteer/issues/2548
6767
// https://google.com is isolated by default in Chromium embedder.
68-
const browser = await playwright.launch(headfulOptions);
68+
const browser = await browserType.launch(headfulOptions);
6969
const page = await browser.newPage();
7070
await page.goto(server.EMPTY_PAGE);
7171
await page.route('**/*', request => {

test/chromium/tracing.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const path = require('path');
2020
/**
2121
* @type {ChromiumTestSuite}
2222
*/
23-
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, ASSETS_DIR}) {
23+
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, browserType, ASSETS_DIR}) {
2424
const {describe, xdescribe, fdescribe} = testRunner;
2525
const {it, fit, xit, dit} = testRunner;
2626
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
2727

2828
describe('Chromium.startTracing', function() {
2929
beforeEach(async function(state) {
3030
state.outputFile = path.join(ASSETS_DIR, `trace-${state.parallelIndex}.json`);
31-
state.browser = await playwright.launch(defaultBrowserOptions);
31+
state.browser = await browserType.launch(defaultBrowserOptions);
3232
state.page = await state.browser.newPage();
3333
});
3434
afterEach(async function(state) {

test/cookies.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @type {PageTestSuite}
2020
*/
21-
module.exports.describe = function({testRunner, expect, playwright, defaultBrowserOptions, MAC, FFOX, CHROMIUM, WEBKIT}) {
21+
module.exports.describe = function({testRunner, expect, browserType, defaultBrowserOptions, MAC, FFOX, CHROMIUM, WEBKIT}) {
2222
const {describe, xdescribe, fdescribe} = testRunner;
2323
const {it, fit, xit, dit} = testRunner;
2424
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -276,12 +276,12 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
276276
}
277277
});
278278
it.slow()('should isolate cookies between launches', async({server}) => {
279-
const browser1 = await playwright.launch(defaultBrowserOptions);
279+
const browser1 = await browserType.launch(defaultBrowserOptions);
280280
const context1 = await browser1.newContext();
281281
await context1.addCookies([{url: server.EMPTY_PAGE, name: 'cookie-in-context-1', value: 'value', expires: Date.now() / 1000 + 10000}]);
282282
await browser1.close();
283283

284-
const browser2 = await playwright.launch(defaultBrowserOptions);
284+
const browser2 = await browserType.launch(defaultBrowserOptions);
285285
const context2 = await browser2.newContext();
286286
const cookies = await context2.cookies();
287287
expect(cookies.length).toBe(0);

test/defaultbrowsercontext.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const { makeUserDataDir, removeUserDataDir } = require('./utils');
2020
/**
2121
* @type {PageTestSuite}
2222
*/
23-
module.exports.describe = function ({ testRunner, expect, defaultBrowserOptions, playwright, WEBKIT }) {
23+
module.exports.describe = function ({ testRunner, expect, defaultBrowserOptions, browserType, WEBKIT }) {
2424
const {describe, xdescribe, fdescribe} = testRunner;
2525
const {it, fit, xit, dit} = testRunner;
2626
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
2727

2828
describe('launchPersistentContext()', function() {
2929
beforeEach(async state => {
3030
state.userDataDir = await makeUserDataDir();
31-
state.browserContext = await playwright.launchPersistentContext(state.userDataDir, defaultBrowserOptions);
31+
state.browserContext = await browserType.launchPersistentContext(state.userDataDir, defaultBrowserOptions);
3232
state.page = await state.browserContext.newPage();
3333
});
3434
afterEach(async state => {

test/fixtures.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const {spawn, execSync} = require('child_process');
2121
/**
2222
* @type {TestSuite}
2323
*/
24-
module.exports.describe = function({testRunner, expect, product, playwright, playwrightPath, defaultBrowserOptions, WIN, FFOX, CHROMIUM, WEBKIT}) {
24+
module.exports.describe = function({testRunner, expect, product, browserType, playwrightPath, defaultBrowserOptions, WIN, FFOX, CHROMIUM, WEBKIT}) {
2525
const {describe, xdescribe, fdescribe} = testRunner;
2626
const {it, fit, xit, dit} = testRunner;
2727
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -58,7 +58,7 @@ module.exports.describe = function({testRunner, expect, product, playwright, pla
5858
browserPid = +match[1];
5959
});
6060
res.on('error', (...args) => console.log("ERROR", ...args));
61-
const browser = await playwright.connect({ wsEndpoint: await wsEndPointPromise });
61+
const browser = await browserType.connect({ wsEndpoint: await wsEndPointPromise });
6262
const promises = [
6363
new Promise(resolve => browser.once('disconnected', resolve)),
6464
new Promise(resolve => res.on('exit', resolve)),

test/headful.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const { makeUserDataDir, removeUserDataDir } = require('./utils');
1919
/**
2020
* @type {TestSuite}
2121
*/
22-
module.exports.describe = function({testRunner, expect, playwright, defaultBrowserOptions, FFOX, CHROMIUM, WEBKIT, WIN}) {
22+
module.exports.describe = function({testRunner, expect, browserType, defaultBrowserOptions, FFOX, CHROMIUM, WEBKIT, WIN}) {
2323
const {describe, xdescribe, fdescribe} = testRunner;
2424
const {it, fit, xit, dit} = testRunner;
2525
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -34,7 +34,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
3434
describe('Headful', function() {
3535
it('should have default url when launching browser', async function() {
3636
const userDataDir = await makeUserDataDir();
37-
const browserContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
37+
const browserContext = await browserType.launchPersistentContext(userDataDir, headfulOptions);
3838
const pages = (await browserContext.pages()).map(page => page.url());
3939
expect(pages).toEqual(['about:blank']);
4040
await browserContext.close();
@@ -44,13 +44,13 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
4444
it.fail((WIN && CHROMIUM) || FFOX)('headless should be able to read cookies written by headful', async({server}) => {
4545
const userDataDir = await makeUserDataDir();
4646
// Write a cookie in headful chrome
47-
const headfulContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
47+
const headfulContext = await browserType.launchPersistentContext(userDataDir, headfulOptions);
4848
const headfulPage = await headfulContext.newPage();
4949
await headfulPage.goto(server.EMPTY_PAGE);
5050
await headfulPage.evaluate(() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
5151
await headfulContext.close();
5252
// Read the cookie from headless chrome
53-
const headlessContext = await playwright.launchPersistentContext(userDataDir, headlessOptions);
53+
const headlessContext = await browserType.launchPersistentContext(userDataDir, headlessOptions);
5454
const headlessPage = await headlessContext.newPage();
5555
await headlessPage.goto(server.EMPTY_PAGE);
5656
const cookie = await headlessPage.evaluate(() => document.cookie);
@@ -61,7 +61,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
6161
});
6262
it.fail(FFOX)('should close browser with beforeunload page', async({server}) => {
6363
const userDataDir = await makeUserDataDir();
64-
const browserContext = await playwright.launchPersistentContext(userDataDir, headfulOptions);
64+
const browserContext = await browserType.launchPersistentContext(userDataDir, headfulOptions);
6565
const page = await browserContext.newPage();
6666
await page.goto(server.PREFIX + '/beforeunload.html');
6767
// We have to interact with a page so that 'beforeunload' handlers

0 commit comments

Comments
 (0)