DEV Community

guru prasad H
guru prasad H

Posted on

Python Selenium Architecture and Significance of python VENV

Python Selenium Architecture in Detail

Introduction

Selenium is an open-source automation testing tool used to automate web browsers. Python Selenium allows testers to write automation scripts in Python to interact with web applications. Selenium follows a client-server architecture where commands written in Python are translated into browser-specific actions.

Components of Selenium Architecture

A. Python Test Script

This is the automation script written by the tester using Python.

Example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

Responsibilities:

Write test cases
Validate expected results
Interact with web elements

B. Selenium WebDriver API

The Selenium WebDriver API acts as a bridge between the Python script and the browser driver.

Example: The WebDriver converts the Python command into a browser-understandable request.

driver.find_element(By.ID, "login-button").click()

C. Browser Driver

Every browser requires its own driver. The browser driver receives commands from Selenium and forwards them to the browser.

Browser Driver
Chrome ChromeDriver
Firefox GeckoDriver
Edge EdgeDriver
Safari SafariDriver

D. Browser

The browser executes the received commands.

Examples:

Open URL
Click button
Enter text
Select dropdown
Upload files
Download files

E. Web Application

This is the application under test.

Examples:

SauceDemo
Amazon
Flipkart
Gmail

The browser interacts with the application and sends responses back to Selenium.

Working of Selenium Architecture

STEP 1: Python script sends a command using driver.get().
STEP 2: Selenium WebDriver receives the command.
STEP 3: The browser driver converts the command into browser-specific instructions.
STEP 4: The browser executes the instruction.
STEP 5: The browser sends the result back to the browser driver.
STEP 6: The browser driver sends the response to Selenium WebDriver.
STEP 7: The Python script receives the response and continues execution.

Advantages of Selenium Architecture
Supports multiple browsers.
Supports multiple operating systems.
Language-independent (Python, Java, C#, JavaScript, Ruby).
Open source and free.
Integrates with Pytest, TestNG, Jenkins, GitHub Actions, etc.
Supports parallel execution.

Significance of the Python Virtual Environment?

Introduction

A Python Virtual Environment (venv) is an isolated environment that contains its own Python interpreter and installed packages. It allows different projects to use different package versions without affecting each other or the global Python installation.

Why Do We Need a Virtual Environment?

Suppose you have two projects:

Project A

Requires:

selenium==4.20
pytest==8.2

Project B

Requires:

selenium==4.35
pytest==9.1

If both projects use the global Python installation, installing one version may overwrite the other, causing compatibility issues.

A virtual environment solves this problem by keeping dependencies isolated.

Advantages of Virtual Environment:

1. Dependency Isolation: Each project has its own libraries and versions.

2. Avoids Version Conflicts: Different projects can use different versions of the same package.

3. Cleaner Development: The global Python installation remains unchanged.

4. Easy Collaboration: You can generate a requirements.txt file so other developers can install the same dependencies.

5. Safe Package Installation : Installing or upgrading packages affects only the current project.

Creating a Virtual Environment

  1. Create a virtual environment. cmd: python -m venv .venv
  2. Activate it venv: .venv\Scripts\activate [Windows] source .venv/bin/activate [Linux/Mac]
  3. Install Packages: pip install selenium pip install pytest
  4. Deactivate when finished. cmd: deactivate

Importance in Selenium Automation:

Virtual environments are especially useful because different Selenium projects may require different versions of:

Selenium
Pytest
pytest-html
webdriver-manager
allure-pytest

Using a virtual environment ensures that updating packages in one project does not break another project.

Top comments (0)