惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
The Playwright Playbook — Part 4: API Testing — The Underrated Superpower
Faizal · 2026-06-18 · via DEV Community

The Playwright Playbook — Part 4: API Testing — The Underrated Superpower

"The best UI test is one that doesn't need the UI at all."

In Part 1, we built the foundation. In Part 2, we intercepted the network. In Part 3, we ran multiple users simultaneously.

Now we go one layer deeper — below the browser entirely.

Playwright's API request context.

Most engineers reach for Postman when they need to test an API. Or they write a separate pytest/Jest suite just for API tests. Separate tool, separate pipeline, separate maintenance burden.

Here's what they're missing: Playwright can make raw HTTP requests without a browser. Same tool. Same TypeScript. Same test runner. Same CI pipeline.

And when you combine API calls with UI assertions in a single test — that's where the real power shows up.

No browser overhead for setup. No flaky UI login flows for data seeding. Just fast, direct, precise API calls — chained into the UI tests you're already writing.

By the end of this part, you'll have a complete API testing layer that lives right inside your Playwright project. Let's build it. 🎯


🏗️ Where We Left Off

After Part 3, our full project structure is:

playwright-playbook/
├── tests/
│   ├── auth/
│   │   └── login.spec.ts                        ✅ Part 1
│   ├── tasks/
│   │   └── task-management.spec.ts              ✅ Part 1
│   ├── network/                                 ✅ Part 2
│   │   ├── api-mocking.spec.ts
│   │   ├── error-simulation.spec.ts
│   │   └── network-assertions.spec.ts
│   ├── multi-user/                              ✅ Part 3
│   │   ├── role-permissions.spec.ts
│   │   └── realtime-collaboration.spec.ts
│   └── multi-tab/                              ✅ Part 3
│       └── multi-tab-flows.spec.ts
├── pages/
│   ├── LoginPage.ts                             ✅ Part 1
│   ├── TaskPage.ts                              ✅ Part 1
│   └── DashboardPage.ts                         ✅ Part 3
├── fixtures/
│   ├── auth.fixture.ts                          ✅ Part 1
│   ├── tasks.json                               ✅ Part 2
│   ├── empty-tasks.json                         ✅ Part 2
│   ├── tasks-har.har                            ✅ Part 2
│   └── multi-user.fixture.ts                    ✅ Part 3
├── scripts/
│   └── record-har.ts                            ✅ Part 2
├── .auth/
│   ├── admin.json
│   └── user.json
├── global-setup.ts                              ✅ Part 1
├── playwright.config.ts                         ✅ Part 1 (updated Part 3)
└── .env

By the end of Part 4, we add:

playwright-playbook/
├── tests/
│   └── api/                                     ← NEW
│       ├── tasks-api.spec.ts
│       ├── auth-api.spec.ts
│       ├── graphql-api.spec.ts
│       └── api-ui-chain.spec.ts
├── api/                                         ← NEW
│   ├── TaskApiClient.ts
│   └── AuthApiClient.ts
├── fixtures/
│   └── api.fixture.ts                           ← NEW
└── utils/
    └── schema-validator.ts                      ← NEW

Every one of these files gets fully built below. Let's go. 👇


🧠 The Mental Model — Three Ways to Use the Request Context

Before we write code, understand the three modes Playwright gives you for API testing:

Mode 1 — request fixture (isolated, no browser cookies)
  └── Pure API tests — no UI involved at all

Mode 2 — page.request (shares browser context cookies)
  └── API calls that need the same auth as the current page

Mode 3 — APIRequestContext from playwright.config.ts
  └── Shared request context across your whole suite (baseURL, headers)

We'll use all three — each for the right job. Let's build from the bottom up.


⚙️ Configuring the API Base in playwright.config.ts

First, add API-specific config so all our API clients share the same base settings:

// playwright.config.ts — updated
import { defineConfig, devices } from '@playwright/test';
import * as dotenv from 'dotenv';

