Skip to contentমূল লেখায় যান

Step 17 · SDET roadmapধাপ 17 · SDET roadmap

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 চালু বা বন্ধ করুন।

2 min2 মিনিট8 steps8টি ধাপ
  • status()স্ট্যাটাস—
  • ok()ok() মান—
  • failOnStatusCodefailOnStatusCode অপশনoff
  • Throwsছোড়ে error—
test({ request })test({ request })ConfigকনফিগAPIAPIserverসার্ভারAPIAPIResponseরেসপন্স

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

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

বাংলা ব্যাখ্যা

  • 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 নেয়। params URL-এ query parameter যোগ করে। data হলো body: object দিলে সেটা JSON হয়ে যায়, আর আপনি না দিলে content-type header হয় application/json Source 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 ফেরত আসে। failOnStatusCode option দিলে 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.

  1. Set the base URL. test.use() in the test file sets baseURL for its tests, to simplify them Source 1সোর্স 1: each request below gives only the path, such as /post.
  2. 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 });
});
  1. Run them. npx playwright test tests/echo.spec.ts. The tests send their requests without loading a page Source 1সোর্স 1.
  2. 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.

  1. Set the base URL. test.use() sets baseURL for the tests in the file, to make them simpler Source 1সোর্স 1: each request below gives only the path, such as /post.
  2. 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 });
});
  1. Run them. npx playwright test tests/echo.spec.ts. The tests send their requests without loading a page Source 1সোর্স 1.
  2. 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।

  1. Base URL ঠিক করুন। টেস্ট সহজ করতে test file-এ test.use() দিয়ে সেই file-এর টেস্টগুলোর baseURL ঠিক করা হয় Source 1সোর্স 1: নিচের প্রতিটা request শুধু path দেয়, যেমন /post।
  2. দুটো টেস্ট লিখুন। এটা 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 });
});
  1. চালান। npx playwright test tests/echo.spec.ts। টেস্টগুলো কোনো পেজ load না করেই request পাঠায় Source 1সোর্স 1।
  2. ইচ্ছা করে একটা 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 beforeAll and afterAll hooks 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 APIRequestContext and 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 a BrowserContext and an APIRequestContext Source 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 in extraHTTPHeaders Source 1সোর্স 1.

সহজ English

বাংলা ব্যাখ্যা

  • টেস্ট ডেটা তৈরি আর মুছে ফেলা। টেস্টের আগে যা লাগবে তা beforeAll hook-এ তৈরি করুন, আর পরে afterAll hook-এ মুছে দিন 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

সহজ English

বাংলা ব্যাখ্যা

  • 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। টেস্টের দরকারি ডেটা একটা beforeAll hook-এ তৈরি করুন, আর afterAll hook-এ মুছে দিন 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

TermMeaning
APIRequestContextThe Playwright class that sends HTTP requests to an API, with no page.
request fixtureThe built-in fixture of Playwright Test that sends API requests; it respects baseURL and extraHTTPHeaders.
APIResponseThe response of a request: status(), ok(), json(), and text().
toBeOK()The assertion that the status is in the range 200..299.
baseURLThe base URL that the request fixture respects; the requests then give only the path.
extraHTTPHeadersAdditional HTTP headers to be sent with every request, such as a token.
failOnStatusCodeThe option that makes a request throw on codes other than 2xx and 3xx.
Storage stateThe saved authenticated state; it works in both a BrowserContext and an APIRequestContext.
Setup projectA project that logs in once, so that each test starts already logged in.
Parameterized testA test parameterized at the test level, for example with a loop over cases.
  1. [1]
    API testing

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  2. [2]
    APIRequestContext

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  3. [3]
    APIResponse

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  4. [4]
    APIResponseAssertions

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  5. [5]
    Authentication

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  6. [6]
    Fixtures

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  7. [7]
    Configuration (use)

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  8. [8]
    Test requests in Postman using the Echo API

    Postman · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  9. [9]
    Parameterize tests

    Playwright (Microsoft) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15