Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/playwright-core/src/client/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export class Locator implements api.Locator {
return await this._frame._channel.resolveSelector({ selector: this._selector });
}

async _resolveForCode(): Promise<string> {
const { resolvedSelector } = await this._resolveSelector();
return asLocatorDescription('javascript', resolvedSelector);
}

async getAttribute(name: string, options?: TimeoutOptions): Promise<string | null> {
return await this._frame.getAttribute(this._selector, name, { strict: true, ...options });
}
Expand Down
32 changes: 18 additions & 14 deletions packages/playwright/src/mcp/browser/tools/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ const verifyElement = defineTabTool({
},

handle: async (tab, params, response) => {
const locator = tab.page.getByRole(params.role as any, { name: params.accessibleName });
if (await locator.count() === 0) {
response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
return;
for (const frame of tab.page.frames()) {
const locator = frame.getByRole(params.role as any, { name: params.accessibleName });
if (await locator.count() > 0) {
const resolved = await locator._resolveForCode();
response.addCode(`await expect(page.${resolved}).toBeVisible();`);
Copy link
Member

Choose a reason for hiding this comment

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

should be resolved.first() in case of count > 1, otherwise it will throw

Copy link
Member Author

Choose a reason for hiding this comment

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

Throwing would be intended behavior.

response.addTextResult('Done');
return;
}
}

response.addCode(`await expect(page.getByRole(${escapeWithQuotes(params.role)}, { name: ${escapeWithQuotes(params.accessibleName)} })).toBeVisible();`);
response.addTextResult('Done');
response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
},
});

Expand All @@ -57,14 +59,16 @@ const verifyText = defineTabTool({
},

handle: async (tab, params, response) => {
const locator = tab.page.getByText(params.text).filter({ visible: true });
if (await locator.count() === 0) {
response.addError('Text not found');
return;
for (const frame of tab.page.frames()) {
const locator = frame.getByText(params.text).filter({ visible: true });
if (await locator.count() > 0) {
const resolved = await locator._resolveForCode();
response.addCode(`await expect(page.${resolved}).toBeVisible();`);
Copy link
Member

Choose a reason for hiding this comment

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

ditto

response.addTextResult('Done');
return;
}
}

response.addCode(`await expect(page.getByText(${escapeWithQuotes(params.text)})).toBeVisible();`);
response.addTextResult('Done');
response.addError('Text not found');
},
});

Expand Down
49 changes: 49 additions & 0 deletions tests/mcp/verify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,55 @@ test('browser_verify_element_visible (not found)', async ({ client, server }) =>
});
});

test('browser_verify_element_visible (iframe)', async ({ client, server }) => {
server.setContent('/', `
<title>Test Page</title>
<h1>Outer frame text</h1>
<iframe srcdoc="<html><body><h1>Inner iframe</h1></body></html>" width="400" height="200">
</iframe>
`, 'text/html');

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

expect(await client.callTool({
name: 'browser_verify_element_visible',
arguments: {
role: 'heading',
accessibleName: 'Inner iframe',
},
})).toHaveResponse({
result: 'Done',
code: `await expect(page.locator('iframe').contentFrame().getByRole('heading', { name: 'Inner iframe' })).toBeVisible();`,
});
});

test('browser_verify_text_visible (iframe)', async ({ client, server }) => {
server.setContent('/', `
<title>Test Page</title>
<h1>Outer frame text</h1>
<iframe srcdoc="<html><body><h1>Inner iframe</h1></body></html>" width="400" height="200">
</iframe>
`, 'text/html');

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

expect(await client.callTool({
name: 'browser_verify_text_visible',
arguments: {
text: 'Inner iframe',
},
})).toHaveResponse({
result: 'Done',
code: `await expect(page.locator('iframe').contentFrame().getByRole('heading', { name: 'Inner iframe' })).toBeVisible();`,
});
});

test('browser_verify_text_visible', async ({ client, server }) => {
server.setContent('/', `
<title>Test Page</title>
Expand Down
Loading