# API test automation with Playwright

*Published:* 2023-01-17
*Author:* Aviral Mittal

> Our in-house API testing framework built on Playwright, customised for applications crafted around WordPress.

[QA Automation](https://rtcamp.com/blog/series/qa-automation/ "QA Automation")

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](https://playwright.dev/) 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](https://github.com/rtCamp/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](https://github.com/rtCamp/api_playwright_utils#api) 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](https://playwright.dev/docs/api-testing#using-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](https://playwright.dev/docs/test-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](https://playwright.dev/docs/test-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](https://github.com/rtCamp/api_playwright_utils) 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](https://playwright.dev/docs/api/class-testoptions) into this file and rename it as **“.env”**.

![API Playwright installation](https://rtcamp.com/wp-content/uploads/sites/2/2022/11/sample-env.png?resize=720%2C339&quality=100&strip=all)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](https://rtcamp.com/wp-content/uploads/sites/2/2022/11/response-API.png?resize=720%2C369&quality=100&strip=all)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](https://rtcamp.com/wp-content/uploads/sites/2/2023/01/payload-screenshot-playwright.png?w=720&resize=720%2C319&quality=100&strip=all)After configuring the response, the test cases can be added to the **tests** folder with the procedure described in the general [Playwright test method](https://playwright.dev/docs/writing-tests).

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](https://rtcamp.com/wp-content/uploads/sites/2/2022/11/sample-test.png?w=720&resize=720%2C539&quality=100&strip=all)## 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](https://playwright.dev/docs/running-tests).

![npx playwright test](https://rtcamp.com/wp-content/uploads/sites/2/2024/09/npx-playwright-test.webp?resize=720%2C462&quality=100&strip=all)## Preparing HTML Reports

Playwright can generate custom reports out of the box with several options. Here is the [official documentation](https://playwright.dev/docs/test-reporters) 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](https://github.com/rtCamp/api_playwright_utils) will dovetail well with applications crafted around WordPress.

Explore our [WordPress Quality Assurance Services](https://rtcamp.com/quality-assurance-services/) and [drop us a line](https://rtcamp.com/business-inquiry/) if you’d like to build or improve your QA pipeline!

**Links:** [API Playwright Utils](https://github.com/rtCamp/api_playwright_utils) | [Quality Assurance Services](https://rtcamp.com/quality-assurance-services/) | [We’re Hiring](https://careers.rtcamp.com/)!

### Categories and Taxonomies

#### Categories
[Releases](https://rtcamp.com/blog/category/release.md)

#### Service
[WP Quality Assurance](https://rtcamp.com/blog/service/wp-quality-assurance.md), [WP Quality Engineering](https://rtcamp.com/blog/service/wp-quality-engineering.md)

#### Solutions
[QE/QA](https://rtcamp.com/blog/solution/qe-qa.md)

#### Topics
[QE/QA](https://rtcamp.com/topics/qe-qa.md), [WordPress](https://rtcamp.com/topics/wordpress.md)

### Related Posts

- [AEM vs WordPress : Which is the better CMS?](https://rtcamp.com/resources/aem-vs-wordpress.md)
- [Sanity to WordPress migration : A complete how-to guide](https://rtcamp.com/resources/sanity-wordpress-migration.md)
- [Sanity vs WordPress: The honest comparison for enterprises going headless](https://rtcamp.com/resources/sanity-vs-wordpress.md)
- [HubSpot CMS vs WordPress : Should your CRM also run your website?](https://rtcamp.com/resources/hubspot-vs-wordpress.md)
- [A practical guide to the WordPress Abilities API](https://rtcamp.com/resources/abilities-api.md)
