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

Can Playwright tests run without a browser? Explain.

The Answer

Yes β€” when using Playwright's `request` fixture for pure API testing, no browser is launched. The `APIRequestContext` sends HTTP requests directly from Node.js without any browser overhead.

Deep Dive Explanation

API-only tests execute much faster (no browser launch overhead) and are perfect for smoke testing backend services, validating authentication flows, and setting up/tearing down test data. Running Playwright API tests alongside UI tests in the same framework ensures consistent tooling and reporting.

example.spec.ts
import { test, expect } from '@playwright/test';

// This test runs WITHOUT a browser
test('pure API test - no browser needed', async ({ request }) => {
  const response = await request.get('https://api.github.com/repos/microsoft/playwright');
  expect(response.ok()).toBeTruthy();
  const data = await response.json();
  expect(data.name).toBe('playwright');
  expect(data.stargazers_count).toBeGreaterThan(50000);
});

// Create standalone API context (outside test runner)
import { request } from '@playwright/test';
const apiContext = await request.newContext({
  baseURL: 'https://api.example.com',
  extraHTTPHeaders: { 'Authorization': 'Bearer token' },
});
const res = await apiContext.get('/users');