dotenv.config();

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 1 : 0,
  workers: process.env.CI ? 4 : undefined,
  globalSetup: './global-setup.ts',

  reporter: [
    ['html', { open: 'never' }],
    ['list'],
  ],

  use: {
    baseURL: process.env.BASE_URL || 'http://localhost:3000',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    trace: 'on-first-retry',
    // Default headers for ALL API requests across the suite
    extraHTTPHeaders: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
  },

  projects: [
    {
      name: 'admin',
      use: {
        ...devices['Desktop Chrome'],
        storageState: '.auth/admin.json',
      },
      testMatch: ['**/auth/**', '**/tasks/**', '**/network/**'],
    },
    {
      name: 'user',
      use: {
        ...devices['Desktop Chrome'],
        storageState: '.auth/user.json',
      },
      testMatch: ['**/tasks/**'],
    },
    {
      name: 'multi-context',
      use: { ...devices['Desktop Chrome'] },
      testMatch: ['**/multi-user/**', '**/multi-tab/**'],
    },
    {
      // API tests — no browser needed, no storageState
      name: 'api',
      use: {},
      testMatch: ['**/api/**'],
    },
  ],
});

The api project has no devices — no browser spins up at all for pure API tests. Fastest possible execution. ✅


🔑 Building the Auth API Client

Our Task Manager uses JWT tokens. Before we can call protected API endpoints, we need a way to get tokens programmatically — without touching the browser.

// api/AuthApiClient.ts
import { APIRequestContext } from '@playwright/test';

export interface AuthTokens {
  accessToken: string;
  refreshToken: string;
  expiresIn: number;
}

export interface UserCredentials {
  email: string;
  password: string;
}

export class AuthApiClient {
  constructor(private readonly request: APIRequestContext) {}

  async login(credentials: UserCredentials): Promise<AuthTokens> {
    const response = await this.request.post('/api/auth/login', {
      data: credentials,
    });

    if (!response.ok()) {
      throw new Error(
        `Login failed: ${response.status()} ${await response.text()}`
      );
    }

    return response.json() as Promise<AuthTokens>;
  }

  async refreshToken(refreshToken: string): Promise<AuthTokens> {
    const response = await this.request.post('/api/auth/refresh', {
      data: { refreshToken },
    });

    if (!response.ok()) {
      throw new Error(`Token refresh failed: ${response.status()}`);
    }

    return response.json() as Promise<AuthTokens>;
  }

