chore: reuse the first tab when navigating (#131)

This commit is contained in:
Pavel Feldman 2025-04-03 22:39:55 -07:00 committed by GitHub
parent e36d4ea695
commit fc0cccf4a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 12 deletions

View File

@ -74,11 +74,9 @@ export class Context {
}
async ensureTab(): Promise<Tab> {
if (this._currentTab)
return this._currentTab;
const context = await this._ensureBrowserContext();
await context.newPage();
if (!this._currentTab)
await context.newPage();
return this._currentTab!;
}
@ -110,9 +108,13 @@ export class Context {
}
private _onPageClosed(tab: Tab) {
this._tabs = this._tabs.filter(t => t !== tab);
const index = this._tabs.indexOf(tab);
if (index === -1)
return;
this._tabs.splice(index, 1);
if (this._currentTab === tab)
this._currentTab = this._tabs[0];
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
const browser = this._browser;
if (this._browserContext && !this._tabs.length) {
void this._browserContext.close().then(() => browser?.close()).catch(() => {});
@ -151,6 +153,8 @@ export class Context {
const context = await this._createBrowserContext();
this._browser = context.browser;
this._browserContext = context.browserContext;
for (const page of this._browserContext.pages())
this._onPageCreated(page);
this._browserContext.on('page', page => this._onPageCreated(page));
}
return this._browserContext;

View File

@ -14,6 +14,8 @@
* limitations under the License.
*/
import { chromium } from 'playwright';
import { test, expect } from './fixtures';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
@ -29,6 +31,11 @@ async function createTab(client: Client, title: string, body: string) {
test('create new tab', async ({ client }) => {
expect(await createTab(client, 'Tab one', 'Body one')).toHaveTextContent(`
Open tabs:
- 1: [] (about:blank)
- 2: (current) [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
Current tab:
- Page URL: data:text/html,<title>Tab one</title><body>Body one</body>
- Page Title: Tab one
- Page Snapshot
@ -38,8 +45,9 @@ test('create new tab', async ({ client }) => {
expect(await createTab(client, 'Tab two', 'Body two')).toHaveTextContent(`
Open tabs:
- 1: [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
- 2: (current) [Tab two] (data:text/html,<title>Tab two</title><body>Body two</body>)
- 1: [] (about:blank)
- 2: [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
- 3: (current) [Tab two] (data:text/html,<title>Tab two</title><body>Body two</body>)
Current tab:
- Page URL: data:text/html,<title>Tab two</title><body>Body two</body>
@ -56,12 +64,13 @@ test('select tab', async ({ client }) => {
expect(await client.callTool({
name: 'browser_select_tab',
arguments: {
index: 1,
index: 2,
},
})).toHaveTextContent(`
Open tabs:
- 1: (current) [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
- 2: [Tab two] (data:text/html,<title>Tab two</title><body>Body two</body>)
- 1: [] (about:blank)
- 2: (current) [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
- 3: [Tab two] (data:text/html,<title>Tab two</title><body>Body two</body>)
Current tab:
- Page URL: data:text/html,<title>Tab one</title><body>Body one</body>
@ -78,9 +87,14 @@ test('close tab', async ({ client }) => {
expect(await client.callTool({
name: 'browser_close_tab',
arguments: {
index: 2,
index: 3,
},
})).toHaveTextContent(`
Open tabs:
- 1: [] (about:blank)
- 2: (current) [Tab one] (data:text/html,<title>Tab one</title><body>Body one</body>)
Current tab:
- Page URL: data:text/html,<title>Tab one</title><body>Body one</body>
- Page Title: Tab one
- Page Snapshot
@ -88,3 +102,20 @@ test('close tab', async ({ client }) => {
- text: Body one
\`\`\``);
});
test('reuse first tab when navigating', async ({ startClient, cdpEndpoint }) => {
const browser = await chromium.connectOverCDP(cdpEndpoint);
const [context] = browser.contexts();
const pages = context.pages();
const client = await startClient({ args: [`--cdp-endpoint=${cdpEndpoint}`] });
await client.callTool({
name: 'browser_navigate',
arguments: {
url: 'data:text/html,<title>Title</title><body>Body</body>',
},
});
expect(pages.length).toBe(1);
expect(await pages[0].title()).toBe('Title');
});