Coder Social home page Coder Social logo

vivifyhealthcare / playwright_api_testing Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 2.0 168 KB

API Automation Testing Via Playwright

Home Page: https://vivifyhealthcare.com/blogs

License: MIT License

Python 100.00%
lifeeazy playwright python vivifyhealthcare

playwright_api_testing's Introduction

API Testing Via Playwright Using Python

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.

How to Installation :

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):
page.goto("https://playwright.dev/")

# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))

# 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.
get_started.click()

# Expects the URL to contain intro.
expect(page).to_have_url(re.compile(".*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.

pytest

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

@pytest.fixture(scope="session")

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

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 :

pytest

Results :

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.