  async logout(accessToken: string): Promise<void> {
    await this.request.post('/api/auth/logout', {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
  }

  async getAdminToken(): Promise<string> {
    const tokens = await this.login({
      email: process.env.ADMIN_EMAIL!,
      password: process.env.ADMIN_PASSWORD!,
    });
    return tokens.accessToken;
  }

  async getUserToken(): Promise<string> {
    const tokens = await this.login({
      email: process.env.USER_EMAIL!,
      password: process.env.USER_PASSWORD!,
    });
    return tokens.accessToken;
  }
}


📋 Building the Task API Client

Now the main API client — wrapping all task-related endpoints with typed methods.

// api/TaskApiClient.ts
import { APIRequestContext, APIResponse } from '@playwright/test';

export interface Task {
  id: number;
  title: string;
  status: 'pending' | 'in_progress' | 'completed';
  assignee: string;
  createdAt: string;
  updatedAt: string;
}

export interface CreateTaskPayload {
  title: string;
  status?: Task['status'];
  assignee?: string;
}

export interface UpdateTaskPayload {
  title?: string;
  status?: Task['status'];
  assignee?: string;
}

export class TaskApiClient {
  constructor(
    private readonly request: APIRequestContext,
    private readonly token: string
  ) {}

  private get authHeaders() {
    return { Authorization: `Bearer ${this.token}` };
  }

  async getAllTasks(): Promise<Task[]> {
    const response = await this.request.get('/api/tasks', {
      headers: this.authHeaders,
    });
    return response.json();
  }

  async getTask(id: number): Promise<Task> {
    const response = await this.request.get(`/api/tasks/${id}`, {
      headers: this.authHeaders,
    });
    return response.json();
  }

  async createTask(payload: CreateTaskPayload): Promise<{ response: APIResponse; task: Task }> {
    const response = await this.request.post('/api/tasks', {
      headers: this.authHeaders,
      data: payload,
    });
    const task = await response.json();
    return { response, task };
  }

  async updateTask(id: number, payload: UpdateTaskPayload): Promise<{ response: APIResponse; task: Task }> {
    const response = await this.request.patch(`/api/tasks/${id}`, {
      headers: this.authHeaders,
      data: payload,
    });
    const task = await response.json();
    return { response, task };
  }

  async deleteTask(id: number): Promise<APIResponse> {
    return this.request.delete(`/api/tasks/${id}`, {
      headers: this.authHeaders,
    });
  }

  async getTasksByStatus(status: Task['status']): Promise<Task[]> {
    const response = await this.request.get(`/api/tasks?status=${status}`, {
      headers: this.authHeaders,
    });
    return response.json();
  }
}

Fully typed. Every method returns the raw APIResponse where needed (so tests can assert on status codes) AND the parsed body. No hunting through raw responses in test files. 🎯


🧩 Building the API Fixture

Just like Part 1's auth.fixture.ts made POM setup invisible in tests, our api.fixture.ts makes API client setup invisible.

// fixtures/api.fixture.ts
import { test as base, APIRequestContext } from '@playwright/test';
import { TaskApiClient } from '../api/TaskApiClient';
import { AuthApiClient } from '../api/AuthApiClient';

type ApiFixtures = {
  authApi: AuthApiClient;
  adminTaskApi: TaskApiClient;
  userTaskApi: TaskApiClient;
};

export const test = base.extend<ApiFixtures>({
  // Auth API client — unauthenticated, used for login/logout tests
  authApi: async ({ request }, use) => {
    await use(new AuthApiClient(request));
  },

  // Task API client — authenticated as admin
  adminTaskApi: async ({ request }, use) => {
    const authApi = new AuthApiClient(request);
    const token = await authApi.getAdminToken();
    await use(new TaskApiClient(request, token));
  },

  // Task API client — authenticated as regular user
  userTaskApi: async ({ request }, use) => {
    const authApi = new AuthApiClient(request);
    const token = await authApi.getUserToken();
    await use(new TaskApiClient(request, token));
  },
});

export { expect } from '@playwright/test';


🛡️ Building the Schema Validator

One of the most common API testing gaps: everyone checks status codes, almost nobody validates response structure. A 200 with a broken body is still a broken API.

// utils/schema-validator.ts

export interface TaskSchema {
  id: number;
  title: string;
  status: string;
  assignee: string;
  createdAt: string;
  updatedAt: string;
}

export function validateTaskSchema(task: unknown): task is TaskSchema {
  if (typeof task !== 'object' || task === null) return false;

  const t = task as Record<string, unknown>;

  return (
    typeof t.id === 'number' &&
    typeof t.title === 'string' &&
    t.title.length > 0 &&
    ['pending', 'in_progress', 'completed'].includes(t.status as string) &&
    typeof t.assignee === 'string' &&
    typeof t.createdAt === 'string' &&
    typeof t.updatedAt === 'string' &&
    !isNaN(Date.parse(t.createdAt as string)) &&
    !isNaN(Date.parse(t.updatedAt as string))
  );
}

export function validateTaskListSchema(tasks: unknown): tasks is TaskSchema[] {
  return Array.isArray(tasks) && tasks.every(validateTaskSchema);
}

// Generic field presence checker — useful for partial response validation
export function assertRequiredFields(
  obj: Record<string, unknown>,
  fields: string[]
): void {
  const missing = fields.filter(field => !(field in obj) || obj[field] === undefined);
  if (missing.length > 0) {
    throw new Error(`Missing required fields in response: ${missing.join(', ')}`);
  }
}


✅ Task API Tests — CRUD, Status Codes & Schema Validation

// tests/api/tasks-api.spec.ts
import { test, expect } from '../../fixtures/api.fixture';
import { validateTaskSchema, validateTaskListSchema } from '../../utils/schema-validator';

test.describe('Tasks API — GET', () => {
  test('GET /api/tasks returns 200 and valid task list schema', async ({ adminTaskApi }) => {
    const tasks = await adminTaskApi.getAllTasks();

    expect(Array.isArray(tasks)).toBe(true);
    expect(validateTaskListSchema(tasks)).toBe(true);
  });

  test('GET /api/tasks/:id returns correct task', async ({ adminTaskApi }) => {
    // Create a task first so we have a known ID
    const { task: created } = await adminTaskApi.createTask({
      title: 'Schema validation test task',
    });

    const fetched = await adminTaskApi.getTask(created.id);

    expect(fetched.id).toBe(created.id);
    expect(fetched.title).toBe('Schema validation test task');
    expect(validateTaskSchema(fetched)).toBe(true);

    // Cleanup
    await adminTaskApi.deleteTask(created.id);
  });

  test('GET /api/tasks/:id returns 404 for non-existent task', async ({ request }) => {
    // Use raw request here since TaskApiClient throws on non-ok responses
    const response = await request.get('/api/tasks/999999', {
      headers: { Authorization: `Bearer ${process.env.ADMIN_TOKEN}` },
    });
    expect(response.status()).toBe(404);

    const body = await response.json();
    expect(body).toHaveProperty('error');
  });

  test('GET /api/tasks filters by status correctly', async ({ adminTaskApi }) => {
    const completedTasks = await adminTaskApi.getTasksByStatus('completed');

    expect(Array.isArray(completedTasks)).toBe(true);
    completedTasks.forEach(task => {
      expect(task.status).toBe('completed');
    });
  });
});

test.describe('Tasks API — POST', () => {
  test('POST /api/tasks creates task and returns 201', async ({ adminTaskApi }) => {
    const { response, task } = await adminTaskApi.createTask({
      title: 'New API test task',
      status: 'pending',
    });

    // Status code
    expect(response.status()).toBe(201);

    // Response body
    expect(task.title).toBe('New API test task');
    expect(task.status).toBe('pending');
    expect(task.id).toBeDefined();
    expect(typeof task.id).toBe('number');

    // Full schema
    expect(validateTaskSchema(task)).toBe(true);

    // Cleanup
    await adminTaskApi.deleteTask(task.id);
  });

  test('POST /api/tasks with missing title returns 400', async ({ request }) => {
    const response = await request.post('/api/tasks', {
      headers: {
        Authorization: `Bearer ${process.env.ADMIN_TOKEN}`,
        'Content-Type': 'application/json',
      },
      data: { status: 'pending' }, // no title
    });

    expect(response.status()).toBe(400);
    const body = await response.json();
    expect(body.error).toContain('title');
  });

  test('POST /api/tasks without auth returns 401', async ({ request }) => {
    const response = await request.post('/api/tasks', {
      data: { title: 'Unauthorized task' },
      // No Authorization header
    });

    expect(response.status()).toBe(401);
  });
});

test.describe('Tasks API — PATCH', () => {
  test('PATCH /api/tasks/:id updates task correctly', async ({ adminTaskApi }) => {
    // Create
    const { task: created } = await adminTaskApi.createTask({
      title: 'Task to update',
      status: 'pending',
    });

    // Update
    const { response, task: updated } = await adminTaskApi.updateTask(created.id, {
      status: 'completed',
      title: 'Updated task title',
    });

    expect(response.status()).toBe(200);
    expect(updated.status).toBe('completed');
    expect(updated.title).toBe('Updated task title');
    expect(updated.id).toBe(created.id); // ID should never change
    expect(updated.updatedAt).not.toBe(created.updatedAt); // timestamp should change

    // Cleanup
    await adminTaskApi.deleteTask(created.id);
  });

  test('regular user cannot update another user\'s task', async ({
    adminTaskApi,
    userTaskApi,
  }) => {
    // Admin creates a task
    const { task } = await adminTaskApi.createTask({
      title: 'Admin task — user should not update',
    });

    // User tries to update it
    const { response } = await userTaskApi.updateTask(task.id, {
      title: 'Hijacked title',
    });

    expect(response.status()).toBe(403);

    // Cleanup
    await adminTaskApi.deleteTask(task.id);
  });
});

test.describe('Tasks API — DELETE', () => {
  test('DELETE /api/tasks/:id returns 200 and task no longer exists', async ({
    adminTaskApi,
  }) => {
    const { task } = await adminTaskApi.createTask({ title: 'Task to delete via API' });

    const deleteResponse = await adminTaskApi.deleteTask(task.id);
    expect(deleteResponse.status()).toBe(200);

    // Try to fetch the deleted task — should be 404
    const tasks = await adminTaskApi.getAllTasks();
    const stillExists = tasks.find(t => t.id === task.id);
    expect(stillExists).toBeUndefined();
  });

  test('regular user cannot delete admin task — returns 403', async ({
    adminTaskApi,
    userTaskApi,
  }) => {
    const { task } = await adminTaskApi.createTask({ title: 'Protected admin task' });

    const deleteResponse = await userTaskApi.deleteTask(task.id);
    expect(deleteResponse.status()).toBe(403);

    // Cleanup as admin
    await adminTaskApi.deleteTask(task.id);
  });
});


🔐 Auth API Tests — Token Flows & Security

// tests/api/auth-api.spec.ts
import { test, expect } from '../../fixtures/api.fixture';

test('successful login returns valid token structure', async ({ authApi }) => {
  const tokens = await authApi.login({
    email: process.env.USER_EMAIL!,
    password: process.env.USER_PASSWORD!,
  });

  expect(typeof tokens.accessToken).toBe('string');
  expect(tokens.accessToken.length).toBeGreaterThan(0);
  expect(typeof tokens.refreshToken).toBe('string');
  expect(typeof tokens.expiresIn).toBe('number');
  expect(tokens.expiresIn).toBeGreaterThan(0);
});

test('invalid credentials return 401', async ({ request }) => {
  const response = await request.post('/api/auth/login', {
    data: {
      email: 'notauser@test.com',
      password: 'wrongpassword',
    },
  });

  expect(response.status()).toBe(401);
  const body = await response.json();
  expect(body).toHaveProperty('error');
});

test('expired or invalid token returns 401 on protected route', async ({ request }) => {
  const response = await request.get('/api/tasks', {
    headers: {
      Authorization: 'Bearer this.is.not.a.valid.token',
    },
  });

  expect(response.status()).toBe(401);
});

test('refresh token returns new access token', async ({ authApi }) => {
  // Login to get tokens
  const initial = await authApi.login({
    email: process.env.USER_EMAIL!,
    password: process.env.USER_PASSWORD!,
  });

  // Use refresh token to get a new access token
  const refreshed = await authApi.refreshToken(initial.refreshToken);

  expect(typeof refreshed.accessToken).toBe('string');
  // New access token should be different from the original
  expect(refreshed.accessToken).not.toBe(initial.accessToken);
});

test('logout invalidates the token', async ({ authApi, request }) => {
  // Login
  const tokens = await authApi.login({
    email: process.env.USER_EMAIL!,
    password: process.env.USER_PASSWORD!,
  });

  // Logout
  await authApi.logout(tokens.accessToken);

  // Try to use the token after logout — should be rejected
  const response = await request.get('/api/tasks', {
    headers: { Authorization: `Bearer ${tokens.accessToken}` },
  });

  expect(response.status()).toBe(401);
});


🔷 GraphQL API Tests

Many modern apps expose a GraphQL API alongside (or instead of) REST. Playwright handles it just as cleanly — GraphQL is just a POST to one endpoint.

// tests/api/graphql-api.spec.ts
import { test, expect } from '../../fixtures/api.fixture';
import { validateTaskSchema } from '../../utils/schema-validator';

// Helper to fire a GraphQL query/mutation
async function gql(
  request: import('@playwright/test').APIRequestContext,
  token: string,
  query: string,
  variables?: Record<string, unknown>
) {
  const response = await request.post('/graphql', {
    headers: {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
    },
    data: { query, variables },
  });
  return response;
}

test('GraphQL query — fetch all tasks', async ({ request, authApi }) => {
  const token = await authApi.getAdminToken();

  const response = await gql(request, token, `
    query GetTasks {
      tasks {
        id
        title
        status
        assignee
        createdAt
        updatedAt
      }
    }
  `);

  expect(response.status()).toBe(200);

  const body = await response.json();
  expect(body).not.toHaveProperty('errors');
  expect(Array.isArray(body.data.tasks)).toBe(true);
  expect(validateTaskSchema(body.data.tasks[0])).toBe(true);
});

test('GraphQL mutation — create task', async ({ request, authApi }) => {
  const token = await authApi.getAdminToken();

  const response = await gql(
    request,
    token,
    `
    mutation CreateTask($title: String!, $status: TaskStatus) {
      createTask(title: $title, status: $status) {
        id
        title
        status
        createdAt
      }
    }
  `,
    { title: 'GraphQL created task', status: 'PENDING' }
  );

  expect(response.status()).toBe(200);

  const body = await response.json();
  expect(body).not.toHaveProperty('errors');
  expect(body.data.createTask.title).toBe('GraphQL created task');
  expect(body.data.createTask.id).toBeDefined();
});

test('GraphQL returns errors array on bad query — not HTTP error', async ({
  request,
  authApi,
}) => {
  const token = await authApi.getAdminToken();

  // GraphQL always returns 200 — errors live in the body
  const response = await gql(request, token, `
    query {
      nonExistentField {
        id
      }
    }
  `);

  expect(response.status()).toBe(200); // ← this is the GraphQL gotcha
  const body = await response.json();
  expect(body).toHaveProperty('errors');
  expect(Array.isArray(body.errors)).toBe(true);
});

The last test is important — it catches the GraphQL testing gotcha that trips up most engineers. GraphQL almost always returns HTTP 200, even for errors. The actual error lives in body.errors. If you're only asserting on the status code, you'll miss half your bugs. 🎯


🔗 Chaining API + UI — The Most Powerful Pattern

This is the pattern that makes your entire test suite faster and more reliable.

The idea: Use the API to set up test data (fast, no UI overhead) — then use the UI to assert on what the user actually sees. Or flip it — interact via UI, then validate what hit the database via API.

// tests/api/api-ui-chain.spec.ts
import { test, expect } from '@playwright/test';
import { TaskApiClient } from '../../api/TaskApiClient';
import { AuthApiClient } from '../../api/AuthApiClient';
import { TaskPage } from '../../pages/TaskPage';
import { validateTaskSchema } from '../../utils/schema-validator';

test('seed task via API — verify it appears in UI', async ({ page, request }) => {
  // Step 1 — Get token and create task via API (fast, no browser)
  const authApi = new AuthApiClient(request);
  const token = await authApi.getAdminToken();
  const taskApi = new TaskApiClient(request, token);

  const { task } = await taskApi.createTask({
    title: 'API-seeded task for UI verification',
    status: 'pending',
  });

  // Step 2 — Navigate to UI and verify the task is visible
  const taskPage = new TaskPage(page);
  await taskPage.goto();

  await expect(
    taskPage.getTaskLocator('API-seeded task for UI verification')
  ).toBeVisible();

  // Cleanup via API — no UI overhead for teardown either
  await taskApi.deleteTask(task.id);
});

test('create task via UI — verify it hit the API correctly', async ({
  page,
  request,
}) => {
  const taskPage = new TaskPage(page);
  await taskPage.goto();

  // Listen for the POST and capture what was sent
  let capturedTaskId: number | null = null;

  const [response] = await Promise.all([
    page.waitForResponse(
      resp =>
        resp.url().includes('/api/tasks') &&
        resp.request().method() === 'POST' &&
        resp.status() === 201
    ),
    taskPage.createTask('UI-created task — verify via API'),
  ]);

  const createdTask = await response.json();
  capturedTaskId = createdTask.id;

  // Now verify via API that the correct data was persisted
  const authApi = new AuthApiClient(request);
  const token = await authApi.getAdminToken();
  const taskApi = new TaskApiClient(request, token);

  const fetchedTask = await taskApi.getTask(capturedTaskId!);

  expect(fetchedTask.title).toBe('UI-created task — verify via API');
  expect(fetchedTask.status).toBe('pending');
  expect(validateTaskSchema(fetchedTask)).toBe(true);

  // Cleanup
  await taskApi.deleteTask(capturedTaskId!);
});

test('bulk seed via API — UI pagination works correctly', async ({
  page,
  request,
}) => {
  const authApi = new AuthApiClient(request);
  const token = await authApi.getAdminToken();
  const taskApi = new TaskApiClient(request, token);

  // Create 25 tasks via API — in parallel for speed
  const createdTasks = await Promise.all(
    Array.from({ length: 25 }, (_, i) =>
      taskApi.createTask({ title: `Pagination test task ${i + 1}` })
    )
  );

  // Navigate to UI and check pagination renders correctly
  const taskPage = new TaskPage(page);
  await taskPage.goto();

  // First page should show page size (assuming 10 per page)
  await expect(page.getByRole('listitem')).toHaveCount(10);
  await expect(page.getByTestId('pagination')).toBeVisible();
  await expect(page.getByTestId('total-count')).toContainText('25');

  // Cleanup all created tasks via API in parallel
  await Promise.all(
    createdTasks.map(({ task }) => taskApi.deleteTask(task.id))
  );
});

test('delete via UI — confirm gone via API', async ({ page, request }) => {
  // Seed task via API
  const authApi = new AuthApiClient(request);
  const token = await authApi.getAdminToken();
  const taskApi = new TaskApiClient(request, token);

  const { task } = await taskApi.createTask({
    title: 'Task to delete from UI',
  });

  // Delete via UI
  const taskPage = new TaskPage(page);
  await taskPage.goto();
  await taskPage.deleteTask('Task to delete from UI');

  // Confirm UI updated
  await expect(taskPage.getTaskLocator('Task to delete from UI')).not.toBeVisible();

  // Confirm API also reflects deletion
  const allTasks = await taskApi.getAllTasks();
  const stillExists = allTasks.find(t => t.id === task.id);
  expect(stillExists).toBeUndefined();
});

This is the pattern that catches the bugs your pure UI tests miss — when the UI says something is deleted but the database still has it. Or when the UI shows a task but the API returns it with wrong data. Catching the gap between what the user sees and what actually happened is where this shines. 🔥


📁 Final Project Structure After Part 4

Every file listed below has been fully built across Parts 1 through 4:

playwright-playbook/
├── tests/
│   ├── auth/
│   │   └── login.spec.ts                        ✅ Part 1
│   ├── tasks/
│   │   └── task-management.spec.ts              ✅ Part 1
│   ├── network/                                 ✅ Part 2
│   │   ├── api-mocking.spec.ts
│   │   ├── error-simulation.spec.ts
│   │   └── network-assertions.spec.ts
│   ├── multi-user/                              ✅ Part 3
│   │   ├── role-permissions.spec.ts
│   │   └── realtime-collaboration.spec.ts
│   ├── multi-tab/                               ✅ Part 3
│   │   └── multi-tab-flows.spec.ts
│   └── api/                                     ✅ Part 4
│       ├── tasks-api.spec.ts
│       ├── auth-api.spec.ts
│       ├── graphql-api.spec.ts
│       └── api-ui-chain.spec.ts
├── pages/
│   ├── LoginPage.ts                             ✅ Part 1
│   ├── TaskPage.ts                              ✅ Part 1
│   └── DashboardPage.ts                         ✅ Part 3
├── api/                                         ✅ Part 4
│   ├── TaskApiClient.ts
│   └── AuthApiClient.ts
├── fixtures/
│   ├── auth.fixture.ts                          ✅ Part 1
│   ├── tasks.json                               ✅ Part 2
│   ├── empty-tasks.json                         ✅ Part 2
│   ├── tasks-har.har                            ✅ Part 2
│   ├── multi-user.fixture.ts                    ✅ Part 3
│   └── api.fixture.ts                           ✅ Part 4
├── scripts/
│   └── record-har.ts                            ✅ Part 2
├── utils/                                       ✅ Part 4
│   └── schema-validator.ts
├── .auth/                                       ← git-ignored
│   ├── admin.json
│   └── user.json
├── global-setup.ts                              ✅ Part 1
├── playwright.config.ts                         ✅ Part 1 (updated Parts 3 & 4)
├── .env                                         ← git-ignored
└── package.json


🗺️ What's Coming in This Series

Part 1 — Stop Writing Tests Like a Beginner              ✅ Done
Part 2 — Network Interception: The Complete Guide        ✅ Done
Part 3 — Multi-User, Multi-Tab & Context Testing         ✅ Done
Part 4 — API Testing (The Underrated Superpower)         ← You are here
Part 5 — Visual Regression Testing
Part 6 — Debugging Like a Pro: Trace Viewer & Inspector
Part 7 — The CI/CD Setup Nobody Shows You
Part 8 — Playwright Meets AI: Agents, MCP & Self-Healing Tests

In Part 5, we add visual regression testing — toHaveScreenshot(), masking dynamic content, pixel tolerance config, and running VRT in CI without flakiness. The kind of bugs that slip past every assertion you've written so far.


🔖 Before You Go

After four parts, you've built something real:

  • A POM-based UI test layer with proper auth
  • A network interception layer that owns the API at the browser level
  • A multi-user layer that tests collaboration and permissions simultaneously
  • A raw API testing layer with typed clients, schema validation, and API-UI chaining

Each layer is independent. Each layer builds on the last.

And you've done it all in one tool, one language, one pipeline. No Postman. No separate pytest suite. No context switching. 💪


Follow me so you don't miss Part 5 — where we catch the bugs that slip past every assertion: CSS regressions, layout shifts, and design inconsistencies — with Playwright's visual regression testing.

Drop a comment below 👇

  • Are you currently running API tests in a separate tool from your UI tests?
  • Did the GraphQL gotcha (200 status with errors in body) catch you off guard?
  • What's the first API endpoint you'd write a schema validation test for?

Let's talk in the comments. 🙌


Faizal Shaikh | Senior Automation Engineer | Playwright & AI Testing
Connect with me on LinkedIn