Playwright Database Integration Testing with Prisma and PostgreSQL
Write Playwright tests that validate database-driven UI, seed test data with Prisma, and verify end-to-end data persistence in web applications.
playwright-v1-49-matrix
Playwright Database Integration Testing with Prisma and PostgreSQL
Write Playwright tests that validate database-driven UI, seed test data with Prisma, and verify end-to-end data persistence in web applications. This guide provides step-by-step Playwright patterns to handle this scenario reliably.
Introduction
Robust automation requires handling domain-specific patterns effectively. This article covers playwright database integration testing with prisma and postgresql with production-ready Playwright code.
Architecture Overview
graph TD
GlobalSetup["Global Setup"] --> Seed["Prisma Seed"]
Seed --> Tests["Test Suite"]
Tests --> DB["PostgreSQL"]
DB --> Teardown["Global Teardown"]
Teardown --> Clean["Delete Test Data"]Implementation Guide
// global-setup.ts
import { PrismaClient } from '@prisma/client';
export default async function globalSetup() {
const prisma = new PrismaClient();
// Seed test database
await prisma.user.create({
data: { email: '[email protected]', role: 'ADMIN' }
});
await prisma.$disconnect();
}
// global-teardown.ts
export default async function globalTeardown() {
const prisma = new PrismaClient();
await prisma.user.deleteMany({ where: { email: { contains: 'test@' } } });
await prisma.$disconnect();
}Run Your Tests
npx playwright test --grep "Playwright Database Integration"
npx playwright test --uiReference Table
| Database Action | Playwright Hook | Prisma Method |
|---|---|---|
| Seed data | globalSetup | prisma.create() |
| Clean up | globalTeardown | prisma.deleteMany() |
| Per-test data | beforeEach | prisma.upsert() |
| Verify data | In test | prisma.findFirst() |
Best Practices
getByRole() and getByLabel() for resilient locators that survive UI changes.await expect(locator).toBeVisible() instead of waitForTimeout()Common Mistakes
| Anti-Pattern | Solution |
| Hardcoded wait times | Use auto-waiting assertions |
| Shared mutable test data | Create unique data per test |
| Testing 3rd party UIs directly | Mock external services |
Frequently Asked Questions
Should Playwright tests directly query the database?
Prefer testing via UI and API. Direct DB queries are acceptable for verification but avoid coupling tests to schema.
How to run database migrations before test suite?
In globalSetup, run prisma migrate deploy to ensure the schema is up to date before tests run.
How to test database-driven features?
Seed specific data states via Prisma, then verify the UI correctly displays and allows manipulation of that data.
Can Playwright tests run against a real database?
Yes, use a dedicated test database with a separate connection string to avoid affecting production data.
How to handle database connection pools in Playwright?
Create and disconnect Prisma clients in each setup/teardown hook to avoid connection leaks.
Summary
Write Playwright tests that validate database-driven UI, seed test data with Prisma, and verify end-to-end data persistence in web applications. Apply these patterns to build a reliable, maintainable automation suite for your specific use case.
Related Articles
About The Author
PlaywrightPad Editorial reports on Chromium engines, E2E test optimizations, and AI integration specifications.
Newsletter
Get weekly browser reports sent directly to your inbox.