Playwright is a powerful open-source automation framework for web applications. It allows you to automate browser tasks and interactions with a user-friendly API. Whether you’re testing web applications or performing repetitive tasks, Playwright simplifies the process, making it easier to script and execute actions in multiple browsers.
Playwright for Python is a powerful automation tool that lets you control web browsers with Python code. It’s perfect for tasks like web scraping, testing, and automating repetitive web interactions.
Steps to Set up Playwright with Python
Setting up Playwright is relatively simple, and here are the basic steps to get started
Prerequisites
1. Python: Ensure that Python is installed on your system. Python 3.8 or higher is needed
To check if Python is installed, open your terminal or command prompt and run
python --version
If not, install Python using the below command. This will install Python 3. x.
sudo apt update
sudo apt install python3
2. Package Manager (pip): In this article, I prefer to use the PIP Package Manager since it’s the most common one
Verify that you have the Python package manager, pip, installed. It is usually included with Python installations. You can check its version by running
pip --version
If not installed install it using
sudo apt update
sudo apt install python3-pip
3. Virtual Environment: Creating a virtual environment is a good practice to isolate your Python environment for different projects. You can create a virtual environment using the following commands. Replace the “PSU” with your ENV name.
python3 -m venv PSU
Let’s get into the setup steps
1. Update and Upgrade: Get the system updated using the below command
sudo apt update && sudo apt upgrade
2. Create a new folder: Create a new folder to keep it separate
3. Install Playwright: Switch to the new folder and install Playwright. You can install Playwright using “pip”
pip install playwright
4. Initialize Playwright: After installation, you need to initialize Playwright to download the browser binaries. You can do this by running the following command
playwright install
5. Testing framework: Here I am using Pytest as a testing framework. You can indeed install pytest-playwright
along with Playwright
pip install pytest-playwright
Once you have both Playwright and pytest-playwright
installed, you can use the features provided by pytest-playwright
to write and run browser based tests using Playwright within Pytest test functions
5. Get started!
Yay, you are done with the setup. Activate the Virtual Environment to get started (Make sure you give the right path and name of the environment name)
Now you can generate the automation code using Playwright Codegen
playwright codegen https://example.com/
This will open up a Chromium browser with the given site address and a code writer window with options to switch between different test scripts. Here I have chosen Pytest
Perform whatever actions you want to automate on the site and the code for that will be generated
6. Run a sample test
Create a test file
Paste the code in the file
Run it using pytest
That’s it, You are done!
Keep in mind that sometimes you need to alter the code for more modifications
Reference: You can set up Playwright with other test frameworks too