πŸ’‘ If you like this website, please share it with your friends and network! πŸš€

Lesson 11: API Testing & Mocking

API Testing and Network Interception

1. Direct API Requests

Playwright has a built-in API request context for testing backend services directly.

typescript

import { test, expect } from '@playwright/test';

test('Create user API', async ({ request }) => {

const response = await request.post('/api/users', { data: { name: 'vishvas' } });

expect(response.ok()).toBeTruthy();

});

2. Mocking Responses

Intercept frontend network calls to mock the backend and test edge cases.

typescript

await page.route('**/api/v1/fruits', async route => {

const json = [{ name: 'Strawberry', id: 21 }];

await route.fulfill({ json });

});

    CareerRaah: The Ultimate Interview Preparation Platform