Playwright Tutorial 0/154 lessons ~6 min read Lesson 77

    APIRequestContext

    APIRequestContext is a practical Playwright skill for testers who want faster, less brittle suites by combining API and UI checks.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    APIRequestContext is a practical Playwright skill for testers who want faster, less brittle suites by combining API and UI checks. Instead of memorizing syntax, learn the production reason behind it: what risk it reduces, what evidence it gives, and how it changes the way a team ships software.

    Purpose of this lesson

    Story: A login workflow test dropped from 70 seconds to 9 seconds when setup moved from UI clicks to API calls. In this lesson, aPIRequestContext lets tests call APIs without leaving Playwright. That is the difference between a test that merely runs and a test that helps a team decide.

    Understanding the topic

    Why this exists: APIRequestContext lets tests call APIs without leaving Playwright. API testing in Playwright is not a separate world. It prepares data, validates contracts, and shortens UI flows.

    • Real problem solved: APIRequestContext reduces ambiguity when browser behavior, data, timing, or infrastructure changes.
    • Production use: test data setup, cleanup, contract checks, auth flows, chained workflows, and backend validation.
    • Beginner misuse: Using UI for every setup step, making tests slow and fragile.
    • Elite SDET move: Use APIs to create state, then use UI to prove the user-facing journey.

    Visual explanation

    Mental model:

    text
    API setup
    UI action
    API verification
    Cleanup

    Informative example

    A practical example for APIRequestContext:

    ts
    const created = await request.post("/api/orders", {
    data: { sku: "apirequestcontext", quantity: 1 },
    });
    expect(created.ok()).toBeTruthy();
    await page.goto(`/orders/${(await created.json()).id}`);
    await expect(page.getByText("apirequestcontext")).toBeVisible();

    Execution workflow

    1APIRequestContext production workflow
    1 / 4

    Intent

    Start from user behavior, business risk, and the signal this test must protect.

    Real-world use

    In real teams, APIRequestContext matters because product code changes every week. The test must still tell a useful story: what user behavior was protected, what state was expected, what evidence was captured, and whether the failure belongs to the app, the test, the data, or the environment.

    Best practices

    • Create expensive state through API when UI setup is not the thing under test.
    • Validate status, schema, and business fields.
    • Keep API helpers typed.
    • Clean up data through API when possible.

    Common mistakes

    • Turning an end-to-end test into an API-only test accidentally.
    • Ignoring contract validation and checking only status 200.
    • Leaving test data behind after failures.

    Debugging tips

    • Attach request and response payloads on API failure.
    • Check auth headers and environment baseURL first.
    • If UI and API disagree, verify caching and eventual consistency.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1QuestionWhy is APIRequestContext important in Playwright?+

    Answer

    It addresses apirequestcontext lets tests call apis without leaving playwright. and gives teams a clearer signal about user behavior, not just command execution.
    2QuestionWhat mistake do beginners make with APIRequestContext?+

    Answer

    Using UI for every setup step, making tests slow and fragile.
    3QuestionHow do senior SDETs use APIRequestContext?+

    Answer

    Use APIs to create state, then use UI to prove the user-facing journey. They also make the failure observable with trace, report, data, and environment context.

    Summary

    APIRequestContext is valuable when it makes browser automation faster, clearer, and easier to debug under real product change.

    Ready to mark this lesson complete?Track your journey across the entire course.