Skip to content

Commit 43c3372

Browse files
authored
feat(api): open api needed for mcp (#39646)
1 parent f026e4f commit 43c3372

38 files changed

Lines changed: 276 additions & 187 deletions

docs/src/api/class-browser.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ System.Console.WriteLine(browser.Contexts.Count); // prints "1"
158158

159159
Indicates that the browser is connected.
160160

161+
## method: Browser.launchOptions
162+
* since: v1.59
163+
* langs: js
164+
- returns: <[Object]>
165+
166+
Returns the launch options that were used to launch this browser. The return type matches the options
167+
accepted by [`method: BrowserType.launch`].
168+
161169
## async method: Browser.newBrowserCDPSession
162170
* since: v1.11
163171
- returns: <[CDPSession]>
@@ -382,6 +390,14 @@ This API controls [Chromium Tracing](https://www.chromium.org/developers/how-tos
382390

383391
Returns the buffer with trace data.
384392

393+
## method: Browser.userDataDir
394+
* since: v1.59
395+
* langs: js
396+
- returns: <[null]|[string]>
397+
398+
Returns the user data directory that the browser was launched with, or `null` if the browser was
399+
launched without a persistent context.
400+
385401
## method: Browser.version
386402
* since: v1.8
387403
- returns: <[string]>

docs/src/api/class-browsercontext.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,12 @@ Here are some permissions that may be supported by some browsers:
968968

969969
The [origin] to grant permissions to, e.g. "https://example.com".
970970

971+
## method: BrowserContext.isClosedOrClosing
972+
* since: v1.59
973+
- returns: <[boolean]>
974+
975+
Indicates that the browser context is in the process of closing or has already been closed.
976+
971977
## async method: BrowserContext.newCDPSession
972978
* since: v1.11
973979
- returns: <[CDPSession]>

docs/src/api/class-locator.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,12 @@ var banana = await page.GetByRole(AriaRole.Listitem).Last(1);
18031803
### option: Locator.locator.hasNotText = %%-locator-option-has-not-text-%%
18041804
* since: v1.33
18051805

1806+
## async method: Locator.normalize
1807+
* since: v1.59
1808+
- returns: <[Locator]>
1809+
1810+
Returns a new locator that uses best practices for referencing the matched element, prioritizing test ids,
1811+
aria roles, and other user-facing attributes over CSS selectors. This is useful for converting implementation-detail selectors into more resilient, human-readable locators.
18061812

18071813
## method: Locator.nth
18081814
* since: v1.14

docs/src/api/class-page.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,6 +4212,24 @@ Page width in pixels.
42124212

42134213
Page height in pixels.
42144214

4215+
## async method: Page.snapshotForAI
4216+
* since: v1.59
4217+
- returns: <[Object]>
4218+
- `full` <[string]> Full accessibility snapshot of the page.
4219+
- `incremental` ?<[string]> Incremental snapshot containing only changes since the last tracked snapshot, when using the [`option: Page.snapshotForAI.track`] option.
4220+
4221+
Returns an accessibility snapshot of the page optimized for AI consumption.
4222+
4223+
### option: Page.snapshotForAI.timeout = %%-input-timeout-%%
4224+
* since: v1.59
4225+
4226+
### option: Page.snapshotForAI.track
4227+
* since: v1.59
4228+
- `track` <[string]>
4229+
4230+
When specified, enables incremental snapshots. Subsequent calls with the same track name will return
4231+
an incremental snapshot containing only changes since the last call.
4232+
42154233
## async method: Page.tap
42164234
* since: v1.8
42174235
* discouraged: Use locator-based [`method: Locator.tap`] instead. Read more about [locators](../locators.md).

docs/src/api/class-tracing.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ and by ':' on other platforms).
176176

177177
Trace name to be shown in the Trace Viewer.
178178

179+
### option: Tracing.start.live
180+
* since: v1.59
181+
- `live` <[boolean]>
182+
183+
When enabled, the trace is written to an unarchived file that is updated in real time as actions occur,
184+
instead of caching changes and archiving them into a zip file at the end. This is useful for live trace
185+
viewing during test execution.
186+
179187
## async method: Tracing.startChunk
180188
* since: v1.15
181189

packages/playwright-client/types/types.d.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,6 +4533,29 @@ export interface Page {
45334533
height: number;
45344534
}): Promise<void>;
45354535

