6.8 KiB
Playwright Actions Reference
Comprehensive mapping of natural language actions to Playwright code.
Navigation Actions
Navigate to Page
Natural Language:
- "navigate to X"
- "go to X"
- "visit X"
- "open X"
Playwright Code:
await page.goto('/x')
Navigate Back
Natural Language:
- "go back"
- "navigate back"
Playwright Code:
await page.goBack()
Navigate Forward
Natural Language:
- "go forward"
- "navigate forward"
Playwright Code:
await page.goForward()
Reload Page
Natural Language:
- "reload"
- "refresh page"
Playwright Code:
await page.reload()
Click Actions
Click Button
Natural Language:
- "click X button"
- "click on X"
- "press X"
Playwright Code:
await page.getByRole('button', { name: /x/i }).click()
Click Link
Natural Language:
- "click X link"
- "follow X link"
Playwright Code:
await page.getByRole('link', { name: /x/i }).click()
Double Click
Natural Language:
- "double click X"
Playwright Code:
await page.getByRole('button', { name: /x/i }).dblclick()
Right Click
Natural Language:
- "right click X"
- "context menu on X"
Playwright Code:
await page.getByRole('button', { name: /x/i }).click({ button: 'right' })
Form Input Actions
Fill Text Input
Natural Language:
- "fill X with Y"
- "enter Y in X"
- "type Y in X field"
Playwright Code:
await page.getByLabel(/x/i).fill('y')
Clear Input
Natural Language:
- "clear X"
- "empty X field"
Playwright Code:
await page.getByLabel(/x/i).clear()
Type with Delay
Natural Language:
- "slowly type X"
- "type X with delay"
Playwright Code:
await page.getByLabel(/field/i).type('x', { delay: 100 })
Select Dropdown Option
Natural Language:
- "select X from Y"
- "choose X in Y dropdown"
Playwright Code:
await page.getByLabel(/y/i).selectOption('x')
Check Checkbox
Natural Language:
- "check X"
- "enable X checkbox"
Playwright Code:
await page.getByLabel(/x/i).check()
Uncheck Checkbox
Natural Language:
- "uncheck X"
- "disable X checkbox"
Playwright Code:
await page.getByLabel(/x/i).uncheck()
Upload File
Natural Language:
- "upload X file"
- "attach X"
Playwright Code:
await page.getByLabel(/upload/i).setInputFiles('path/to/file.txt')
Keyboard Actions
Press Key
Natural Language:
- "press Enter"
- "hit Escape key"
Playwright Code:
await page.keyboard.press('Enter')
await page.keyboard.press('Escape')
Type Text
Natural Language:
- "type X"
Playwright Code:
await page.keyboard.type('x')
Key Combination
Natural Language:
- "press Ctrl+S"
- "use keyboard shortcut Cmd+K"
Playwright Code:
await page.keyboard.press('Control+S')
await page.keyboard.press('Meta+K')
Mouse Actions
Hover
Natural Language:
- "hover over X"
- "mouse over X"
Playwright Code:
await page.getByRole('button', { name: /x/i }).hover()
Drag and Drop
Natural Language:
- "drag X to Y"
- "move X to Y"
Playwright Code:
await page.getByText('X').dragTo(page.getByText('Y'))
Wait Actions
Wait for Element
Natural Language:
- "wait for X"
- "wait until X appears"
Playwright Code:
await page.getByText(/x/i).waitFor()
Wait for Navigation
Natural Language:
- "wait for page load"
- "wait for navigation"
Playwright Code:
await page.waitForLoadState('networkidle')
Wait for Timeout
Natural Language:
- "wait 2 seconds"
- "pause for 1000ms"
Playwright Code:
await page.waitForTimeout(2000)
Assertion Actions
Assert Visible
Natural Language:
- "see X"
- "verify X is visible"
- "X is displayed"
Playwright Code:
await expect(page.getByText(/x/i)).toBeVisible()
Assert Not Visible
Natural Language:
- "X is hidden"
- "don't see X"
- "X not visible"
Playwright Code:
await expect(page.getByText(/x/i)).not.toBeVisible()
Assert URL
Natural Language:
- "is redirected to X"
- "URL is X"
- "page is X"
Playwright Code:
await expect(page).toHaveURL(/x/)
Assert Text Content
Natural Language:
- "X contains Y"
- "X displays Y"
Playwright Code:
await expect(page.getByRole('x')).toContainText(/y/i)
Assert Enabled/Disabled
Natural Language:
- "X is enabled"
- "X is disabled"
Playwright Code:
await expect(page.getByRole('button', { name: /x/i })).toBeEnabled()
await expect(page.getByRole('button', { name: /x/i })).toBeDisabled()
Assert Checked
Natural Language:
- "X is checked"
- "X is unchecked"
Playwright Code:
await expect(page.getByLabel(/x/i)).toBeChecked()
await expect(page.getByLabel(/x/i)).not.toBeChecked()
Assert Count
Natural Language:
- "there are X items"
- "X items are displayed"
Playwright Code:
await expect(page.getByRole('listitem')).toHaveCount(5)
Selector Patterns
By Role
Use for semantic elements:
page.getByRole('button', { name: /submit/i })
page.getByRole('link', { name: /home/i })
page.getByRole('heading', { name: /title/i, level: 1 })
page.getByRole('textbox', { name: /search/i })
By Label
Use for form inputs:
page.getByLabel(/email/i)
page.getByLabel(/password/i)
By Text
Use for visible text:
page.getByText(/welcome/i)
page.getByText('Exact Text')
By Test ID
Use for custom test identifiers:
page.getByTestId('entity-card')
page.getByTestId('create-button')
By Placeholder
Use for input placeholders:
page.getByPlaceholder(/enter name/i)
Common Patterns
Fill Form
await page.getByLabel(/name/i).fill('John Doe')
await page.getByLabel(/email/i).fill('john@example.com')
await page.getByLabel(/type/i).selectOption('character')
await page.getByRole('button', { name: /submit/i }).click()
Navigate and Verify
await page.goto('/entities')
await expect(page).toHaveURL(/entities/)
await expect(page.getByRole('heading', { name: /entities/i })).toBeVisible()
Conditional Actions
const deleteButton = page.getByRole('button', { name: /delete/i })
if (await deleteButton.isVisible()) {
await deleteButton.click()
}
Loop Through Items
const items = page.getByRole('listitem')
const count = await items.count()
for (let i = 0; i < count; i++) {
await expect(items.nth(i)).toBeVisible()
}