DEV Community

Roshan Rumaiza
Roshan Rumaiza

Posted on

Python Selenium Architecture

1.Python Selenium Architecture:

Selenium is a powerful automation tool used for automating web browsers.
Selenium supports multiple browser such as Chrome, Edge, Safari, Firefox and also multiple programming language such as Java, Ruby, C#, Python and Java script.
As Python is versatile and has simple syntax, when selenium is used with Python it becomes a powerful and readable automation tool that supports complex workflow and makes it an excellent choice for writing and maintaining test scripts.

The architecture of Selenium with Python is based on client-server design.
Here different layers work together to perform browser automation.
The architecture consists of four layers namely Selenium client library, JSON wire protocol, browser driver and browser.

  • Selenium Client library(Python binding):

It consists of languages like Java, Ruby, C# ,Python and so on.
Here the automation testers will write the test scripts using Python.
The selenium python binding is a set of libraries that exposes selenium commands to Python. These commands include opening a browser, clicking elements, filling out forms, and handling alerts. Once the test cases is triggered entire selenium code is converted to JSON format.

Example:

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

  • JSON Wire protocol:

JSON stands for Java Script Object Notation. JSON is responsible for transferring the data from server to the client.
Selenium Webdriver is a crucial components that enables interaction between python code and browser. Selenium webdriver translates the Python commands to browser native action. JSON is primarily responsible for transferring the data between HTTP servers. Generated JSON is made available to the browser driver through HTTP protocol.

  • Browser drivers:

Each browser has a specific browser driver.This browser driver will interact directly with the browser and execute the commands by interpreting JSON which they received from the selenium webdriver.
Browser driver as soon as they receive any instruction they run them on the browser. Then the responses is given back in the form of HTTP response.

Chrome- Chromedriver
Edge-msedgedriver
Firefox-Geckodriver

  • Browser:

This is the final layer where action is performed. Here the end user action is performed. Clicking, typing, scrolling and page transitions.
Once the action is performed the response is sent back to the python script for validation.

Request-Response workflows:

    driver = webdriver.Chrome(service=service_obj)
    driver.get('https://www.google.co.in/')
Enter fullscreen mode Exit fullscreen mode

When the above code is executed the entire code is converted into URL with the help of JSON protocol. The Chrome browser will get this URL through HTTP request. The browser driver will utilizes HTTP server to get request from HTTP.As soon as the chrome driver get the URL it passes the request to its respective browser through HTTP for executing the action.
After the execution the response will be sent to browser driver through HTTP. The browser driver will send the response to selenium webdriver via JSON wire protocol. Selenium webdriver will return the response to the python script for validation.

2.Python Virtual Environment:

Python virtual environment is important for managing the project dependencies and ensuring code reproducibility.
Python is a versatile and most widely used programming language known for its simplicity, readability and has rich ecosystem of library.
However managing the project across different system and teams become challenging due to dependency conflicts between projects.
Python virtual environment provide an isolated environment for python projects avoiding conflicts between different project dependencies.

Importance of python virtual environment:

  • Dependency Isolation:
    Different projects may require different versions of same library, virtual environment isolates these dependencies, allowing installing library for one project won't effect another.

  • Reproducibility:
    By specifying the exact versions needed in requirement.txt one can create same environment on different machines, ensuring code runs on every machine.

  • Cleanliness:
    Virtual environment helps to keep the global python installation clean, by preventing clutter of project specific packages.

  • Collaboration:
    It ease collaboration ensuring everyone working on the project has same environment ,avoiding "it works on my machine" issue.

  • Security:
    By isolating projects, it reduces the risk of unauthorized access to the system increasing project stability.

  • Experimentation:
    You can experiment with different libraries and version within the virtual environment without effecting main project.

Example:

If you have an old project built in Python 3.6 while also working on a new one using Python 3.11. Virtual environments let you install and manage both versions without conflict.

**Python Virtual Environment setup:

Python has the built-in venv module for creating virtual environments.

To create a virtual environment on the computer, open the command prompt, and navigate to the folder where you want to create your project, then type this command:

python -m venv myfirstvenv

Top comments (0)