Python Selenium Architecture
Selenium is a widely-used open-source tool designed for automating web browsers.
When utilizing Selenium with Python, its architecture consists of several layers and components that collaborate to automate browser activities.
Overview
Python Test Script
- Written using Selenium Python Bindings.
- Contains test logic: locate elements, click, send keys, assertions, etc.
Selenium WebDriver
- A Python library (selenium) that provides an interface to communicate with different browser drivers.
- Converts the Python commands into HTTP requests (JSON Wire Protocol was used by Selenium <= 3.x and now W3C WebDriver protocol is used by Selenium 4+).
Browser Drivers
- Acts as a bridge between WebDriver and browser.
- Examples: chromedriver for Chrome, geckodriver for Firefox.
- Accepts WebDriver commands in JSON format over HTTP (following the W3C WebDriver standard) and communicates with the actual browser.
Browsers
- Actual browsers like Chrome, Firefox, Edge, Safari.
- Perform requested actions (like opening a URL, clicking a button, etc.).
Benefits of this architecture
- Enables compatibility across different browsers.
- Maintains test logic that is independent of programming languages (the same architecture can be used in Java, C#, etc.).
- Facilitates remote execution through Selenium Grid.
- Separates test scripts from the browser, enhancing flexibility
Python Virtual Environment
A Python Virtual Environment is an independent directory that includes a specific version of Python along with its associated libraries. It segregates dependencies for various projects, avoiding conflicts and ensuring that each project operates in its own distinct environment.
Advantages
- Prevent conflicts among various projects
- Maintain a clean and tidy system
- Install and test new packages securely
- This is particularly beneficial when multiple projects need different versions of the same package.
Steps to setup Python virtual environment
Step 1:
Create a virtual environment. This creates a folder "venv" with its own Python and pip libraries.
python -m venv your_project_env
Note: pip is a package management system designed for installing and managing software packages developed in Python. It enables users to effortlessly install, upgrade, and uninstall Python packages from the Python Package Index (PyPI) as well as other repositories.
Step 2:
Activate the virtual environment for Windows
your_project_env\Scripts\activate
Once the virtual environment is activated we can see the console is turned to (venv)
Step 3:
Install the required packages using
pip install package_name
This installs the given "package_name" library only inside the virtual environment, not at global level.
Step 4:
Freeze the local package environment
pip freeze > requirements.txt
This requirement.txt with a list of all installed packages and their versions. It can be used to recreate the same environment later.
Step 5:
Deactivate the Environment
To get back to the default Python environment
deactivate
Examples to understand the use of Virtual Environment better
Example 1:
To create a weather application using virtual environment
# Create a virtual environment
python -m venv weather_env
# Activate the environment
# Windows:
weather_env\Scripts\activate
# Install the requests package
pip install requests
Now the "requests" package can be used only within this project, without disturbing the global setup.
Example2:
If I want to include fun jokes to one of my project, then it can be done via virtual environment, without changing the global setup.
python -m venv add_joke
add_joke\Scripts\activate
pip install pyjokes
#importing the pyjokes inside the class
import pyjokes
print("😂 Start your day with a fun joke:", pyjokes.get_joke())
Example 3:
Consider 2 different projects need different version of requests package, then activating virtual environment for each project with the required version of requests package allows the projects to run without any conflicts.
# Project A
python -m venv projectA_env
projectA_env\Scripts\activate
pip install requests==2.25.0
deactivate
# Project B
python -m venv projectB_env
projectB_env\Scripts\activate
pip install requests==2.31.0
Example 4:
Consider if we want to test a new script using "yagmail", we can use virtual environment instead of changing the global setup.
python -m venv yagmail_env
yagmail_env\Scripts\activate
pip install yagmail
We can deactivate the venv once the testing is completed.
Summary:
Whenever we start a new Python project, always create a virtual environment first. It’s one of the best habits we can develop as a Python beginner
Top comments (0)