Skip to content

Commit

Permalink
feat(api): rename browserContext() to context() in the apis, remove u…
Browse files Browse the repository at this point in the history
…rl from newPage
  • Loading branch information
pavelfeldman committed Feb 10, 2020
1 parent 4d84e35 commit 5ae15c4
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 117 deletions.
91 changes: 50 additions & 41 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const { chromium, firefox, webkit } = require('playwright');

(async () => {
const browser = await chromium.launch(); // Or 'firefox' or 'webkit'.
const page = await browser.newPage('http://example.com');
const page = await browser.newPage();
await page.goto('http://example.com');
// other actions...
await browser.close();
})();
Expand Down Expand Up @@ -81,7 +82,8 @@ const iPhone = devices['iPhone 6'];
viewport: iPhone.viewport,
userAgent: iPhone.userAgent
});
const page = await context.newPage('http://example.com');
const page = await context.newPage();
await page.goto('http://example.com');
// other actions...
await browser.close();
})();
Expand Down Expand Up @@ -136,7 +138,8 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.

(async () => {
const browser = await firefox.launch();
const page = await browser.newPage('https://example.com');
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
})();
```
Expand All @@ -145,11 +148,11 @@ See [ChromiumBrowser], [FirefoxBrowser] and [WebKitBrowser] for browser-specific

<!-- GEN:toc -->
- [event: 'disconnected'](#event-disconnected)
- [browser.browserContexts()](#browserbrowsercontexts)
- [browser.close()](#browserclose)
- [browser.contexts()](#browsercontexts)
- [browser.isConnected()](#browserisconnected)
- [browser.newContext(options)](#browsernewcontextoptions)
- [browser.newPage(url, [options])](#browsernewpageurl-options)
- [browser.newPage([options])](#browsernewpageoptions)
- [browser.pages()](#browserpages)
<!-- GEN:stop -->

Expand All @@ -158,12 +161,6 @@ Emitted when Browser gets disconnected from the browser application. This might
- Browser application is closed or crashed.
- The [`browser.disconnect`](#browserdisconnect) method was called.

#### browser.browserContexts()
- returns: <[Array]<[BrowserContext]>>

Returns an array of all open browser contexts. In a newly created browser, this will return
a single instance of [BrowserContext].

#### browser.close()
- returns: <[Promise]>

Expand All @@ -173,6 +170,12 @@ In case this browser is obtained using [browserType.connect](#browsertypeconnect

The [Browser] object itself is considered to be disposed and cannot be used anymore.

#### browser.contexts()
- returns: <[Array]<[BrowserContext]>>

Returns an array of all open browser contexts. In a newly created browser, this will return
a single instance of [BrowserContext].

#### browser.isConnected()

- returns: <[boolean]>
Expand Down Expand Up @@ -206,12 +209,12 @@ Creates a new browser context. It won't share cookies/cache with other browser c
// Create a new incognito browser context.
const context = await browser.newContext();
// Create a new page in a pristine context.
const page = await context.newPage('https://example.com');
const page = await context.newPage();
await page.goto('https://example.com');
})();
```

