Playwright es una librería de Microsoft que permite controlar navegadores reales. Un test de playwright abre una página y hace click y navega como lo haría un usuario, entonces comprueba lo que hay en la pantalla.
Cada test se ejecuta en un chromium aislado. Cuando un test falla, guarda trazas de este.
Test examples
// todo.spec.ts
import { test, expect } from '@playwright/test'
test.describe('To-do app', () => {
test.beforeEach(async ({ page }) => {
// Fake the backend so the test doesn't depend on real data
await page.route('**/api/todos', (route) =>
route.fulfill({
contentType: 'application/json',
body: JSON.stringify([{ id: 1, title: 'Buy milk', done: false }]),
}),
)
await page.goto('https://example.com/todos')
})
test('shows existing todos from the API', async ({ page }) => {
await expect(page.getByRole('listitem')).toHaveCount(1)
await expect(page.getByRole('listitem').first()).toHaveText('Buy milk')
})
test('user can add and complete a todo', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?')
await test.step('add a new todo', async () => {
await input.fill('Walk the dog')
await input.press('Enter')
await expect(page.getByRole('listitem')).toHaveCount(2)
await expect(input).toHaveValue('') // input cleared after adding
})
await test.step('mark it as done', async () => {
const item = page.getByRole('listitem').filter({ hasText: 'Walk the dog' })
await item.getByRole('checkbox').check()
await expect(item).toHaveClass(/completed/)
})
})
test('shows a validation error for empty input', async ({ page }) => {
await page.getByRole('button', { name: 'Add' }).click()
await expect(page.getByRole('alert')).toHaveText('Title is required')
})
})
Como ejecutar los tests
(en un sistema donde tengamos los tests guardados en la carpeta e2e)
# only the first time
npx playwright install chromium
# run every test - starts the server automatically
npm run test:e2e
# run a single file - selecting it or by name
npx playwright test e2e/details-format-rules.spec.ts
npx playwright test -g "Reglas de validacion"
