API Automation Testing Via Playwright Using Python
API Automation Testing Via Playwright Using Python

The release of API Testing Via Playwright Using Python at Vivify Healthcare is part of the company’s research and development efforts to find the best technology stack that is both sustainable and affordable.
Playwright is a cross-browser automation tool that allows developers to automate user interactions with web applications. Playwright is not a term that is typically associated with the Python programming language. However, there is a Python package called “playwright-python” which provides a Python API for the Playwright testing and automation library. With Playwright Python, developers can write scripts to interact with webpages using a variety of browsers such as Chromium, Firefox, and Webkit.

Playwright provides a powerful and flexible API that allows developers to automate a wide range of user interactions, such as clicking buttons, filling in forms, and navigating between pages. It can be used for Automated testing, Web scraping, Task automation, Performance testing. Overall Playwright is a powerful tool for developers looking to automate web application interactions and streamline their development and testing processes.

Playwright Using Python
Playwright Using Python
Vivify Healthcare

API Automation Testing Using Python Via playwright

Playwright is a opensource framework that provides a high-level capability to enable automation testing. It also allows you to write scripts in Python that can control web browsers and perform various tasks such as filling out forms, clicking buttons, navigating pages, and taking screenshots.

Playwright offers several advantages over other browser automation tools:

  • It supports multiple browsers, which means you can test your web application on different browsers using the same script.
  • It has a simple and intuitive API that makes it easy to use even for beginners.
  • It provides better performance and reliability compared to other tools. In summary, Playwright is a powerful and flexible tool for automating web browsers in Python, and it can be used for a wide range of tasks such as testing, web scraping, and web automation.

Playwright enables automation of web browsers (including Chrome, Firefox, and Safari) for end-to-end testing of web applications. While Playwright is primarily designed to be used with JavaScript, it is possible to use it with Python as well by using the py_playwright library, which provides a Python API for Playwright.

Installation of API Automation Testing Using Python via playwright:

Install Playwright and its dependencies using pip:

pip install playwright
playwright install

Example Test :

Create a test_playwright.py file inside the current working directory or in a sub-directory with the code below:

import re
from playwright.sync_api import Page, expect

def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page):

# Expect a title "to contain" a substring.

# create a locator
get_started = page.get_by_role("link", name="Get started")

# Expect an attribute "to be strictly equal" to the value.
expect(get_started).to_have_attribute("href", "/docs/intro")

# Click the get started link.

# Expects the URL to contain intro.

Running the Example Test :

By default tests will be run on chromium. This can be configured via the CLI options. Tests are run in headless mode meaning no browser UI will open up when running the tests. Results of the tests and test logs will be shown in the terminal.


How to use Playwright for API testing :

Playwright can be used to get access to the REST API of your application.

Sometimes you may want to send requests to the server directly from Python without loading a page and running js code in it. A few examples where it may come in handy:

  • Test your server API.
  • Prepare server side state before visiting the web application in a test.
  • Validate server side post-conditions after running some actions in the browser. All of that could be achieved via APIRequestContext methods.

Writing API Test :

APIRequestContext can send all kinds of HTTP(S) requests over network.

The following example demonstrates how to use Playwright to test issues creation via GitHub API. The test suite will do the following:

  • Create a new repository before running tests.
  • Create a few issues and validate server state.
  • Delete the repository after running tests.

Configure :

In our case; VivifyHealthCare API requires authorization, so we can configure the token once for all tests. While at it, we’ll also set the baseURL to simplify the tests. below example set is without JWT authentication.

import pytest
from playwright.sync_api import Playwright, APIRequestContext
from typing import Generator


def api_request_context(
playwright: Playwright,
)-> Generator[APIRequestContext,None,None]:
request_context = playwright.request.new_context()
yield request_context

Write tests :

Now that we initialized request object we can add a few tests

Post :

from playwright.sync_api import Playwright,APIRequestContext

def test_post_example(api_request_context: APIRequestContext) -> None:
headers = {"Content-type": "application/json"}
data = {
"title": "foo",
"body": "bar",
"userId": 1
post_todo = api_request_context.post(
f"https://jsonplaceholder.typicode.com/posts", data=data, headers=headers
# assert post_todo.ok
assert post_todo.json()["title"] == "foo"

Get :

def test_get_example(api_request_context: APIRequestContext:
headers = {"Content-type": "application/json"}
get_todo = api_request_context.get(
f"https://jsonplaceholder.typicode.com/posts/1",  headers=headers
assert get_todo.ok

Put (Update):

def test_put_example(api_request_context: APIRequestContext:
headers = {"Content-type": "application/json"}
data = {
"title": "foo2",
"body": "bar",
"userId": 1
put_todo = api_request_context.put(
f"https://jsonplaceholder.typicode.com/posts/1", data=data, headers=headers
assert put_todo.ok
assert put_todo.status == 200
assert put_todo.json()["title"] == "foo2"

Delete :

def test_delete_example(api_request_context: APIRequestContext):
headers = {"Content-type": "application/json"}
delete_todo = api_request_context.delete(
f"https://jsonplaceholder.typicode.com/posts/1", headers=headers)
assert delete_todo.ok
assert delete_todo.status == 200

To Run :

To run this script, need to be in directory and run the following command :


Results :

Vivify Healthcare
Skip to content