πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 13 of 100
Framework
Intermediate

What are Fixtures in Playwright?

The Answer

Fixtures are Playwright's dependency injection mechanism. They provide isolated, reusable setup/teardown environments to tests. Built-in fixtures include `page`, `context`, `browser`, `request`. You can extend them with custom fixtures for your application.

Deep Dive Explanation

See Question 5 for a deep dive on custom fixtures. Key properties of fixtures: 1) Lazy β€” only created if test requests them. 2) Isolated β€” each test gets its own instance. 3) Composable β€” fixtures can depend on other fixtures. 4) Scoped β€” can be 'test' or 'worker' scoped.

example.spec.ts
// Built-in fixtures available in every test:
test('built-in fixtures', async ({ 
  page,      // Fresh Page in an isolated BrowserContext
  context,   // The BrowserContext for this test
  browser,   // The shared Browser instance
  request,   // APIRequestContext for HTTP calls
  browserName // 'chromium' | 'firefox' | 'webkit'
}) => {
  console.log(`Running on: ${browserName}`);
  await page.goto('/');
});