Skip to content

feat(browser): browser.on(context)#40315

Merged
Skn0tt merged 3 commits intomicrosoft:mainfrom
Skn0tt:feat-browser-on-context
Apr 22, 2026
Merged

feat(browser): browser.on(context)#40315
Skn0tt merged 3 commits intomicrosoft:mainfrom
Skn0tt:feat-browser-on-context

Conversation

@Skn0tt
Copy link
Copy Markdown
Member

@Skn0tt Skn0tt commented Apr 20, 2026

Summary

  • Add a public browser.on('context', ...) event, fired when a new context becomes visible via browser.contexts().

@Skn0tt Skn0tt requested a review from dgozman April 20, 2026 12:55
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread docs/src/api/class-browser.md Outdated
* since: v1.60
- argument: <[BrowserContext]>

Emitted when a new browser context is created. This event is also emitted for contexts created by other clients
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mention or advertise the Browser.bind.

type BrowserSlot = {
guid: string;
contextGuid: string;
contextGuid?: string;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are never good, let's flatten browser/context early to avoid nullability.

if (!slot)
if (!slot?.context)
return;
const attached = new AttachedBrowser(this, slot);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be a browser/context pair

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Skn0tt
Copy link
Copy Markdown
Member Author

Skn0tt commented Apr 21, 2026

let's do #40333 first

@Skn0tt Skn0tt closed this Apr 21, 2026
@Skn0tt Skn0tt reopened this Apr 22, 2026
@Skn0tt Skn0tt force-pushed the feat-browser-on-context branch 2 times, most recently from 201b051 to b56a119 Compare April 22, 2026 06:34
@Skn0tt Skn0tt force-pushed the feat-browser-on-context branch from b56a119 to 1d5286c Compare April 22, 2026 06:41
Copy link
Copy Markdown
Collaborator

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect test coverage for the new public API!

private _didCreateContext(context: BrowserContext) {
context._browser = this;
this._contexts.add(context);
this.emit(Events.Browser.Context, context);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the last line of the method, so that context is fully initialized already.

Comment thread tests/mcp/cli-fixtures.ts
const userDataDirs = await fs.promises.readdir(daemonDir).catch(() => []);
for (const dir of userDataDirs.filter(f => f.startsWith('ud-')))
await fs.promises.rm(path.join(daemonDir, dir), { recursive: true, force: true }).catch(() => {});
const daemonDir = test.info().outputPath('daemon');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems entirely unrelated!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely, but it was useful utility for having copilot find the bug this fixes by itself.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

Test results for "tests 1"

9 flaky ⚠️ [chromium-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@ubuntu-22.04-chromium-tip-of-tree`
⚠️ [chromium-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@chromium-ubuntu-22.04-arm-node20`
⚠️ [chromium-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@chromium-ubuntu-22.04-node24`
⚠️ [chromium-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@chromium-ubuntu-22.04-node20`
⚠️ [chromium-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@chromium-ubuntu-22.04-node22`
⚠️ [firefox-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@firefox-ubuntu-22.04-node20`
⚠️ [firefox-library] › library/inspector/cli-codegen-1.spec.ts:1080 › cli codegen › should not throw csp directive violation errors `@firefox-ubuntu-22.04-node20`
⚠️ [firefox-page] › page/page-emulate-media.spec.ts:144 › should keep reduced motion and color emulation after reload `@firefox-ubuntu-22.04-node20`
⚠️ [webkit-library] › library/browser.spec.ts:73 › newContext should not leave a context upon failure `@webkit-ubuntu-22.04-node20`

41317 passed, 847 skipped


Merge workflow run.

@github-actions
Copy link
Copy Markdown
Contributor

Test results for "MCP"

3 failed
❌ [chrome] › mcp/autowait.spec.ts:19 › racy navigation destroys context @mcp-windows-latest
❌ [webkit] › mcp/dashboard.spec.ts:262 › should switch screencast to -s session on show --annotate @mcp-ubuntu-latest
❌ [webkit] › mcp/dashboard.spec.ts:262 › should switch screencast to -s session on show --annotate @mcp-windows-latest

1 flaky ⚠️ [chrome] › mcp/cdp.spec.ts:24 › cdp server `@mcp-windows-latest`

6561 passed, 910 skipped


Merge workflow run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants