Framework for API Automation using Playwright with Python

Framework for API Automation using Playwright with Python

In today’s fast-paced development environment, automated testing is crucial for ensuring the reliability and quality of APIs. Playwright can be used to get access to the REST API of your application, I’m excited to share a comprehensive API testing framework built using Playwright, Python and Pytest, designed to streamline your testing process and provide detailed reports with Allure.

This article will walk you through the project structure, key functionalities of various modules, and how we leverage popular Python packages to build this framework.

You can find the complete source code of this framework on GitHub: GitHub Repository

We are continuously looking to improve this framework and would love to hear your feedback. What features would you like to see added? How can we enhance the current functionality? Please share your thoughts and suggestions to help us make this framework even better.

Project directory structure:

Here’s an overview of our framework structure:

Playwright_API_Python_Framework/
    │
    ├── testcases/  	
    │   ├── conftest.py             
    │   ├── test_1_auth.py       
    │   ├── test_2_create_booking.py       
    │   ├── test_3_update_booking_by_ID.py       
    │	 ├── test_4_get_booking_by_ID.py       
    │	 ├── test_5_delete_booking_by_ID.py            
    │   └── ...
    ├── testData/                   
    │   ├── createAuth.json      
    │   ├── createBooking.json     
    │   ├── updateBooking.json 
    │   ├── getBookingByID.json	
    │   ├── deleteBookingByID.json
    │   └── ...
    ├── API_Utilities/
    │   ├── api_actions.py  
    │   ├── api_utilities.py  
    │   ├── api_validations.py  
    │	 ├── file_reader.py  
    │   ├── logger_utility.py  
    │	 ├── Shared_API_Data.py  
    │   └── ...
    ├── AutoLogs/
    ├── allure-results/
    ├── execution_reports/
    ├── pytest.ini              
    ├── requirements.txt  
    ├── .env	
    │
          

Here is an overview of the functionalities covered by the "api_actions.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "api_utilities.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "api_validations.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "file_reader.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "logger_utility.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "Shared_API_Data.py" module within the "API_Utilities" package:

Here is an overview of the functionalities covered by the "conftest.py" module within the "testcases" package:

Here is an overview of the functionalities covered by the "test_1_auth.py" module within the "testcases" package:

Here is an overview of the functionalities covered by the "createAuth.json" module within the "testData" directory:

Integration with allure reporting

allure is a lightweight, flexible, and easily extensible test reporting tool that provides a rich set of report types and features to help you better visualize your test results.

pip install allure-pytest        

Configuration allure-pytest library

Update the pytest.ini file to specify where allure reports are stored

[pytest]
# allure
addopts = --alluredir ./allure-results        

View allure report

Run the following command to view the allure report in the browser

allure serve allure-results        


Filtering test case execution

In the daily API testing process, we need to selectively execute test cases according to the actual situation in order to improve the testing efficiency.

Generally, when we use allure test reports, we can use the Allure tag feature to filter the use cases corresponding to the tag to execute the test, but the Pytest framework does not directly support running tests based on Allure tags. so you can use Pytest markers to accomplish this.

Pytest provides a @pytest.mark tagging feature that can be used to tag different types of test cases and then filter them for execution.

Defining Pytest Markers

Edit the pytest.ini file and add the following: customize the type of markers

  • Regression: Marks the use case for regression testing.
  • Smoke: mark it as a use case for smoke testing
  • Positive : mark tests as positive
  • Negative : mark test as negative

markers =
    Regression: marks tests as Regression
    Smoke: marks tests as Smoke
    Positive : mark tests as positive
    Negative : mark test as negative        

  • Running Regression-tagged test cases

pytest -m Regression        


Adapting CI/CD processes

Conclusion

Building a robust API testing framework is essential for maintaining the quality and reliability of your APIs. By leveraging Python, Playwright, Pytest and Allure, this framework provides a comprehensive solution for automating your API tests and generating detailed reports. With a well-structured project and key functionalities to handle various aspects of API testing, this framework aims to streamline your testing process.

We hope this framework helps you in your testing endeavors. Don't forget to check out the GitHub repository and share your feedback to help us improve and add new features.

Happy testing!


要查看或添加评论,请登录

Dipankar Dandapat的更多文章

社区洞察

其他会员也浏览了