File tree Expand file tree Collapse file tree 5 files changed +35
-4
lines changed
Expand file tree Collapse file tree 5 files changed +35
-4
lines changed Original file line number Diff line number Diff line change 170170** Parameters:**
171171
172172- ** url** (string) ** (required)** : URL to load in a new page.
173+ - ** background** (boolean) _ (optional)_ : Whether to open the page in the background without bringing it to the front. Default is false (foreground).
173174- ** timeout** (integer) _ (optional)_ : Maximum wait time in milliseconds. If set to 0, the default timeout will be used.
174175
175176---
Original file line number Diff line number Diff line change @@ -265,8 +265,8 @@ export class McpContext implements Context {
265265 return this . #consoleCollector. getById ( this . getSelectedPage ( ) , id ) ;
266266 }
267267
268- async newPage ( ) : Promise < Page > {
269- const page = await this . browser . newPage ( ) ;
268+ async newPage ( background ?: boolean ) : Promise < Page > {
269+ const page = await this . browser . newPage ( { background } ) ;
270270 await this . createPagesSnapshot ( ) ;
271271 this . selectPage ( page ) ;
272272 this . #networkCollector. addPage ( page ) ;
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ export type Context = Readonly<{
110110 getPageById ( pageId : number ) : Page ;
111111 getPageId ( page : Page ) : number | undefined ;
112112 isPageSelected ( page : Page ) : boolean ;
113- newPage ( ) : Promise < Page > ;
113+ newPage ( background ?: boolean ) : Promise < Page > ;
114114 closePage ( pageId : number ) : Promise < void > ;
115115 selectPage ( page : Page ) : void ;
116116 getElementByUid ( uid : string ) : Promise < ElementHandle < Element > > ;
Original file line number Diff line number Diff line change @@ -87,10 +87,16 @@ export const newPage = defineTool({
8787 } ,
8888 schema : {
8989 url : zod . string ( ) . describe ( 'URL to load in a new page.' ) ,
90+ background : zod
91+ . boolean ( )
92+ . optional ( )
93+ . describe (
94+ 'Whether to open the page in the background without bringing it to the front. Default is false (foreground).' ,
95+ ) ,
9096 ...timeoutSchema ,
9197 } ,
9298 handler : async ( request , response , context ) => {
93- const page = await context . newPage ( ) ;
99+ const page = await context . newPage ( request . params . background ) ;
94100
95101 await context . waitForEventsAfterAction ( async ( ) => {
96102 await page . goto ( request . params . url , {
Original file line number Diff line number Diff line change @@ -43,6 +43,30 @@ describe('pages', () => {
4343 assert . ok ( response . includePages ) ;
4444 } ) ;
4545 } ) ;
46+ it ( 'create a page in the background' , async ( ) => {
47+ await withMcpContext ( async ( response , context ) => {
48+ const originalPage = context . getPageById ( 1 ) ;
49+ assert . strictEqual ( originalPage , context . getSelectedPage ( ) ) ;
50+ // Ensure original page has focus
51+ await originalPage . bringToFront ( ) ;
52+ assert . strictEqual (
53+ await originalPage . evaluate ( ( ) => document . hasFocus ( ) ) ,
54+ true ,
55+ ) ;
56+ await newPage . handler (
57+ { params : { url : 'about:blank' , background : true } } ,
58+ response ,
59+ context ,
60+ ) ;
61+ // New page should be selected but original should retain focus
62+ assert . strictEqual ( context . getPageById ( 2 ) , context . getSelectedPage ( ) ) ;
63+ assert . strictEqual (
64+ await originalPage . evaluate ( ( ) => document . hasFocus ( ) ) ,
65+ true ,
66+ ) ;
67+ assert . ok ( response . includePages ) ;
68+ } ) ;
69+ } ) ;
4670 } ) ;
4771 describe ( 'close_page' , ( ) => {
4872 it ( 'closes a page' , async ( ) => {
You can’t perform that action at this time.
0 commit comments