Wednesday, July 23, 2025

Playwright Web Automation Testing Using Python





In this blogpost, we will discuss how to run Playwright automation tests using Python programming language on Visual Studio code IDE.

I’ll put the post in three points as shown below:
  1. Set Up
  2. Test Automation Framework with Playwright and Python
  3. Integrate Automation Tests with CI

1. Set Up
Do the following installations on your Mac.

  • Install Node: Run the command "brew install node" on your terminal. 
  • Install Python3: Go to Python3-Download


Check python3 version 



Check your path of python3 : Run the command "which -a python3"


Install Playwright & pytest
pip install pytest playwright



Install Playwright Browsers
playwright install


Create and Activate Virtual Environment
python3 -m venv venv


Install Pytest Plugin for Playwright Fixtures
pip install pytest-playwright



2. Test Automation Framework with Playwright and Python
basic boilerplate code using Page Object Design framework is created for this post and the folder structure, page objects, pages, tests and test report are shown below.

The Folder structure looks like this


 Page Objects:  The locators used for the tests. For ex: Login page locators below.

The framework is developed using Page Object Design Pattern.

 Pages : The individual web pages used for the test.

 base_page.py - Contains all the web actions used by the tests and will  
 be inherited by the the other pages like login page, cart page, Inventory  
 page etc..



login_page.py


Test:
test_end_to_end.py


The test will call the needed pages and in-turn in the pages will inherit the base page functions/methods to perform the web actions. Assertions are checked and put in place.

Run the Tests locally:
For all tests : pytest --html=reports/report.html
For specific test : pytest tests/test_login.py -s -v



Test Report: 
From your Report Folder 'reports/report.html'







3. Integrate Automation Tests with CI 
GitHub Actions:- 
Run the Tests on GitHub Actions 
Pipeline:


Create a folder .github/workflows and inside this folder create a yml file with name "playwright.yml" file as shown below


Go to GitHub Actions and check the run



Click on the first one



Click on test as shown below




Check the test report!


Save to your desktop to view the report.


Open the report.html file




For Test code reference, Please see the GitHub repo: https://github.com/JayKishoreDuvvuri/Playwright-Python

Happy Automation Testing Guys :)!

No comments:

Post a Comment

Playwright Web Automation Testing Using Python

In this blogpost, we will discuss how to run Playwright automation tests using Python programming language on Visual Studio code IDE. I’ll p...