4536+
/**
4537+
* Returns an accessibility snapshot of the page optimized for AI consumption.
4538+
* @param options
4539+
*/
4540+
snapshotForAI(options?: {
4541+
/**
4542+
* When specified, enables incremental snapshots. Subsequent calls with the same track name will return an incremental
4543+
* snapshot containing only changes since the last call.
4544+
*/
4545+
track?: string;
4546+
}): Promise<{
4547+
/**
4548+
* Full accessibility snapshot of the page.
4549+
*/
4550+
full: string;
4551+
4552+
/**
4553+
* Incremental snapshot containing only changes since the last tracked snapshot, when using the
4554+
* [`track`](https://playwright.dev/docs/api/class-page#page-snapshot-for-ai-option-track) option.
4555+
*/
4556+
incremental?: string;
4557+
}>;
4558+
45364559
/**
45374560
* **NOTE** Use locator-based [locator.tap([options])](https://playwright.dev/docs/api/class-locator#locator-tap) instead. Read
45384561
* more about [locators](https://playwright.dev/docs/locators).
@@ -9096,6 +9119,11 @@ export interface BrowserContext {
90969119
origin?: string;
90979120
}): Promise<void>;
90989121

9122+
/**
9123+
* Indicates that the browser context is in the process of closing or has already been closed.
9124+
*/
9125+
isClosedOrClosing(): boolean;
9126+
90999127
/**
91009128
* **NOTE** CDP sessions are only supported on Chromium-based browsers.
91019129
*
@@ -9801,6 +9829,12 @@ export interface Browser {
98019829
*/
98029830
isConnected(): boolean;
98039831

9832+
/**
9833+
* Returns the launch options that were used to launch this browser. The return type matches the options accepted by
9834+
* [browserType.launch([options])](https://playwright.dev/docs/api/class-browsertype#browser-type-launch).
9835+
*/
9836+
launchOptions(): Object;
9837+
98049838
/**
98059839
* **NOTE** CDP Sessions are only supported on Chromium-based browsers.
98069840
*
@@ -10358,6 +10392,12 @@ export interface Browser {
1035810392
*/
1035910393
stopTracing(): Promise<Buffer>;
1036010394

10395+
/**
10396+
* Returns the user data directory that the browser was launched with, or `null` if the browser was launched without a
10397+
* persistent context.
10398+
*/
10399+
userDataDir(): null|string;
10400+
1036110401
/**
1036210402
* Returns the browser version.
1036310403
*/
@@ -14150,6 +14190,13 @@ export interface Locator {
1415014190
hasText?: string|RegExp;
1415114191
}): Locator;
1415214192

