API Automation with Playwright
This page shows how Playwright Test calls an API with no browser page in between: how the request fixture sends requests, how a test checks the status and the JSON body, how API calls set up test data for UI tests, and how one login can serve a whole suite.
7 min read7 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 9 sources9টি সোর্স · updated Nov 2026আপডেট: Nov 2026
One request fixture, one responseএকটা request fixture, একটা response
See a Playwright API test send a request and read status, ok and json. Then pick a status code and turn failOnStatusCode on or off.দেখুন একটা Playwright API টেস্ট কীভাবে request পাঠায় আর status, ok ও json পড়ে। তারপর একটা status code বেছে failOnStatusCode চালু বা বন্ধ করুন।
You’ll learnআপনি শিখবেন
- What the request fixture doesrequest fixture কী করে✓
- How baseURL keeps requests shortbaseURL কীভাবে request ছোট রাখে
- What status(), ok() and json() givestatus(), ok() ও json() কী দেয়
- Why a 404 does not throw by default404 কেন ডিফল্টে error ছোড়ে না
What it is
Playwright can access the REST API of your application Source 1সোর্স 1. A test sends requests to the server directly from Node.js, without loading a page Source 1সোর্স 1. The Playwright docs name three uses Source 1সোর্স 1:
- test your server API
- prepare server-side state before a test visits the web app
- check server-side results after actions in the browser
The class behind it is APIRequestContext. It can trigger API endpoints, configure microservices, and prepare the environment of an end-to-end test Source 2সোর্স 2. Playwright Test comes with a built-in request fixture, ready to send requests Source 1সোর্স 1, and test fixtures are isolated between tests Source 6সোর্স 6.
New to HTTP methods and status codes? Start with API Testing with Postman.
সহজ English
Playwright can talk to your app's REST API Source 1সোর্স 1. A test sends requests straight to the server, with no web page open Source 1সোর্স 1. You can use this in three ways Source 1সোর্স 1:
- test the API itself
- set up data on the server before a UI test
- check the server after a UI test clicks around
The tool for this is APIRequestContext Source 2সোর্স 2. Playwright Test has a built-in request fixture for it Source 1সোর্স 1, and the fixtures of one test are kept apart from other tests Source 6সোর্স 6.
New to HTTP? Start with API Testing with Postman.
বাংলা ব্যাখ্যা
Playwright দিয়ে আপনার app-এর REST API সরাসরি ব্যবহার করা যায় Source 1সোর্স 1। টেস্ট কোনো পেজ না খুলেই Node.js থেকে সরাসরি server-এ request পাঠায় Source 1সোর্স 1। Playwright-এর docs এর তিনটা ব্যবহার বলে Source 1সোর্স 1:
- server-এর API টেস্ট করা
- UI টেস্ট web app-এ ঢোকার আগে server-এ দরকারি অবস্থা তৈরি করা
- browser-এ কাজের পর server-এর ফলাফল যাচাই করা
এর পেছনের class হলো APIRequestContext। এটা API endpoint চালাতে, microservice কনফিগার করতে আর end-to-end টেস্টের পরিবেশ তৈরি করতে পারে Source 2সোর্স 2। Playwright Test-এ একটা built-in request fixture আছে, যেটা request পাঠাতে তৈরি Source 1সোর্স 1, আর test fixture এক টেস্ট থেকে আরেক টেস্টে আলাদা থাকে Source 6সোর্স 6।
HTTP method আর status code নতুন হলে আগে API Testing with Postman পড়ুন।
How it works
- The fixture. A test names the fixtures that it needs in its arguments, as
{ page }does for the page fixture Source 6সোর্স 6; the API tests take{ request }Source 1সোর্স 1. The fixture respects the config optionsbaseURLandextraHTTPHeadersSource 1সোর্স 1, andextraHTTPHeadersholds additional HTTP headers to be sent with every request Source 7সোর্স 7. - The request.
request.get(),request.post(), and the other methods take options.paramsadds query parameters to the URL Source 2সোর্স 2.datasets the body: an object is serialized to JSON, and thecontent-typeheader becomesapplication/jsonunless you set it Source 2সোর্স 2. - The response.
status()is the status code Source 3সোর্স 3.ok()istruewhen the status is in the range 200–299 Source 3সোর্স 3.json()returns the body as JSON, and throws if the body is not valid JSON;text()returns it as text Source 3সোর্স 3. - The assertion.
await expect(response).toBeOK()checks that the status is in the range 200..299 Source 4সোর্স 4. - An error status is not an exception. By default, the call returns a response for every status code. With the
failOnStatusCodeoption, it throws on codes other than 2xx and 3xx Source 2সোর্স 2.
test('…', async ({ request }) => …)
│
▼
request.post('/post', { data: { title: 'Buy milk' } }) body as JSON
│
▼
APIResponse ── status() 200
├── ok() true for 200–299
└── json() the body as an object
│
▼
await expect(response).toBeOK()সহজ English
- The fixture. A test names what it needs in its arguments, like
{ page }for a page Source 6সোর্স 6; an API test takes{ request }Source 1সোর্স 1. It uses thebaseURLandextraHTTPHeadersfrom your config Source 1সোর্স 1;extraHTTPHeadersare extra headers sent with every request Source 7সোর্স 7. - The request. Methods like
request.get()andrequest.post()take options.paramsadds query parameters to the URL.datais the body; an object is sent as JSON Source 2সোর্স 2. - The response.
status()gives the status code.ok()istruefor 200 to 299.json()reads the body as JSON and fails if it is not JSON.text()reads it as text Source 3সোর্স 3. - The check.
await expect(response).toBeOK()checks for a status from 200 to 299 Source 4সোর্স 4. - An error status is not an exception. By default, you get a response back for every status code. The
failOnStatusCodeoption makes the call throw on codes other than 2xx and 3xx Source 2সোর্স 2.
বাংলা ব্যাখ্যা
- Fixture। টেস্টের argument-এ যে fixture-এর নাম থাকে, টেস্ট সেটাই পায়, যেমন page fixture-এর জন্য
{ page }Source 6সোর্স 6; API টেস্ট নেয়{ request }Source 1সোর্স 1। এটা config-এরbaseURLআরextraHTTPHeadersমেনে চলে Source 1সোর্স 1;extraHTTPHeaders-এ থাকে বাড়তি HTTP header, যেগুলো প্রতিটা request-এর সাথে যায় Source 7সোর্স 7। - Request।
request.get(),request.post()আর অন্য method-গুলো option নেয়।paramsURL-এ query parameter যোগ করে।dataহলো body: object দিলে সেটা JSON হয়ে যায়, আর আপনি না দিলেcontent-typeheader হয়application/jsonSource 2সোর্স 2। - Response।
status()হলো status code। Status 200–299-এর মধ্যে হলেok()হয়true।json()body-টা JSON হিসেবে দেয়, আর body ঠিক JSON না হলে error দেয়;text()দেয় text হিসেবে Source 3সোর্স 3। - Assertion।
await expect(response).toBeOK()দেখে status 200..299-এর মধ্যে কি না Source 4সোর্স 4। - Error status মানেই exception নয়। ডিফল্টভাবে যেকোনো status code-এ response ফেরত আসে।
failOnStatusCodeoption দিলে 2xx আর 3xx ছাড়া অন্য code-এ call নিজেই error দেয় Source 2সোর্স 2।
Try it
Use a Playwright project; UI Automation with Playwright shows how to create one. The test calls the Postman Echo service Source 8সোর্স 8. It returns a JSON response with the details of the request that you sent Source 8সোর্স 8, and the /get and /post endpoints used here need no authentication Source 8সোর্স 8.
- Set the base URL.
test.use()in the test file setsbaseURLfor its tests, to simplify them Source 1সোর্স 1: each request below gives only the path, such as/post. - Write two tests. Save this as
tests/echo.spec.ts:
import { test, expect } from '@playwright/test';
test.use({ baseURL: 'https://postman-echo.com' });
test('GET sends the query parameters', async ({ request }) => {
const response = await request.get('/get', { params: { course: 'sdet', step: 17 } });
await expect(response).toBeOK();
const body = await response.json();
expect(body.args).toEqual({ course: 'sdet', step: '17' });
});
test('POST sends a JSON body', async ({ request }) => {
const response = await request.post('/post', { data: { title: 'Buy milk', done: false } });
expect(response.status()).toBe(200);
const body = await response.json();
expect(body.json).toEqual({ title: 'Buy milk', done: false });
});- Run them.
npx playwright test tests/echo.spec.ts. The tests send their requests without loading a page Source 1সোর্স 1. - Break one on purpose. Change
'Buy milk'in the last line to'Buy bread'and run again. Read the failure: compare what the test expected with what it received.
সহজ English
Use a Playwright project; UI Automation with Playwright shows how to make one. The test calls Postman Echo. It sends back, as JSON, what your request contained Source 8সোর্স 8. These two endpoints need no login Source 8সোর্স 8.
- Set the base URL.
test.use()setsbaseURLfor the tests in the file, to make them simpler Source 1সোর্স 1: each request below gives only the path, such as/post. - Write two tests. Save this as
tests/echo.spec.ts:
import { test, expect } from '@playwright/test';
test.use({ baseURL: 'https://postman-echo.com' });
test('GET sends the query parameters', async ({ request }) => {
const response = await request.get('/get', { params: { course: 'sdet', step: 17 } });
await expect(response).toBeOK();
const body = await response.json();
expect(body.args).toEqual({ course: 'sdet', step: '17' });
});
test('POST sends a JSON body', async ({ request }) => {
const response = await request.post('/post', { data: { title: 'Buy milk', done: false } });
expect(response.status()).toBe(200);
const body = await response.json();
expect(body.json).toEqual({ title: 'Buy milk', done: false });
});- Run them.
npx playwright test tests/echo.spec.ts. The tests send their requests without loading a page Source 1সোর্স 1. - Make one fail. Change
'Buy milk'in the last line to'Buy bread'and run again. Read the error: compare what the test expected with what it got.
বাংলা ব্যাখ্যা
একটা Playwright project নিন; কীভাবে বানাবেন তা UI Automation with Playwright-এ আছে। টেস্টটা Postman Echo service-কে call করে Source 8সোর্স 8। আপনি যে request পাঠান, তার খুঁটিনাটি এটা JSON হিসেবে ফেরত দেয় Source 8সোর্স 8, আর এখানে ব্যবহৃত /get ও /post endpoint-এর জন্য কোনো authentication লাগে না Source 8সোর্স 8।
- Base URL ঠিক করুন। টেস্ট সহজ করতে test file-এ
test.use()দিয়ে সেই file-এর টেস্টগুলোরbaseURLঠিক করা হয় Source 1সোর্স 1: নিচের প্রতিটা request শুধু path দেয়, যেমন/post। - দুটো টেস্ট লিখুন। এটা
tests/echo.spec.tsনামে সেভ করুন:
import { test, expect } from '@playwright/test';
test.use({ baseURL: 'https://postman-echo.com' });
test('GET sends the query parameters', async ({ request }) => {
const response = await request.get('/get', { params: { course: 'sdet', step: 17 } });
await expect(response).toBeOK();
const body = await response.json();
expect(body.args).toEqual({ course: 'sdet', step: '17' });
});
test('POST sends a JSON body', async ({ request }) => {
const response = await request.post('/post', { data: { title: 'Buy milk', done: false } });
expect(response.status()).toBe(200);
const body = await response.json();
expect(body.json).toEqual({ title: 'Buy milk', done: false });
});- চালান।
npx playwright test tests/echo.spec.ts। টেস্টগুলো কোনো পেজ load না করেই request পাঠায় Source 1সোর্স 1। - ইচ্ছা করে একটা fail করান। শেষ লাইনের
'Buy milk'বদলে'Buy bread'করে আবার চালান। Error পড়ুন: টেস্ট কী আশা করেছিল আর কী পেয়েছে, মিলিয়ে দেখুন।
How a tester uses it
- Set up and clean up the data. Create what the tests need before they run, and delete it afterwards, with the
beforeAllandafterAllhooks Source 1সোর্স 1. - Mix API and UI. Prepare the server state with an API call before a UI test visits the app. Or act in the browser, then check the server-side result with an API call Source 1সোর্স 1.
- Log in once. If the app can log in through an API faster than through the UI, send the login request with
APIRequestContextand save the authenticated state Source 5সোর্স 5. A setup project logs in once, and each test starts already logged in Source 5সোর্স 5. Storage state works in both aBrowserContextand anAPIRequestContextSource 1সোর্স 1. - Run one test with many data sets. You can parameterize a test at the test level Source 9সোর্স 9, for example with a loop over cases:
const cases = [
{ course: 'sdet', step: '17' },
{ course: 'qa', step: '2' },
];
for (const query of cases) {
test(`echoes ${query.course} step ${query.step}`, async ({ request }) => {
const response = await request.get('/get', { params: query });
await expect(response).toBeOK();
expect((await response.json()).args).toEqual(query);
});
}- Keep the token out of the code. The Playwright docs read the API token from an environment variable,
process.env.API_TOKEN, and send it inextraHTTPHeadersSource 1সোর্স 1.
সহজ English
- Make and remove test data. Create what the tests need before they run, and delete it after, with
beforeAllandafterAllSource 1সোর্স 1. - Mix API and UI. Set up the server with an API call, then open the app in a UI test. Or click in the browser, then check the server with an API call Source 1সোর্স 1.
- Log in once. If logging in by API is faster than by the UI, log in once with an API call and save the logged-in state Source 5সোর্স 5. Then every test starts already logged in Source 5সোর্স 5.
- One test, many data sets. You can parameterize a test at the test level, for example with a loop over cases Source 9সোর্স 9.
- No token in the code. The Playwright docs read the token from an environment variable,
process.env.API_TOKENSource 1সোর্স 1.
বাংলা ব্যাখ্যা
- টেস্ট ডেটা তৈরি আর মুছে ফেলা। টেস্টের আগে যা লাগবে তা
beforeAllhook-এ তৈরি করুন, আর পরেafterAllhook-এ মুছে দিন Source 1সোর্স 1। - API আর UI একসাথে। UI টেস্ট app খোলার আগে API call দিয়ে server-এর অবস্থা তৈরি করুন। অথবা browser-এ কাজ করে API call দিয়ে server-এর ফলাফল যাচাই করুন Source 1সোর্স 1।
- একবার login। UI-এর চেয়ে API দিয়ে login দ্রুত হলে,
APIRequestContextদিয়ে login request পাঠিয়ে logged-in অবস্থাটা সেভ করুন Source 5সোর্স 5। একটা setup project একবার login করে, আর প্রতিটা টেস্ট আগে থেকেই logged-in অবস্থায় শুরু হয় Source 5সোর্স 5। - এক টেস্ট, অনেক ডেটা। Test level-এ একটা টেস্টকে parameterize করা যায়, যেমন case-এর একটা list-এর ওপর loop চালিয়ে Source 9সোর্স 9।
- কোডে token রাখবেন না। Playwright-এর docs API token নেয় একটা environment variable থেকে,
process.env.API_TOKEN, আর সেটাextraHTTPHeaders-এ পাঠায় Source 1সোর্স 1।
Common mistakes
- Waiting for a 404 to throw. By default, a request returns a response for every status code Source 2সোর্স 2. Assert the status, or set
failOnStatusCodeSource 2সোর্স 2. - Calling
json()on a body that is not JSON.json()throws if the body cannot be parsed Source 3সোর্স 3.text()returns the body as text Source 3সোর্স 3, so log it to see what came back. - A missing
awaitbeforetoBeOK(). The documented usage isawait expect(response).toBeOK(), and it returns a Promise Source 4সোর্স 4. - Tests that share data. Fixtures are isolated between tests Source 6সোর্স 6. Create the data that the tests need in a
beforeAllhook, and delete it in anafterAllhook Source 1সোর্স 1. - The login state in Git. The saved state file can hold cookies that let someone act as your test account. The Playwright docs strongly advise against committing it Source 5সোর্স 5.
সহজ English
- Waiting for a 404 to throw an error. By default, you get a response back for every status code Source 2সোর্স 2. Check the status yourself, or turn on
failOnStatusCodeSource 2সোর্স 2. - Reading JSON that is not JSON.
json()fails if the body is not JSON.text()gives the body as text Source 3সোর্স 3: log it to see what came back. - No
awaitbeforetoBeOK(). Writeawait expect(response).toBeOK(): it gives back a Promise Source 4সোর্স 4. - Tests that share data. Each test gets its own fixtures Source 6সোর্স 6. Make the data the tests need in a
beforeAllhook, and delete it in anafterAllhook Source 1সোর্স 1. - The login file in Git. It can hold cookies that let someone act as your test account. Playwright strongly advises not to commit it Source 5সোর্স 5.
বাংলা ব্যাখ্যা
- 404 পেলে error হবে ভেবে বসে থাকা। ডিফল্টভাবে যেকোনো status code-এ response ফেরত আসে Source 2সোর্স 2। Status নিজে assert করুন, অথবা
failOnStatusCodeচালু করুন Source 2সোর্স 2। - JSON নয় এমন body-তে
json()ডাকা। Body parse না হলেjson()error দেয় Source 3সোর্স 3।text()body-টা text হিসেবে দেয় Source 3সোর্স 3, তাই কী এল দেখতে সেটা log করুন। toBeOK()-এর আগেawaitনা দেওয়া। Docs-এ এর ব্যবহারawait expect(response).toBeOK(), আর এটা একটা Promise ফেরত দেয় Source 4সোর্স 4।- টেস্টগুলোর মধ্যে ডেটা ভাগ করা। প্রতিটা টেস্টের fixture আলাদা Source 6সোর্স 6। টেস্টের দরকারি ডেটা একটা
beforeAllhook-এ তৈরি করুন, আরafterAllhook-এ মুছে দিন Source 1সোর্স 1। - Login state file Git-এ রাখা। এই file-এ এমন cookie থাকতে পারে, যা দিয়ে কেউ আপনার test account হয়ে কাজ করতে পারে। Playwright এটা commit করতে জোরালোভাবে নিষেধ করে Source 5সোর্স 5।
What a tester can check
Tutorial advice
This list is the tutorial's own advice, not a fact from a source.
- Depth of the checks. Does each test check the status and the fields of the body, not only that the call was OK?
- Negative cases. Are there tests for a missing field, a wrong type, no token, an expired token, and the id of another user's record?
- Own data. Does each test create the data it needs and remove it, so the suite runs on an empty system and twice in a row?
- Secrets. Are tokens read from the environment, and kept out of the logs, the HTML report, and Git?
- One login. Does the suite log in once and reuse the state, instead of logging in inside every test?
- Useful failures. When a call fails, does the report show the request and the response body?
সহজ English
Tutorial advice
This list is the tutorial's own advice, not a fact from a source.
- Real checks. Does each test check the status and the fields of the body, not only "OK"?
- Bad cases. Are there tests for a missing field, a wrong type, no token, an expired token, and another user's record?
- Own data. Does each test make and remove its own data, so the suite can run twice in a row?
- Secrets. Are tokens read from the environment, and kept out of the logs, the report, and Git?
- One login. Does the suite log in once, not in every test?
- Clear failures. When a call fails, can you see the request and the response?
বাংলা ব্যাখ্যা
Tutorial advice
এই তালিকা টিউটোরিয়ালের নিজের পরামর্শ, কোনো source থেকে নেওয়া তথ্য নয়।
- চেক কতটা গভীর। প্রতিটা টেস্ট কি status আর body-র field-গুলো চেক করে, শুধু call OK হলো কি না সেটা নয়?
- Negative case। Field না থাকা, ভুল type, token না থাকা, মেয়াদ শেষ token, আর অন্য user-এর record-এর id, এগুলোর টেস্ট আছে কি?
- নিজের ডেটা। প্রতিটা টেস্ট কি নিজের ডেটা তৈরি করে আর মুছে দেয়, যাতে suite খালি system-এ আর পরপর দুবার চলতে পারে?
- Secret। Token কি environment থেকে পড়া হয়, আর log, HTML report আর Git-এর বাইরে রাখা হয়?
- একবার login। Suite কি একবার login করে সেই state আবার ব্যবহার করে, নাকি প্রতিটা টেস্টে login করে?
- কাজের failure। একটা call fail করলে report-এ কি request আর response body দেখা যায়?
Key words
| Term | Meaning |
|---|---|
APIRequestContext | The Playwright class that sends HTTP requests to an API, with no page. |
request fixture | The built-in fixture of Playwright Test that sends API requests; it respects baseURL and extraHTTPHeaders. |
APIResponse | The response of a request: status(), ok(), json(), and text(). |
toBeOK() | The assertion that the status is in the range 200..299. |
baseURL | The base URL that the request fixture respects; the requests then give only the path. |
extraHTTPHeaders | Additional HTTP headers to be sent with every request, such as a token. |
failOnStatusCode | The option that makes a request throw on codes other than 2xx and 3xx. |
| Storage state | The saved authenticated state; it works in both a BrowserContext and an APIRequestContext. |
| Setup project | A project that logs in once, so that each test starts already logged in. |
| Parameterized test | A test parameterized at the test level, for example with a loop over cases. |