API test automation with Playwright

Last updated on Sep 30, 2024

Organizations have increasingly been embracing microservices architecture for their enterprise projects. It necessitates APIs as a core infrastructure requirement for communication among different modules of the web stack. It is thus imperative to implement a proper API testing strategy for such web applications.

After evaluating different API testing frameworks, we found Playwright to be a suitable foundation to develop a comprehensive, and seamless solution for the WordPress platform. And our QA team built a custom tool, API Playwright Utils.

Created by Microsoft, Playwright is a Node.js library for test automation, with new features and functionalities being added continuously. It is highly modular with a plethora of integration options and cross-browser support. This blog post is a run-down of the solution we built, the benefits we packed in, and its implementation.

Capabilities of API Playwright Utils

Playwright has a bunch of interesting features built on the request-response model:

  • Our framework enhances Playwright’s API testing functionality with 10 custom utils that support all types of API requests, and verify their corresponding response codes. With proper configuration, these utils can be reused for different test cases.
  • Every utils function includes a request context for better control over the use of fixtures, enabling multiple user tokens to access different types of requests. As it helps make multiple assertions with internal data from the responses, our framework minimizes the codes for executing test cases and outputs the responses in JSON format.

Pros of API test automation

  • User testing automation of applications with effortless identification of regression bugs.
  • Blazing fast execution of tests in parallel. Enormously time-efficient when compared with manual testing, which can take hours.
  • Integration with CI/CD pipeline to check the stability of the web application. 
  • Utility in creating various testing scenarios, and enhanced API coverage for different types of edge test cases.

Cons of API test automation

  • Performing test automation with API is not sufficient in itself for complete quality assurance, as it requires additional UI testing as well.
  • Configuring API test automation can be time-consuming without pre-defined software requirements, and the software scripts may need frequent upkeep.

Installation and configuration

Implementing the API Playwright Utils framework is quite a streamlined process.  

First, clone our GitHub repository for initiating the installation. To install the dependencies, run the “npm install” command.

Inside the project directory, find the sample.env file, enter the tokens, and baseURL into this file and rename it as “.env”.

API Playwright installation

Within the same project directory, navigate to the utils folder and find the response.js file. Insert all the response statuses for different requests as per the project requirements. 

Note that every project requires a different response status. 

API Playwright configuration

Find the payload.js file in the directory to insert all the JSON payloads wrapped in custom functions, as shown in the following screenshot.

API Playwright test

After configuring the response, the test cases can be added to the tests folder with the procedure described in the general Playwright test method.

Import all the functions to be executed from the utils folder. To understand the pattern for writing test cases, you can explore a few demo test cases from the sample.spec.js file. Implementing the utils will help make your test cases short and uncomplicated.

Find some demo test cases inside the sample.spec.js file

Executing test cases

Running “npx playwright test” command executes all the test cases.

For executing a specific test case, run the “npx playwright test [filename]” command.

Refer to Playwright’s official documentation to further inform yourself about the different options for running tests.

npx playwright test

Preparing HTML Reports

Playwright can generate custom reports out of the box with several options. Here is the official documentation detailing built-in reporters. 

Use cases

At rtCamp, this framework is used in projects where the scope warrants an application. Here are some of its common use cases:

  • Setup & preparation of API testing environments, and verifying all the request endpoints.
  • Effecting positive and negative scenarios for API requests to verify whether all the test cases are properly covered.
  • Conducting regression testing with API test suites before deployment to verify regression bugs.

Conclusion

While there are other solutions out there, we believe that the test automation features of our implementation of API Playwright Utils will dovetail well with applications crafted around WordPress.

Explore our Quality Assurance Services and drop us a line if you’d like to build or improve your QA pipeline!

Links: API Playwright Utils | Quality Assurance Services | We’re Hiring

On this page

Contributor

Abhijit Prabhudan

Abhijit

Abhijit Prabhudan

Technical Writer

Comments

Leave a Reply