This update 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.
Problem Statement
Testing Setup at one place for API Endpoints related to multiple Products/ Projects (Grouping) so that it is not required to have multiple Testing Setup for different different projects.
Solution
Reshaped the Testing Code Block for Grouping the Endpoints with Product/Project and Save the Test Cases and their respective outcomes in to different collections.
Python-based API testing and automation tool that allows testers and developers to efficiently organize, execute, and manage test cases for multiple projects or products. The project utilizes Django as the web framework and MongoDB as the backend database for seamless data storage. It offers a user-friendly interface for grouping API collections by product (e.g.,Lifeeazy and IvinPro) and uploading test cases for each product.The tool automates the execution of test cases, captures the data, and stores the results in MongoDB.
Prerequisites:
- Install Python and MonogoDB on your system.
- Install Playwright:
pip install playwright
playwright install
- Install necessary dependencies:
pip install pytest
pip install playwright
pip install requests
Setup your project:
- Create a new project directory
- Inside the project directory, create a virtual environment (optional but recommended) by running:
python -m venv venv
- Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- Configure the MongoDB connection settings in the Django settings file.
- Django project with the required components (models, serializes, views, and URLs) to handle API requests.
Excel Test Cases:
- Create an Excel document containing test cases for both the “Lifeeazy” and “Ivinpro” APIs.
- Organize the test cases with details such as API endpoints, request payloads, status, status_code, method, testcase_version, expected responses, etc.
Uploading Test Cases via API:
- Write a view function that reads the test cases from the Excel document for both “Lifeeazy” and “Ivinpro” APIs.
- Create API endpoints to allow users to upload test cases for API collections.
- Use a library like pandas to parse the Excel file and extract the relevant information.
- Accept test case data in a standardized format, including API endpoints, request payloads, and expected responses, etc.
- To run the command in the Terminal:
python manage.py runserver
Saving Test Cases to MongoDB:
- In the views function, process the uploaded test cases and save them to separate MongoDB collections for each product.
- For example, save the Lifeeazy test cases to the “lifeeazy_test_cases” collection and the IvinPro test cases to the “ivinpro_test_cases” collection.
Writing Playwright Test Cases:
- Once the data is saved in MongoDB, you might perform API testing.
- Utilize Playwright to write test cases for each product’s APIs separately (e.g., Lifeeazy and IVINPro).
- Send a specific request to the endpoint (e.g., GET, POST, PUT, DELETE) with the necessary parameters.
- Capture and validate the response received from the API endpoint.
- Ensure that each Playwright test case corresponds to a specific API endpoint and captures the relevant response data.
Test Case Execution:
- Execute the Playwright test cases for both “Lifeeazy” and “Ivinpro” APIs.
- To run this script, need to be in directory and run the following command :
pytest --template=html1/index.html --report=report.html
Saving Test Results to MongoDB:
- After executing the test cases, save the test results and captured data to separate MongoDB collections for each product.
- For instance, store the Lifeeazy test results in the “Lifeeazy_test_results” collection and the IvinPro test results in the “IvinPro_test_results” collection.