#### browser.newPage(url, [options])
- `url` <?[string]> Optional url to navigate the page to.
#### browser.newPage([options])
- `options` <[Object]>
- `ignoreHTTPSErrors` <?[boolean]> Whether to ignore HTTPS errors during navigation. Defaults to `false`.
- `bypassCSP` <?[boolean]> Toggles bypassing page's Content-Security-Policy.
Expand All @@ -230,7 +233,7 @@ Creates a new browser context. It won't share cookies/cache with other browser c
- `permissions` <[Object]> A map from origin keys to permissions values. See [browserContext.setPermissions](#browsercontextsetpermissionsorigin-permissions) for more details.
- returns: <[Promise]<[Page]>>

Creates a new page in a new browser context and optionally navigates it to the specified URL.
Creates a new page in a new browser context.

#### browser.pages()
- returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages.
Expand All @@ -253,7 +256,8 @@ Playwright allows creation of "incognito" browser contexts with `browser.newCont
// Create a new incognito browser context
const context = await browser.newContext();
// Create a new page inside context.
const page = await context.newPage('https://example.com');
const page = await context.newPage();
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();
```
Expand All @@ -263,7 +267,7 @@ await context.close();
- [browserContext.clearPermissions()](#browsercontextclearpermissions)
- [browserContext.close()](#browsercontextclose)
- [browserContext.cookies([...urls])](#browsercontextcookiesurls)
- [browserContext.newPage(url)](#browsercontextnewpageurl)
- [browserContext.newPage()](#browsercontextnewpage)
- [browserContext.pages()](#browsercontextpages)
- [browserContext.setCookies(cookies)](#browsercontextsetcookiescookies)
- [browserContext.setGeolocation(geolocation)](#browsercontextsetgeolocationgeolocation)
Expand Down Expand Up @@ -314,11 +318,10 @@ If URLs are specified, only cookies that affect those URLs are returned.

> **NOTE** the default browser context cannot be closed.
#### browserContext.newPage(url)
- `url` <?[string]> Optional url to navigate the page to.
#### browserContext.newPage()
- returns: <[Promise]<[Page]>>

Creates a new page in the browser context and optionally navigates it to the specified URL.
Creates a new page in the browser context.

#### browserContext.pages()
- returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages. Non visible pages, such as `"background_page"`, will not be listed here. You can find them using [target.page()](#targetpage).
Expand Down Expand Up @@ -397,7 +400,8 @@ const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
(async () => {
const browser = await webkit.launch();
const context = await browser.newContext();
const page = await context.newPage('https://example.com');
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'screenshot.png'});
await browser.close();
})();
Expand Down Expand Up @@ -449,11 +453,11 @@ page.removeListener('request', logRequest);
- [page.addScriptTag(options)](#pageaddscripttagoptions)
- [page.addStyleTag(options)](#pageaddstyletagoptions)
- [page.authenticate(credentials)](#pageauthenticatecredentials)
- [page.browserContext()](#pagebrowsercontext)
- [page.check(selector, [options])](#pagecheckselector-options)
- [page.click(selector[, options])](#pageclickselector-options)
- [page.close([options])](#pagecloseoptions)
- [page.content()](#pagecontent)
- [page.context()](#pagecontext)
- [page.coverage](#pagecoverage)
- [page.dblclick(selector[, options])](#pagedblclickselector-options)
- [page.emulateMedia(options)](#pageemulatemediaoptions)
Expand Down Expand Up @@ -729,12 +733,6 @@ Provide credentials for [HTTP authentication](https://developer.mozilla.org/en-U

To disable authentication, pass `null`.

#### page.browserContext()

- returns: <[BrowserContext]>

Get the browser context that the page belongs to.

#### page.check(selector, [options])
- `selector` <[string]> A selector to search for checkbox or radio button to check. If there are multiple elements satisfying the selector, the first will be checked.
- `options` <[Object]>
Expand Down Expand Up @@ -792,6 +790,12 @@ By default, `page.close()` **does not** run beforeunload handlers.

Gets the full HTML contents of the page, including the doctype.

#### page.context()

- returns: <[BrowserContext]>

Get the browser context that the page belongs to.

#### page.coverage

- returns: <[Coverage]>
Expand Down Expand Up @@ -1633,7 +1637,8 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.

(async () => {
const browser = await firefox.launch();
const page = await browser.newPage('https://www.google.com/chrome/browser/canary.html');
const page = await browser.newPage();
await page.goto('https://www.google.com/chrome/browser/canary.html');
dumpFrameTree(page.mainFrame(), '');
await browser.close();

Expand Down Expand Up @@ -2224,7 +2229,8 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage('https://example.com');
const page = await browser.newPage();
await page.goto('https://example.com');
const hrefElement = await page.$('a');
await hrefElement.click();
// ...
Expand Down Expand Up @@ -3143,7 +3149,8 @@ const { selectors, firefox } = require('playwright'); // Or 'chromium' or 'webk
await selectors.register(createTagNameEngine);

const browser = await firefox.launch();
const page = await browser.newPage('http://example.com');
const page = await browser.newPage();
await page.goto('https://example.com');

// Use the selector prefixed with its name.
const button = await page.$('tag=button');
Expand Down Expand Up @@ -3453,7 +3460,8 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage('http://example.com');
const page = await browser.newPage();
await page.goto('https://example.com');
// other actions...
await browser.close();
})();
Expand Down Expand Up @@ -3493,7 +3501,8 @@ const iPhone = webkit.devices['iPhone 6'];
viewport: iPhone.viewport,
userAgent: iPhone.userAgent
});
const page = await context.newPage('http://example.com');
const page = await context.newPage();
await page.goto('https://example.com');
// other actions...
await browser.close();
})();
Expand Down Expand Up @@ -3638,11 +3647,11 @@ await browser.stopTracing();
<!-- GEN:stop -->
<!-- GEN:toc-extends-Browser -->
- [event: 'disconnected'](#event-disconnected)
- [browser.browserContexts()](#browserbrowsercontexts)
- [browser.close()](#browserclose)
- [browser.contexts()](#browsercontexts)
- [browser.isConnected()](#browserisconnected)
- [browser.newContext(options)](#browsernewcontextoptions)
- [browser.newPage(url, [options])](#browsernewpageurl-options)
- [browser.newPage([options])](#browsernewpageoptions)
- [browser.pages()](#browserpages)
<!-- GEN:stop -->

Expand Down Expand Up @@ -3760,15 +3769,15 @@ to send messages.


<!-- GEN:toc -->
- [chromiumTarget.browserContext()](#chromiumtargetbrowsercontext)
- [chromiumTarget.context()](#chromiumtargetcontext)
- [chromiumTarget.createCDPSession()](#chromiumtargetcreatecdpsession)
- [chromiumTarget.opener()](#chromiumtargetopener)
- [chromiumTarget.page()](#chromiumtargetpage)
- [chromiumTarget.type()](#chromiumtargettype)
- [chromiumTarget.url()](#chromiumtargeturl)
<!-- GEN:stop -->

#### chromiumTarget.browserContext()
#### chromiumTarget.context()

- returns: <[BrowserContext]>

Expand Down Expand Up @@ -3805,11 +3814,11 @@ Firefox browser instance does not expose Firefox-specific features.

<!-- GEN:toc-extends-Browser -->
- [event: 'disconnected'](#event-disconnected)
- [browser.browserContexts()](#browserbrowsercontexts)
- [browser.close()](#browserclose)
- [browser.contexts()](#browsercontexts)
- [browser.isConnected()](#browserisconnected)
- [browser.newContext(options)](#browsernewcontextoptions)
- [browser.newPage(url, [options])](#browsernewpageurl-options)
- [browser.newPage([options])](#browsernewpageoptions)
- [browser.pages()](#browserpages)
<!-- GEN:stop -->

Expand All @@ -3821,11 +3830,11 @@ WebKit browser instance does not expose WebKit-specific features.

<!-- GEN:toc-extends-Browser -->
- [event: 'disconnected'](#event-disconnected)
- [browser.browserContexts()](#browserbrowsercontexts)
- [browser.close()](#browserclose)
- [browser.contexts()](#browsercontexts)
- [browser.isConnected()](#browserisconnected)
- [browser.newContext(options)](#browsernewcontextoptions)
- [browser.newPage(url, [options])](#browsernewpageurl-options)
- [browser.newPage([options])](#browsernewpageoptions)
- [browser.pages()](#browserpages)
<!-- GEN:stop -->

Expand Down
7 changes: 4 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import { Page } from './page';

export interface Browser extends platform.EventEmitterType {
newContext(options?: BrowserContextOptions): Promise<BrowserContext>;
browserContexts(): BrowserContext[];
contexts(): BrowserContext[];
pages(): Promise<Page[]>;
newPage(url?: string, options?: BrowserContextOptions): Promise<Page>;
newPage(options?: BrowserContextOptions): Promise<Page>;
isConnected(): boolean;
close(): Promise<void>;
_defaultContext: BrowserContext | undefined;
}

export type ConnectOptions = {
Expand All @@ -34,7 +35,7 @@ export type ConnectOptions = {

export async function collectPages(browser: Browser): Promise<Page[]> {
const result: Promise<Page[]>[] = [];
for (const browserContext of browser.browserContexts())
for (const browserContext of browser.contexts())
result.push(browserContext.pages());
const pages: Page[] = [];
for (const group of await Promise.all(result))
Expand Down
7 changes: 2 additions & 5 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ export class BrowserContext {
return this._delegate.pages();
}

async newPage(url?: string): Promise<Page> {
const page = await this._delegate.newPage();
if (url)
await page.goto(url);
return page;
async newPage(): Promise<Page> {
return this._delegate.newPage();
}

async cookies(...urls: string[]): Promise<network.NetworkCookie[]> {
Expand Down
17 changes: 9 additions & 8 deletions src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
this._client.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
this._client.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
}

_createBrowserContext(contextId: string | null, options: BrowserContextOptions): BrowserContext {
const context = new BrowserContext({
pages: async (): Promise<Page[]> => {
const targets = this._allTargets().filter(target => target.browserContext() === context && target.type() === 'page');
const targets = this._allTargets().filter(target => target.context() === context && target.type() === 'page');
const pages = await Promise.all(targets.map(target => target.page()));
return pages.filter(page => !!page) as Page[];
},

existingPages: (): Page[] => {
const pages: Page[] = [];
for (const target of this._allTargets()) {
if (target.browserContext() === context && target._crPage)
if (target.context() === context && target._crPage)
pages.push(target._crPage.page());
}
return pages;
Expand Down Expand Up @@ -159,17 +160,17 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
return context;
}

browserContexts(): BrowserContext[] {
contexts(): BrowserContext[] {
return Array.from(this._contexts.values());
}

async pages(): Promise<Page[]> {
return collectPages(this);
}

async newPage(url?: string, options?: BrowserContextOptions): Promise<Page> {
const browserContext = await this.newContext(options);
return browserContext.newPage(url);
async newPage(options?: BrowserContextOptions): Promise<Page> {
const context = await this.newContext(options);
return context.newPage();
}

async _targetCreated(event: Protocol.Target.targetCreatedPayload) {
Expand Down Expand Up @@ -240,7 +241,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {

async close() {
const disconnected = new Promise(f => this._connection.once(ConnectionEvents.Disconnected, f));
await Promise.all(this.browserContexts().map(context => context.close()));
await Promise.all(this.contexts().map(context => context.close()));
this._connection.close();
await disconnected;
}
Expand Down Expand Up @@ -294,7 +295,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {

targets(context?: BrowserContext): CRTarget[] {
const targets = this._allTargets();
return context ? targets.filter(t => t.browserContext() === context) : targets;
return context ? targets.filter(t => t.context() === context) : targets;
}

pageTarget(page: Page): CRTarget {
Expand Down
Loading

0 comments on commit 5ae15c4

Please sign in to comment.