|

Day 14: Test Data Management — Factories, Faker, and Cleanup

This is Day 14 of the 21-Day Playwright with TypeScript Challenge. One lesson per day. Zero to production-ready in 3 weeks.

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


Hardcoded test data = shared state = flaky tests. Dynamic data with automatic cleanup = reliable parallel execution.

Contents

Faker.js for Realistic Data

import { faker } from '@faker-js/faker';

const testUser = {
  name: faker.person.fullName(),
  email: faker.internet.email(),
  phone: faker.phone.number(),
  address: faker.location.streetAddress(),
};

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

TestDataFactory Class

import { faker } from '@faker-js/faker';
import { APIRequestContext } from '@playwright/test';

export class TestDataFactory {
  constructor(private request: APIRequestContext) {}

  async createUser(overrides = {}) {
    const data = {
      name: faker.person.fullName(),
      email: faker.internet.email(),
      password: 'TestPass123!',
      ...overrides,
    };
    const res = await this.request.post('/api/users', { data });
    return { ...await res.json(), password: data.password };
  }

  async deleteUser(id: string) {
    await this.request.delete('/api/users/' + id);
  }
}

Fixture with Auto-Cleanup

export const test = base.extend<{ testUser: any }>({
  testUser: async ({ request }, use) => {
    const factory = new TestDataFactory(request);
    const user = await factory.createUser();
    await use(user);  // Test runs here
    await factory.deleteUser(user.id);  // Auto cleanup
  },
});

// Usage
test('profile shows name', async ({ testUser, page }) => {
  // testUser already created via API, cleaned up after test
  await page.goto('/profile/' + testUser.id);
  await expect(page.getByText(testUser.name)).toBeVisible();
});

Week 2 complete! Network mocking, auth, iframes, visual testing, parallel execution, debugging, test data. Tomorrow starts Week 3: reporters.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.