Score 100% on the quiz to continue
Writing a happy-path test
Send a GET request and assert on the status code and body structure.
Let's start with the most important test case, the happy path: sending a GET request to the /customers endpoint and asserting that it responds successfully, with a status code of 200 and a body that has the expected structure.
Unlike web tests, API tests do not drive a browser. Instead, Playwright provides a request fixture that sends HTTP requests directly and returns the response for you to assert on.
Commands overview 📖
import { test, expect } from '@playwright/test'
Every spec file starts by importing Playwright's test function (used to declare test cases and hooks) and expect (used to write assertions) from the test runner.
Syntax:
import { test, expect } from '@playwright/test'test.describe
Groups related test cases under a shared title, which keeps the suite organized and the report easier to read.
Syntax:
test.describe(title, () => { /* tests */ })Example:
test.describe('GET /customers', () => {
// hooks and tests go here
})test
Declares a single test case. It receives a title and an async callback, and Playwright injects its fixtures (such as request) as the callback's argument.
Syntax:
test(title, async ({ request }) => { /* ... */ })Example:
test('gets customers successfully', async ({ request }) => {
// arrange, act, and assert here
})The request fixture
Playwright exposes a request fixture (an APIRequestContext) that can send HTTP requests. You receive it as an argument of your test.
Syntax:
test('...', async ({ request }) => { /* ... */ })request.get
Sends a GET request and returns the response.
Syntax:
const response = await request.get(url)Example:
// With baseURL configured, a relative path is enough
const response = await request.get('/customers')response.status and response.ok
Return the HTTP status code and whether it is in the 2xx range.
Example:
expect(response.status()).toBe(200)
expect(response.ok()).toBeTruthy()response.json
Parses the response body as JSON.
Example:
const body = await response.json()
expect(body).toHaveProperty('customers')
expect(body).toHaveProperty('pageInfo')Suggested content 📚 and documentation links 🔗
Exercise 🎯
Create a spec file (for example, tests/customers.spec.ts). Wrap your tests in a test.describe block (for example, titled GET /customers) to organize the suite. Then, inside a test, send a GET request to /customers. Assert that the response status is 200, and that the parsed body has a customers array and a pageInfo object with the properties currentPage, totalPages, and totalCustomers.
👨🏫 Remember to importtestandexpectfrom@playwright/testat the top of your spec file. Without that import, your test will not run and your assertions will not work.
Make sure the API is running locally with Docker before running your test.
Run the test withnpx playwright testto make sure your implementation works.
Show the world what you learned 🌎
To show your professional network what you learned in this lesson, post the following on LinkedIn.
I am taking the "Basics of API Testing with Playwright" course by @Walmyr Lima e Silva Filho at the @Talking About Testing school, and I just implemented my first API test, sending a GET request and asserting on the response status code and body structure using Playwright's request fixture. #TalkingAboutTesting #TATSchool #BasicsOfAPITestingWithPlaywright #Playwright #APITesting #TestAutomation
👨🏫 Remember to tag me in your post. Here is my LinkedIn profile.
Quiz
Which Playwright fixture do you use to send HTTP requests in an API test?
Score 100% on the quiz to continue