Given the following DOM structure: [DOM HTML Structure]
Generate Playwright test code in TypeScript to perform [specific action].
Here is the page URL: [URL]
Requirements:
- IMPORTANT - Use only modern Playwright Locator API:
- Always use page.locator() or the recommended getBy* methods
- For multiple elements, use .all() method instead of deprecated $$ syntax
- Never use deprecated page.$() or page.$$() syntax
- Example for multiple elements:
- Use: await page.locator('.item').all()
- Or: await page.getByRole('listitem').all()
- Don't use: await page.$$('.item')
- Use only recommended Playwright locators in this priority order:
- Always use the html ids or names as css locators if id and name are present
- Role-based locators (getByRole) ONLY when the element has a unique role in its context
- Bad: page.getByRole('list') when there are multiple lists
- Good: page.getByRole('list', { name: 'Todo Items' }) or use a more specific selector
- Label-based locators (getByLabel)
- Text-based locators (getByText)
- Test ID-based locators (getByTestId)
- CSS selectors when you need to be more specific (e.g., '.todo-list' for a specific list)
- Only use other locators if above options are not applicable
- IMPORTANT - Handling ambiguous elements:
- When dealing with elements that might appear multiple times (like lists, buttons, inputs):
- Always check if the role/text/label alone is unique enough
- If not, use more specific selectors or add additional filters:
- Use name option: page.getByRole('button', { name: 'Submit' })
- Use exact option: page.getByText('Submit', { exact: true })
- Use parent containers: page.locator('.todo-section').getByRole('list')
- Use class/id selectors: page.locator('.todo-list')
- Avoid ambiguous locators that might match multiple elements
- Implementation guidelines:
- Write code using TypeScript with proper type annotations
- Include appropriate web-first assertions to validate the action
- Use Playwright's built-in configurations and devices when applicable
- Store frequently used locators in variables for reuse
- Avoid hard-coded wait times - use auto-waiting mechanisms
- Include error handling where appropriate
- Add comments explaining complex logic or non-obvious choices
- For most of the input values try to use js faker library to generate real world like values
- If the test scenario involves navigation to a new page, then do not put any assertions for this new page by assuming the title, page url and locators of the next page which are not shared with you.
- Code structure:
- Start with necessary imports
- Include test description that clearly states the action being performed
- Break down complex actions into smaller, descriptive test steps
- Use meaningful variable names that reflect their purpose
- Follow Playwright's best practices for test organization
- Performance and reliability:
- Implement proper waiting strategies using built-in auto-waiting
- Use proper assertion timeouts instead of arbitrary waits
- Include retry logic for flaky operations if necessary
- Consider network conditions and page load states
Respond with only the complete code block and no other text.
Example format:
import { test, expect } from '@playwright/test';
test('descriptive test name', async ({ page }) => {
// Implementation here
});
This detailed prompt provides comprehensive guidelines for writing high-quality, maintainable UI automation tests using Playwright, covering locator strategies, best practices, code structure, and reliability considerations.
Given the following DOM structure: [DOM]
Generate Cypress test code in TypeScript to perform the following action:
Please give me the complete INPUT PROMPT without any manipulation
Here is the page URL: [get the page url]
Requirements:
Use recommended Cypress selectors in this priority order:
data-cy or data-test attributes
ID or unique attributes
Class or tag combinations
Text content
Complex CSS selectors as last resort
Implementation guidelines:
Always handle Cypress commands as asynchronous operations
Use .then() for handling async operations and assertions
Avoid using synchronous loops with async operations
Use cy.wrap() when working with jQuery objects or promises
Chain Cypress commands instead of using variables for elements
Remember that Cypress commands are not promises - they are enqueued and run serially
Use proper async patterns:
.then() for sequential operations
cy.wrap() for non-Cypress async operations
Avoid mixing sync and async code
Use closure variables within .then() blocks
Chain assertions using .should() instead of expect
Write code using TypeScript with proper type annotations
Include appropriate assertions to validate actions
Store frequently used selectors in variables
Avoid hard-coded wait times - use built-in retry mechanisms
Include error handling where appropriate
Add comments explaining complex logic
Use faker for test data generation
Follow Cypress best practices for async operations
Code structure:
Start with necessary imports
Include test description that clearly states the action
Break down complex actions into smaller steps
Use meaningful variable names
Follow Cypress async/await patterns
Use custom commands for reusable async operations
Handle jQuery elements within .then() blocks
Chain commands for better readability
Performance and reliability:
Use Cypress's automatic retry-ability
Leverage built-in assertions that retry
Use .should() with callbacks for complex assertions
Handle timeouts appropriately
Use proper command chaining
Include retry logic for flaky operations
Consider network conditions
Use cy.intercept() for network requests
Async operation patterns to follow:
Use .then() for sequential operations:
cy.get().then($el => {
// Work with $el here
})
Chain assertions:
cy.get().should('have.text', 'expected')
Custom commands for reusable async operations:
Cypress.Commands.add('customCommand', (param1, param2) => {
return cy.get().then($el => {
// Async operation
})
})
Handle multiple elements:
cy.get().each(($el, index) => {
cy.wrap($el).then(() => {
// Async operation per element
})
})
Respond with only the complete code block and no other text.
Example format: