Skip to content

Commit 0a91ecf

Browse files
committed
cherry-pick(#37694): chore: move best practices into the journal
1 parent a5c4437 commit 0a91ecf

File tree

5 files changed

+30
-41
lines changed

5 files changed

+30
-41
lines changed

examples/todomvc/.claude/agents/playwright-test-generator.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ application behavior.
2424
- Test title must match the scenario name
2525
- Includes a comment with the step text before each step execution. Do not duplicate comments if step requires
2626
multiple actions.
27+
- Always use best practices from the log when generating tests.
2728

2829
<example-generation>
2930
For following plan:
@@ -55,16 +56,4 @@ application behavior.
5556
});
5657
});
5758
```
58-
</example-generation>
59-
60-
# Best practices
61-
- Each test has clear, descriptive assertions that validate the expected behavior
62-
- Includes proper error handling and meaningful failure messages
63-
- Uses Playwright best practices (page.waitForLoadState, expect.toBeVisible, etc.)
64-
- Do not improvise, do not add directives that were not asked for
65-
- Uses reliable locators (preferring data-testid, role-based, or text-based selectors over fragile CSS selectors)
66-
- Uses local variables for locators that are used multiple times
67-
- Uses explicit waits rather than arbitrary timeouts
68-
- Never waits for networkidle or use other discouraged or deprecated apis
69-
- Is self-contained and can run independently
70-
- Is deterministic and not prone to flaky behavior
59+
</example-generation>

examples/todomvc/.github/chatmodes/🎭 generator.chatmode.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ application behavior.
2121
- Test title must match the scenario name
2222
- Includes a comment with the step text before each step execution. Do not duplicate comments if step requires
2323
multiple actions.
24+
- Always use best practices from the log when generating tests.
2425

2526
<example-generation>
2627
For following plan:
@@ -53,17 +54,5 @@ application behavior.
5354
});
5455
```
5556
</example-generation>
56-
57-
# Best practices
58-
- Each test has clear, descriptive assertions that validate the expected behavior
59-
- Includes proper error handling and meaningful failure messages
60-
- Uses Playwright best practices (page.waitForLoadState, expect.toBeVisible, etc.)
61-
- Do not improvise, do not add directives that were not asked for
62-
- Uses reliable locators (preferring data-testid, role-based, or text-based selectors over fragile CSS selectors)
63-
- Uses local variables for locators that are used multiple times
64-
- Uses explicit waits rather than arbitrary timeouts
65-
- Never waits for networkidle or use other discouraged or deprecated apis
66-
- Is self-contained and can run independently
67-
- Is deterministic and not prone to flaky behavior
6857
<example>Context: User wants to test a login flow on their web application. user: 'I need a test that logs into my app at localhost:3000 with username [email protected] and password 123456, then verifies the dashboard page loads' assistant: 'I'll use the generator agent to create and validate this login test for you' <commentary> The user needs a specific browser automation test created, which is exactly what the generator agent is designed for. </commentary></example>
6958
<example>Context: User has built a new checkout flow and wants to ensure it works correctly. user: 'Can you create a test that adds items to cart, proceeds to checkout, fills in payment details, and confirms the order?' assistant: 'I'll use the generator agent to build a comprehensive checkout flow test' <commentary> This is a complex user journey that needs to be automated and tested, perfect for the generator agent. </commentary></example>

packages/playwright/src/agents/generator.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ application behavior.
4646
- Test title must match the scenario name
4747
- Includes a comment with the step text before each step execution. Do not duplicate comments if step requires
4848
multiple actions.
49+
- Always use best practices from the log when generating tests.
4950

5051
<example-generation>
5152
For following plan:
@@ -79,18 +80,6 @@ application behavior.
7980
```
8081
</example-generation>
8182

82-
# Best practices
83-
- Each test has clear, descriptive assertions that validate the expected behavior
84-
- Includes proper error handling and meaningful failure messages
85-
- Uses Playwright best practices (page.waitForLoadState, expect.toBeVisible, etc.)
86-
- Do not improvise, do not add directives that were not asked for
87-
- Uses reliable locators (preferring data-testid, role-based, or text-based selectors over fragile CSS selectors)
88-
- Uses local variables for locators that are used multiple times
89-
- Uses explicit waits rather than arbitrary timeouts
90-
- Never waits for networkidle or use other discouraged or deprecated apis
91-
- Is self-contained and can run independently
92-
- Is deterministic and not prone to flaky behavior
93-
9483
<example>
9584
Context: User wants to test a login flow on their web application.
9685
user: 'I need a test that logs into my app at localhost:3000 with username [email protected] and password 123456, then

packages/playwright/src/mcp/test/testContext.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class GeneratorJournal {
6565
\`\`\`ts
6666
${step.code}
6767
\`\`\``).join('\n\n'));
68+
result.push(bestPracticesMarkdown);
6869
return result.join('\n\n');
6970
}
7071
}
@@ -177,3 +178,16 @@ export class TestContext {
177178
async close() {
178179
}
179180
}
181+
182+
const bestPracticesMarkdown = `
183+
# Best practices
184+
- Do not improvise, do not add directives that were not asked for
185+
- Use clear, descriptive assertions to validate the expected behavior
186+
- Use reliable locators from this log
187+
- Use local variables for locators that are used multiple times
188+
- Use Playwright waiting assertions and best practices from this log
189+
- NEVER! use page.waitForLoadState()
190+
- NEVER! use page.waitForNavigation()
191+
- NEVER! use page.waitForTimeout()
192+
- NEVER! use page.evaluate()
193+
`;

tests/mcp/generator.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test('generator_setup_page', async ({ startClient }) => {
101101
expect(await client.callTool({
102102
name: 'generator_read_log',
103103
arguments: {},
104-
})).toHaveTextResponse(`# Plan
104+
})).toHaveTextResponse(expect.stringContaining(`# Plan
105105
106106
Test plan
107107
@@ -125,7 +125,11 @@ Test plan
125125
### Click submit button
126126
\`\`\`ts
127127
await page.getByRole('button', { name: 'Submit' }).click();
128-
\`\`\``);
128+
\`\`\`
129+
130+
131+
# Best practices
132+
`));
129133
});
130134

131135
test('click after generator_log_action', async ({ startClient }) => {
@@ -164,7 +168,7 @@ test('click after generator_log_action', async ({ startClient }) => {
164168
expect(await client.callTool({
165169
name: 'generator_read_log',
166170
arguments: {},
167-
})).toHaveTextResponse(`# Plan
171+
})).toHaveTextResponse(expect.stringContaining(`# Plan
168172
169173
Test plan
170174
@@ -188,7 +192,11 @@ Test plan
188192
### Click submit button
189193
\`\`\`ts
190194
await page.getByRole('button', { name: 'Submit' }).click();
191-
\`\`\``);
195+
\`\`\`
196+
197+
198+
# Best practices
199+
`));
192200
});
193201

194202
test('generator_setup_page is required', async ({ startClient }) => {

0 commit comments

Comments
 (0)