Skip to content
Open
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
52 changes: 52 additions & 0 deletions tests/form-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { test, expect } from '@playwright/test';

test.describe('Form validation', () => {

test.beforeEach(async ({ page }) => {
await page.goto('https://demo.playwright.dev/todomvc');
});

test('should show error when required field is empty', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?');
await input.click();
await input.press('Enter');
const items = page.getByTestId('todo-item');
await expect(items).toHaveCount(0);
});

test('should not accept whitespace-only input', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?');
await input.fill(' ');
await input.press('Enter');
const items = page.getByTestId('todo-item');
await expect(items).toHaveCount(0);
});

test('should accept valid input and add item', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?');
await input.fill('Buy groceries');
await input.press('Enter');
const items = page.getByTestId('todo-item');
await expect(items).toHaveCount(1);
await expect(items.first()).toContainText('Buy groceries');
});

test('should clear input field after valid submission', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?');
await input.fill('Walk the dog');
await input.press('Enter');
await expect(input).toHaveValue('');
});

test('should allow adding multiple valid items', async ({ page }) => {
const input = page.getByPlaceholder('What needs to be done?');
const todos = ['Buy milk', 'Read a book', 'Go for a run'];
for (const todo of todos) {
await input.fill(todo);
await input.press('Enter');
}
const items = page.getByTestId('todo-item');
await expect(items).toHaveCount(3);
});

});