What is Playwright in automation?
Playwright is one of the modern automation tools used for automation testing that includes Web, API and Mobile web applications. It is a tool maintained by Microsoft. Playwright supports Cross-Browser testing that includes different browsers such as Chromium, WebKit, and Firefox. It also supports cross-platforms where you can test in platforms such as Windows, Linux, and macOS, locally or on CI, headless. The tool by default supports parallel execution. The execution time required to run a test is also less compared to other tools. The playwright has proved to be resilient and quick.
Can we use Playwright for API testing?
Yes! Playwright sends requests from Node.js so it’s resilient and fast. It comes with an in-built request method so most of the time it doesn’t require any external libraries to function. Playwright calls API independently without using any browsers and using browsers. It can also store cookies for the API requests.
Features:
- Supports different types of languages such as Typescript, JavaScript, Python, .NET, Java.
- Playwright has an auto-wait feature that waits for elements to be actionable prior to performing actions.
- It has the tracing ability to capture videos & screenshots.
Pre-requisite:
To have node and npm installed preferably the latest version.
Installation
The first command to run is the init command:
#Run from your project root directory

#Add as a dependency to your project:

#To run tests in your project:

Syntax:
A simple example to make requests using Playwright:

Configuration in config file:
As the base URL and the Authorization token remains the same throughout the tests, So it can be called inside the Playwright.config.js with other configuration as well.

Create Code [POST]:
Creating new user data using a POST request. While using a POST request a proper mandatory data needs to go inside the Json body that creates a user for e.g: name, email, etc. . Insert the Authorization token with mandatory data of the user. Assert responses with proper data that was passed while creating.

Read Code [GET]:
Reading the created user using the GET request with proper API endpoint. Insert the mandatory parameters inside the Json body. The responses are asserted using expect function.

Update Code [PUT]:
Updating the user using the PUT request with proper API endpoint. While using PUT request proper data needs to be sent inside the Json body e.g: name, email, gender, etc . Then asserting the request using the expect function. If the status of the request arrives at 200 that means the PUT request was successful.

Delete Code [DELETE]:
Deleting user using a DELETE request with a proper API endpoint. While deleting a user that was created earlier, we need to fetch the proper Id that was used while creating the user and pass it into the DELETE request API endpoint. Proper assertions can be done using the expect function from the chai library. If the status arrives to be 204 it means the user is been deleted successfully.

Pros
- Faster execution time than any other testing tool.
- Can be integrated with other testing frameworks.
- Execution can be carried out in headed as well as headless mode.
- It works without any external libraries or dependencies.
- It has video and screenshot features that enable us to debug problems easily.
- It can be integrated with any CI/CD pipeline.
- It has greater execution stability than other testing tools in the market.
Cons
- The community support for Playwright is at a growing stage.
- The documentation and resources are less.
Good starting point for anyone looking to start API testing on playwright !!
Can you share information on how to Debug API tests while using Playwirght with typescript.