feat: expose browser context id (#9134)

Closes #9132
This commit is contained in:
Alex Rudenko 2022-10-19 10:30:57 +02:00 committed by GitHub
parent a19b270018
commit 122778a1f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,13 @@
---
sidebar_label: BrowserContext.id
---
# BrowserContext.id property
**Signature:**
```typescript
class BrowserContext {
get id(): string | undefined;
}
```

View File

@ -37,6 +37,12 @@ await page.goto('https://example.com');
await context.close(); await context.close();
``` ```
## Properties
| Property | Modifiers | Type | Description |
| -------------------------------------- | --------------------- | ------------------- | ----------- |
| [id](./puppeteer.browsercontext.id.md) | <code>readonly</code> | string \| undefined | |
## Methods ## Methods
| Method | Modifiers | Description | | Method | Modifiers | Description |

View File

@ -178,4 +178,8 @@ export class BrowserContext extends EventEmitter {
close(): Promise<void> { close(): Promise<void> {
throw new Error('Not implemented'); throw new Error('Not implemented');
} }
get id(): string | undefined {
return undefined;
}
} }

View File

@ -596,6 +596,10 @@ export class CDPBrowserContext extends BrowserContext {
this.#id = contextId; this.#id = contextId;
} }
override get id(): string | undefined {
return this.#id;
}
/** /**
* An array of all active targets inside the browser context. * An array of all active targets inside the browser context.
*/ */

View File

@ -227,4 +227,16 @@ describe('BrowserContext', function () {
remoteBrowser.disconnect(); remoteBrowser.disconnect();
await context.close(); await context.close();
}); });
it('should provide a context id', async () => {
const {browser} = getTestState();
expect(browser.browserContexts().length).toBe(1);
expect(browser.browserContexts()[0]!.id).toBeUndefined();
const context = await browser.createIncognitoBrowserContext();
expect(browser.browserContexts().length).toBe(2);
expect(browser.browserContexts()[1]!.id).toBeDefined();
await context.close();
});
}); });