Skip to content

chore: support custom testIdAttribute #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ npx @playwright/mcp@latest --config path/to/config.json

// Remote Playwright server endpoint
remoteEndpoint?: string;

// testIdAttribute to use. Defaults to "data-testid"
testIdAttribute?: string;
},

// Server configuration
Expand Down
5 changes: 5 additions & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export type Config = {
* Remote endpoint to connect to an existing Playwright server.
*/
remoteEndpoint?: string;

/**
* The attribute to use for test IDs. Defaults to "data-testid".
*/
testIdAttribute?: string;
},

server?: {
Expand Down
2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ ${code.join('\n')}
sources: false,
});
}
if (this.config.browser?.testIdAttribute)
playwright.selectors.setTestIdAttribute(this.config.browser.testIdAttribute);
return result;
}
}
73 changes: 73 additions & 0 deletions tests/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,79 @@ await page.getByRole('button', { name: 'Submit' }).click();
`);
});

test('browser_click with testid', async ({ client, server }) => {
server.setContent('/', `
<title>Title</title>
<button data-testid="submit-btn">Submit</button>
`, 'text/html');

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

expect(await client.callTool({
name: 'browser_click',
arguments: {
element: 'Submit button',
ref: 'e2',
},
})).toHaveTextContent(`
- Ran Playwright code:
\`\`\`js
// Click Submit button
await page.getByTestId('submit-btn').click();
\`\`\`

- Page URL: ${server.PREFIX}
- Page Title: Title
- Page Snapshot
\`\`\`yaml
- button "Submit" [ref=e2]
\`\`\`
`);
});

test('browser_click with custom testid', async ({ startClient, server }) => {
const { client } = await startClient({
config: {
browser: {
testIdAttribute: 'data-custom-testid',
}
}
});
server.setContent('/', `
<title>Title</title>
<button data-custom-testid="submit-btn">Submit</button>
`, 'text/html');

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

expect(await client.callTool({
name: 'browser_click',
arguments: {
element: 'Submit button',
ref: 'e2',
},
})).toHaveTextContent(`
- Ran Playwright code:
\`\`\`js
// Click Submit button
await page.getByTestId('submit-btn').click();
\`\`\`

- Page URL: ${server.PREFIX}
- Page Title: Title
- Page Snapshot
\`\`\`yaml
- button "Submit" [ref=e2]
\`\`\`
`);
});

test('browser_select_option', async ({ client, server }) => {
server.setContent('/', `
<title>Title</title>
Expand Down