Playwright API Testing Tutorial | Experts View — Devstringx

Devstringx Technologies
4 min readJun 30, 2023


APIs are an integral part of the microservices architecture as they enable communication and interaction between the individual services that make up the application. Each microservice exposes a well-defined set of APIs that other services can use to access its functionality. When running automation tests, you need to automate the UI, the API, or both use cases.

Consider an example where you need to sign in with a token or quickly create a record using an API and then do some UI testing. You need an automated test environment to run API endpoint and UI tests. It also supports end-to-end testing, generating the required data, setting up microservices, and setting up your environment.

Playwright API Testing

Playwright is one such platform that not only provides the ability to automate your UI flow but can also help you automate your API. It has added support for API testing since version 1.16, making it ideal for E2E testing. Playwright allows you to access your app’s REST API. Sometimes you want to send requests directly from Node.js to the server without loading the page and running the JS code on it.

Why Do We Use Playwright for API Testing?

The playwright is suitable for testing the user interface of apps based on APIs, such as Single Page Apps (SPAs) or Progressive Web Apps (PWAs) that use APIs to retrieve data or perform other online operations. The APIRequest class is used to create an APIRequestContext instance. An API request instance may be created at Playwright’s request.

How to Write an API Test?

Playwright Test has a built-in query fixture that takes into account the configuration options we provide, such as baseURL or extra HTTP headers, and is ready to send specific queries.

const {test,expect} = require(‘@playwright/test’);

const {APIUtils}=require(‘../../utils/APIUtils’);

const {customtest} = require(‘../../utils/test-base’);

global[‘token’] = “”;

test.describe(‘Login Application API Test’, () => {

customtest(‘Admin Login’, async ({request, adminApiData_Login})=> {

const apiUtils = new APIUtils(request, adminApiData_Login);

const response= await apiUtils.postLogin(‘login’);

global.token =;

console.log( global.token);


expect(response.message).toEqual(‘Login successfully’);



test.describe(‘get employee details’, () => {

customtest(‘get response’, async ({request})=>{


const apiUtils = new APIUtils(request);

const response = await apiUtils.get({employeeUserId:’bbaf854789323cbb6fdb89',token:token},’viewEmployee’);


expect(response.message).toEqual(‘Record found successfully’);




customtest(‘view Record’, async ({request, adminApiData_Login})=> {

const job_details_dataSet = require(‘../../testData/api_detail.json’);


const apiUtils = new APIUtils(request, adminApiData_Login);

const response= await{payload:job_details_dataSet,token:token}, ‘employeeList’);


expect(response.message).toEqual(‘Record found successfully’);


  • Describe

This optional method is used to group any number of test statements.

  • Test

Pass a function to this method and the playwright’s test runner will run that function as a test block.

  • Request

A device that honors the specified configuration options such as baseURL or extra HTTP headers and is ready to send specific requests.

  • Expect

This is a condition that must pass the test. Compare the received parameter with the matcher. It also provides access to built-in, which you can use to validate different things.

Good to Read:- Playwright Automation Tool: Pros & Cons, Framework

Configuration in the Config File

Since the base URL and the authentication token remain the same when tested, it can also be called in Playwright.config.js with a different configuration.

API Methods Using in Playwright

Below are the Four methods.

  • GET
  • POST
  • PUT

1) GET

By providing an API endpoint, we can read the data using a GET request. We need to pass some parameters to get the required data from the API. By providing the response code, we can verify the request. The response code for a GET request is 200. You can also confirm the body of the JSON response.


The POST method is used to add a new record via the API, where we need to pass the data as a payload, e.g. B. First Name, Last Name, etc. The response code for a successful POST request is 201.

3) PUT

The PUT request is used to update existing records through API. We need to pass the data we want to update as a payload, for example, first and last name, etc. The response code for a PUT request is 200.


We can delete existing records via the API with a DELETE request. It would be better to add a record before deleting it. You need to add the ID to the DELETE URL. To delete a record using the API, we first need to pass in the URI (Universal Resource Identifier) ​​of the record. The response code for a DELETE request is 200.


  • Faster execution times than any other testing tool.
  • Can be integrated with other test platforms.
  • The execution can be carried out both headed and headless.
  • Works without any external libraries or dependencies.
  • Can be integrated into any CI/CD pipeline.
  • Higher performance stability than other test tools on the market.

Originally published at on June 30, 2023



Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services