mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-14 21:53:35 +03:00
api: s/createIncognitoBrowserContext/newContext/
This commit is contained in:
parent
90f0b8c2b0
commit
c7cf4886ea
10
docs/api.md
10
docs/api.md
@ -32,7 +32,7 @@
|
|||||||
* [browser.browserContexts()](#browserbrowsercontexts)
|
* [browser.browserContexts()](#browserbrowsercontexts)
|
||||||
* [browser.chromium](#browserchromium)
|
* [browser.chromium](#browserchromium)
|
||||||
* [browser.close()](#browserclose)
|
* [browser.close()](#browserclose)
|
||||||
* [browser.createIncognitoBrowserContext()](#browsercreateincognitobrowsercontext)
|
* [browser.newContext()](#browsernewcontext)
|
||||||
* [browser.defaultBrowserContext()](#browserdefaultbrowsercontext)
|
* [browser.defaultBrowserContext()](#browserdefaultbrowsercontext)
|
||||||
* [browser.disconnect()](#browserdisconnect)
|
* [browser.disconnect()](#browserdisconnect)
|
||||||
* [browser.isConnected()](#browserisconnected)
|
* [browser.isConnected()](#browserisconnected)
|
||||||
@ -625,7 +625,7 @@ a single instance of [BrowserContext].
|
|||||||
|
|
||||||
Closes Chromium and all of its pages (if any were opened). The [Browser] object itself is considered to be disposed and cannot be used anymore.
|
Closes Chromium and all of its pages (if any were opened). The [Browser] object itself is considered to be disposed and cannot be used anymore.
|
||||||
|
|
||||||
#### browser.createIncognitoBrowserContext()
|
#### browser.newContext()
|
||||||
- returns: <[Promise]<[BrowserContext]>>
|
- returns: <[Promise]<[BrowserContext]>>
|
||||||
|
|
||||||
Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.
|
Creates a new incognito browser context. This won't share cookies/cache with other browser contexts.
|
||||||
@ -634,7 +634,7 @@ Creates a new incognito browser context. This won't share cookies/cache with oth
|
|||||||
(async () => {
|
(async () => {
|
||||||
const browser = await playwright.launch();
|
const browser = await playwright.launch();
|
||||||
// Create a new incognito browser context.
|
// Create a new incognito browser context.
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
// Create a new page in a pristine context.
|
// Create a new page in a pristine context.
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
// Do stuff
|
// Do stuff
|
||||||
@ -691,12 +691,12 @@ a single BrowserContext used by default. The method `browser.newPage()` creates
|
|||||||
If a page opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's browser
|
If a page opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's browser
|
||||||
context.
|
context.
|
||||||
|
|
||||||
Playwright allows creation of "incognito" browser contexts with `browser.createIncognitoBrowserContext()` method.
|
Playwright allows creation of "incognito" browser contexts with `browser.newContext()` method.
|
||||||
"Incognito" browser contexts don't write any browsing data to disk.
|
"Incognito" browser contexts don't write any browsing data to disk.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Create a new incognito browser context
|
// Create a new incognito browser context
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
// Create a new page inside context.
|
// Create a new page inside context.
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
// ... do stuff with page ...
|
// ... do stuff with page ...
|
||||||
|
@ -23,7 +23,7 @@ import * as childProcess from 'child_process';
|
|||||||
export interface BrowserInterface {
|
export interface BrowserInterface {
|
||||||
browserContexts(): BrowserContext[];
|
browserContexts(): BrowserContext[];
|
||||||
close(): Promise<void>;
|
close(): Promise<void>;
|
||||||
createIncognitoBrowserContext(): Promise<BrowserContext>;
|
newContext(): Promise<BrowserContext>;
|
||||||
defaultBrowserContext(): BrowserContext;
|
defaultBrowserContext(): BrowserContext;
|
||||||
newPage(): Promise<Page>;
|
newPage(): Promise<Page>;
|
||||||
pages(): Promise<Page[]>;
|
pages(): Promise<Page[]>;
|
||||||
|
@ -127,7 +127,7 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
return this._process;
|
return this._process;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createIncognitoBrowserContext(): Promise<BrowserContext> {
|
async newContext(): Promise<BrowserContext> {
|
||||||
const {browserContextId} = await this._client.send('Target.createBrowserContext');
|
const {browserContextId} = await this._client.send('Target.createBrowserContext');
|
||||||
const context = this._createBrowserContext(browserContextId);
|
const context = this._createBrowserContext(browserContextId);
|
||||||
this._contexts.set(browserContextId, context);
|
this._contexts.set(browserContextId, context);
|
||||||
|
@ -78,7 +78,7 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
return !this._connection._closed;
|
return !this._connection._closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createIncognitoBrowserContext(): Promise<BrowserContext> {
|
async newContext(): Promise<BrowserContext> {
|
||||||
const {browserContextId} = await this._connection.send('Target.createBrowserContext');
|
const {browserContextId} = await this._connection.send('Target.createBrowserContext');
|
||||||
const context = this._createBrowserContext(browserContextId);
|
const context = this._createBrowserContext(browserContextId);
|
||||||
this._contexts.set(browserContextId, context);
|
this._contexts.set(browserContextId, context);
|
||||||
|
@ -70,7 +70,7 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async userAgent(): Promise<string> {
|
async userAgent(): Promise<string> {
|
||||||
const context = await this.createIncognitoBrowserContext();
|
const context = await this.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
const userAgent = await page.evaluate(() => navigator.userAgent);
|
const userAgent = await page.evaluate(() => navigator.userAgent);
|
||||||
context.close();
|
context.close();
|
||||||
@ -86,7 +86,7 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
return this._process;
|
return this._process;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createIncognitoBrowserContext(): Promise<BrowserContext> {
|
async newContext(): Promise<BrowserContext> {
|
||||||
const {browserContextId} = await this._connection.send('Browser.createContext');
|
const {browserContextId} = await this._connection.send('Browser.createContext');
|
||||||
const context = this._createBrowserContext(browserContextId);
|
const context = this._createBrowserContext(browserContextId);
|
||||||
this._contexts.set(browserContextId, context);
|
this._contexts.set(browserContextId, context);
|
||||||
|
@ -34,7 +34,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
});
|
});
|
||||||
it('should create new incognito context', async function({browser, server}) {
|
it('should create new incognito context', async function({browser, server}) {
|
||||||
expect(browser.browserContexts().length).toBe(1);
|
expect(browser.browserContexts().length).toBe(1);
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
expect(context.isIncognito()).toBe(true);
|
expect(context.isIncognito()).toBe(true);
|
||||||
expect(browser.browserContexts().length).toBe(2);
|
expect(browser.browserContexts().length).toBe(2);
|
||||||
expect(browser.browserContexts().indexOf(context) !== -1).toBe(true);
|
expect(browser.browserContexts().indexOf(context) !== -1).toBe(true);
|
||||||
@ -44,7 +44,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
it('should close all belonging targets once closing context', async function({browser, server}) {
|
it('should close all belonging targets once closing context', async function({browser, server}) {
|
||||||
expect((await browser.pages()).length).toBe(1);
|
expect((await browser.pages()).length).toBe(1);
|
||||||
|
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
await context.newPage();
|
await context.newPage();
|
||||||
expect((await browser.pages()).length).toBe(2);
|
expect((await browser.pages()).length).toBe(2);
|
||||||
expect((await context.pages()).length).toBe(1);
|
expect((await context.pages()).length).toBe(1);
|
||||||
@ -53,7 +53,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
expect((await browser.pages()).length).toBe(1);
|
expect((await browser.pages()).length).toBe(1);
|
||||||
});
|
});
|
||||||
it('window.open should use parent tab context', async function({browser, server}) {
|
it('window.open should use parent tab context', async function({browser, server}) {
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
const [popupTarget] = await Promise.all([
|
const [popupTarget] = await Promise.all([
|
||||||
@ -65,8 +65,8 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
});
|
});
|
||||||
it('should isolate localStorage and cookies', async function({browser, server}) {
|
it('should isolate localStorage and cookies', async function({browser, server}) {
|
||||||
// Create two incognito contexts.
|
// Create two incognito contexts.
|
||||||
const context1 = await browser.createIncognitoBrowserContext();
|
const context1 = await browser.newContext();
|
||||||
const context2 = await browser.createIncognitoBrowserContext();
|
const context2 = await browser.newContext();
|
||||||
expect((await context1.pages()).length).toBe(0);
|
expect((await context1.pages()).length).toBe(0);
|
||||||
expect((await context2.pages()).length).toBe(0);
|
expect((await context2.pages()).length).toBe(0);
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ module.exports.addTests = function({testRunner, expect, playwright, FFOX, CHROME
|
|||||||
describe('Chromium', function() {
|
describe('Chromium', function() {
|
||||||
it('should work across sessions', async function({browser, server}) {
|
it('should work across sessions', async function({browser, server}) {
|
||||||
expect(browser.browserContexts().length).toBe(2);
|
expect(browser.browserContexts().length).toBe(2);
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
expect(browser.browserContexts().length).toBe(3);
|
expect(browser.browserContexts().length).toBe(3);
|
||||||
const remoteBrowser = await playwright.connect({
|
const remoteBrowser = await playwright.connect({
|
||||||
browserWSEndpoint: browser.chromium.wsEndpoint()
|
browserWSEndpoint: browser.chromium.wsEndpoint()
|
||||||
@ -175,7 +175,7 @@ module.exports.addTests = function({testRunner, expect, playwright, FFOX, CHROME
|
|||||||
|
|
||||||
describe('Chromium.waitForTarget', () => {
|
describe('Chromium.waitForTarget', () => {
|
||||||
it('should wait for a target', async function({browser, server}) {
|
it('should wait for a target', async function({browser, server}) {
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
let resolved = false;
|
let resolved = false;
|
||||||
const targetPromise = browser.chromium.waitForTarget(target => target.browserContext() === context && target.url() === server.EMPTY_PAGE);
|
const targetPromise = browser.chromium.waitForTarget(target => target.browserContext() === context && target.url() === server.EMPTY_PAGE);
|
||||||
targetPromise.then(() => resolved = true);
|
targetPromise.then(() => resolved = true);
|
||||||
@ -187,7 +187,7 @@ module.exports.addTests = function({testRunner, expect, playwright, FFOX, CHROME
|
|||||||
await context.close();
|
await context.close();
|
||||||
});
|
});
|
||||||
it('should timeout waiting for a non-existent target', async function({browser, server}) {
|
it('should timeout waiting for a non-existent target', async function({browser, server}) {
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
const error = await browser.chromium.waitForTarget(target => target.browserContext() === context && target.url() === server.EMPTY_PAGE, {timeout: 1}).catch(e => e);
|
const error = await browser.chromium.waitForTarget(target => target.browserContext() === context && target.url() === server.EMPTY_PAGE, {timeout: 1}).catch(e => e);
|
||||||
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
|
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
|
||||||
await context.close();
|
await context.close();
|
||||||
@ -211,7 +211,7 @@ module.exports.addTests = function({testRunner, expect, playwright, FFOX, CHROME
|
|||||||
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
|
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
|
||||||
});
|
});
|
||||||
it('should fire target events', async function({browser, server}) {
|
it('should fire target events', async function({browser, server}) {
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
const events = [];
|
const events = [];
|
||||||
browser.chromium.on('targetcreated', target => events.push('CREATED: ' + target.url()));
|
browser.chromium.on('targetcreated', target => events.push('CREATED: ' + target.url()));
|
||||||
browser.chromium.on('targetchanged', target => events.push('CHANGED: ' + target.url()));
|
browser.chromium.on('targetchanged', target => events.push('CHANGED: ' + target.url()));
|
||||||
|
@ -118,7 +118,7 @@ module.exports.addTests = function({testRunner, expect, playwright, defaultBrows
|
|||||||
});
|
});
|
||||||
it('should open devtools when "devtools: true" option is given', async({server}) => {
|
it('should open devtools when "devtools: true" option is given', async({server}) => {
|
||||||
const browser = await playwright.launch(Object.assign({devtools: true}, headfulOptions));
|
const browser = await playwright.launch(Object.assign({devtools: true}, headfulOptions));
|
||||||
const context = await browser.createIncognitoBrowserContext();
|
const context = await browser.newContext();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
context.newPage(),
|
context.newPage(),
|
||||||
browser.chromium.waitForTarget(target => target.browserContext() === context && target.url().includes('devtools://')),
|
browser.chromium.waitForTarget(target => target.browserContext() === context && target.url().includes('devtools://')),
|
||||||
|
@ -26,7 +26,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
beforeEach(async function(state) {
|
beforeEach(async function(state) {
|
||||||
state.context = await state.browser.createIncognitoBrowserContext();
|
state.context = await state.browser.newContext();
|
||||||
state.page = await state.context.newPage();
|
state.page = await state.context.newPage();
|
||||||
});
|
});
|
||||||
afterEach(async function(state) {
|
afterEach(async function(state) {
|
||||||
|
@ -155,7 +155,7 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
expect(await page.evaluate(() => document.cookie)).toEqual('password=123456');
|
expect(await page.evaluate(() => document.cookie)).toEqual('password=123456');
|
||||||
});
|
});
|
||||||
it('should isolate cookies in browser contexts', async({context, server, browser}) => {
|
it('should isolate cookies in browser contexts', async({context, server, browser}) => {
|
||||||
const anotherContext = await browser.createIncognitoBrowserContext();
|
const anotherContext = await browser.newContext();
|
||||||
await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
|
await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
|
||||||
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
|
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
|
||||||
|
|
||||||
@ -365,7 +365,7 @@ module.exports.addTests = function({testRunner, expect, FFOX, CHROME, WEBKIT}) {
|
|||||||
expect(await page.evaluate('document.cookie')).toBe('');
|
expect(await page.evaluate('document.cookie')).toBe('');
|
||||||
});
|
});
|
||||||
it('should isolate cookies when clearing', async({context, server, browser}) => {
|
it('should isolate cookies when clearing', async({context, server, browser}) => {
|
||||||
const anotherContext = await browser.createIncognitoBrowserContext();
|
const anotherContext = await browser.newContext();
|
||||||
await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
|
await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
|
||||||
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
|
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ module.exports.addTests = function({testRunner, expect, WEBKIT}) {
|
|||||||
});
|
});
|
||||||
it.skip(WEBKIT)('should isolate permissions between browser contexs', async({page, server, context, browser}) => {
|
it.skip(WEBKIT)('should isolate permissions between browser contexs', async({page, server, context, browser}) => {
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
const otherContext = await browser.createIncognitoBrowserContext();
|
const otherContext = await browser.newContext();
|
||||||
const otherPage = await otherContext.newPage();
|
const otherPage = await otherContext.newPage();
|
||||||
await otherPage.goto(server.EMPTY_PAGE);
|
await otherPage.goto(server.EMPTY_PAGE);
|
||||||
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
||||||
|
@ -28,7 +28,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
|||||||
delete state.browser;
|
delete state.browser;
|
||||||
});
|
});
|
||||||
beforeEach(async state => {
|
beforeEach(async state => {
|
||||||
state.context = await state.browser.createIncognitoBrowserContext();
|
state.context = await state.browser.newContext();
|
||||||
state.page = await state.context.newPage();
|
state.page = await state.context.newPage();
|
||||||
});
|
});
|
||||||
afterEach(async state => {
|
afterEach(async state => {
|
||||||
|
@ -126,7 +126,7 @@ module.exports.addTests = ({testRunner, product, playwrightPath}) => {
|
|||||||
|
|
||||||
describe('Page', function() {
|
describe('Page', function() {
|
||||||
beforeEach(async state => {
|
beforeEach(async state => {
|
||||||
state.context = await state.browser.createIncognitoBrowserContext();
|
state.context = await state.browser.newContext();
|
||||||
state.page = await state.context.newPage();
|
state.page = await state.context.newPage();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user