14193+
/**
14194+
* Returns a new locator that uses best practices for referencing the matched element, prioritizing test ids, aria
14195+
* roles, and other user-facing attributes over CSS selectors. This is useful for converting implementation-detail
14196+
* selectors into more resilient, human-readable locators.
14197+
*/
14198+
normalize(): Promise<Locator>;
14199+
1415314200
/**
1415414201
* Returns locator to the n-th matching element. It's zero based, `nth(0)` selects the first element.
1415514202
*
@@ -21830,6 +21877,13 @@ export interface Tracing {
2183021877
* @param options
2183121878
*/
2183221879
start(options?: {
21880+
/**
21881+
* When enabled, the trace is written to an unarchived file that is updated in real time as actions occur, instead of
21882+
* caching changes and archiving them into a zip file at the end. This is useful for live trace viewing during test
21883+
* execution.
21884+
*/
21885+
live?: boolean;
21886+
2183321887
/**
2183421888
* If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the
2183521889
* [`tracesDir`](https://playwright.dev/docs/api/class-browsertype#browser-type-launch-option-traces-dir) directory

packages/playwright-core/src/client/browser.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
3434
private _closedPromise: Promise<void>;
3535
_shouldCloseConnectionOnClose = false;
3636
_browserType!: BrowserType;
37-
_options: LaunchOptions = {};
38-
_userDataDir: string | undefined;
37+
private _options: LaunchOptions = {};
38+
private _userDataDir: string | undefined;
3939
readonly _name: string;
4040
readonly _browserName: 'chromium' | 'webkit' | 'firefox';
4141
private _path: string | undefined;
@@ -152,6 +152,14 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
152152
return this._isConnected;
153153
}
154154

155+
launchOptions(): LaunchOptions {
156+
return this._options;
157+
}
158+
159+
userDataDir(): string | null {
160+
return this._userDataDir ?? null;
161+
}
162+
155163
async newBrowserCDPSession(): Promise<api.CDPSession> {
156164
return CDPSession.from((await this._channel.newBrowserCDPSession()).session);
157165
}

packages/playwright-core/src/client/browserContext.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
7575

7676
readonly _serviceWorkers = new Set<Worker>();
7777
private _harRecorders = new Map<string, { path: string, content: 'embed' | 'attach' | 'omit' | undefined }>();
78-
_closingStatus: 'none' | 'closing' | 'closed' = 'none';
78+
private _closingStatus: 'none' | 'closing' | 'closed' = 'none';
7979
private _closeReason: string | undefined;
8080
private _harRouters: HarRouter[] = [];
8181
private _onRecorderEventSink: RecorderEventSink | undefined;
@@ -231,7 +231,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
231231
const routeHandlers = this._routes.slice();
232232
for (const routeHandler of routeHandlers) {
233233
// If the page or the context was closed we stall all requests right away.
234-
if (page?._closeWasCalled || this._closingStatus !== 'none')
234+
if (page?._closeWasCalled || this.isClosedOrClosing())
235235
return;
236236
if (!routeHandler.matches(route.request().url()))
237237
continue;
@@ -293,6 +293,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
293293
return [...this._pages];
294294
}
295295

296+
isClosedOrClosing(): boolean {
297+
return this._closingStatus !== 'none';
298+
}
299+
296300
async newPage(): Promise<Page> {
297301
if (this._ownerPage)
298302
throw new Error('Please use browser.newContext()');
@@ -511,7 +515,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
511515
}
512516

513517
async close(options: { reason?: string } = {}): Promise<void> {
514-
if (this._closingStatus !== 'none')
518+
if (this.isClosedOrClosing())
515519
return;
516520
this._closeReason = options.reason;
517521
this._closingStatus = 'closing';

packages/playwright-core/src/client/eventEmitter.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424

2525
import type { EventEmitter as EventEmitterType } from 'events';
2626
import type { Platform } from './platform';
27-
import type { Disposable } from './disposable';
28-
29-
type EventEmitterLike = {
30-
on(eventName: string | symbol, handler: (...args: any[]) => unknown): unknown;
31-
removeListener(eventName: string | symbol, handler: (...args: any[]) => unknown): unknown;
32-
};
3327

3428
type EventType = string | symbol;
3529
type Listener = (...args: any[]) => any;
@@ -402,17 +396,3 @@ function unwrapListeners(arr: Listener[]): Listener[] {
402396
function wrappedListener(l: Listener): Listener {
403397
return (l as any).listener;
404398
}
405-
406-
class EventsHelper {
407-
static addEventListener(
408-
emitter: EventEmitterLike,
409-
eventName: (string | symbol),
410-
handler: (...args: any[]) => any): Disposable {
411-
emitter.on(eventName, handler);
412-
return {
413-
dispose: async () => { emitter.removeListener(eventName, handler); }
414-
};
415-
}
416-
}
417-
418-
export const eventsHelper = EventsHelper;

packages/playwright-core/src/client/frame.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,6 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Fr
200200
return parseResult(result.value);
201201
}
202202

203-
async _evaluateFunction(functionDeclaration: string) {
204-
const result = await this._channel.evaluateExpression({ expression: functionDeclaration, isFunction: true, arg: serializeArgument(undefined) });
205-
return parseResult(result.value);
206-
}
207-
208203
async _evaluateExposeUtilityScript<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg): Promise<R> {
209204
assertMaxArguments(arguments.length, 2);
210205
const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });

0 commit comments

Comments
 (0)