DEV Community

sankar
sankar

Posted on • Updated on

Python selenium architecture and Virtual Environment:

Introduction to Selenium:

Automation:

Automation is a technology that is used to perform certain tasks with minimum or no human interactions or interference or efforts.

Selenium:

Selenium is an automation framework. It is an open source softwares (It is nothing but where the source code is distributed, making it available for using, modifying or distributing with it original rights. And it does not require any license to download or use it.)
It was created by Jasson Huggins in 2004. It was written in pure Java.
Selenium can be used with various other programming languages like Python, JavaScript, C#, Java, PHP and so on.
With selenium we can validate or verify the web applications.
Selenium is predominantly used in Automation Testing

Advantages of Selenium:

It is a open source framework
It can work with multiple operating systems like Windows, Mac, Linux / Unix
It works with different and multiple web browsers like Microsoft Edge, Chrome, Firefox, Safari, Opera etc.
You can integrate with any other testing framework like Pytest, Python Behave and so on along with Selenium
It uses less CPU and RAM during its working.

Disadvantages of Selenium:

It supports only web-based applications
You need to have a knowledge of Selenium to work with it as you need to know Python / Any programming language and the framework well. (The learning curve is hard)
You cannot automate CAPTCHA and SMS based OTP verification.
It has a small community so it lacks proper online support and due to this you need to put a lot of efforts to finding the solution to your problem
The program writing time is high.

Selenium Architecture

The architecture of Selenium is composed of:
Selenium IDE
IDE - Integrated Development Environment
It is just a web-browser extension that you need to download and install the extension for that particular web browser and start working with it.
One of the major advantages of using Selenium IDE is that it can automate and record the entire automation process as well
People generally do not use IDE, they rather prefer selenium scripts in order to do the same job.

Python Selenium architecture:

The Python Selenium architecture is composed of several components that work together to automate web browser interactions.

Python:

Python serves as the primary programming language for writing Selenium scripts. It provides a simple and readable syntax, making it accessible for both beginners and experienced developers.

Selenium WebDriver:

Selenium WebDriver is the core component of Selenium. It provides APIs for interacting with web browsers programmatically. WebDriver communicates with the browser using a browser-specific driver. For example, ChromeDriver for Google Chrome, GeckoDriver for Mozilla Firefox, etc.

Web Browser:

Selenium supports various web browsers such as Chrome, Firefox, Safari, Edge, etc. It allows users to automate interactions with these browsers, including navigation, form filling, clicking buttons, etc.
Browser Drivers:
Browser drivers act as intermediaries between Selenium WebDriver and the web browsers. They translate Selenium commands into browser-specific actions. Each browser requires its own driver. Selenium provides separate driver executables for different browsers.

Remote WebDriver:

Selenium also supports running tests on remote machines or Selenium Grids. Remote WebDriver allows you to execute Selenium tests on a different machine than where your code is running. This is useful for distributing tests across multiple environments or running tests in parallel.

Testing Frameworks:

While not strictly a part of Selenium's architecture, testing frameworks like pytest, unit test, or Behave are often used alongside Selenium for organizing and executing tests efficiently.

Selenium Locators:

Id
Class
Name
Tag Name
Link Text
XPATH

Selenium Architecture

The architecture of Selenium is composed of:
Selenium IDE
IDE - Integrated Development Environment
It is just a web-browser extension that you need to download and install the extension for that particular web browser and start working with it.
One of the major advantages of using Selenium IDE is that it can automate and record the entire automation process as well
People generally do not use IDE, they rather prefer selenium scripts in order to do the same job.

Selenium Remote Control

It is an outdated and deprecated technology. We do not use it these days.
It has been replaced with WebDriver which is far more better and easy to use.

Selenium WebDriver:

It is the most important and major component of selenium architecture.
It provides a programming interface between the language and the web-browser.

Selenium Client Library:

It consists of language bindings or commands which you will be using to write in your automation scripts.
They are compatible with W3C protocols like HTTP, HTTPS, TCP, UDP etc
They are the wrappers which send the script commands to the network, for the execution into the web-browser.

Selenium API

It contains a set of rules and regulation which your Programming language (Python) used to communicate with the Selenium
It helps in automation without the need for the user to understand what is happening in the background.

JSON wire protocol

The commands that you write are converted into JSON which is then transmitted across the network or to your web-browser so that it can be executed.
The JSON data are sent to the client using the HTTP protocol
It is used by the browsers also

Browser Drivers

It acts as a bridge between the Selenium Script, Libraries and the browser.
It helps us to run the selenium commands on the web browser.
https://github.com/mozilla/geckodriver/releases/tag/v0.34.0
https://googlechromelabs.github.io/chrome-for-testing/
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/?form=MA13LH

Selenium Grid:

It is used to run parallel test on multiple devices running same or different browsers and we could also simulate at different geographical locations.
We can run multiple test-cases simultaneously at the same time.
It uses the master-slave architecture.

Commands to install selenium and webdriver-manager:

pip install selenium
pip install webdriver-manager

Python Virtual Environment:

A Python virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, as well as additional packages. It helps isolate project dependencies and avoids conflicts between different projects.

Here are some examples of its significance:

1.Dependency Management:

Virtual environments allow you to manage project dependencies separately. You can install specific versions of packages for each project without affecting the system-wide Python installation or other projects.

2.Reproducibility:

By encapsulating dependencies within a virtual environment, you ensure that other developers can replicate your project's environment easily. This enhances collaboration and reproducibility of results.

3.Version Control:

Including the virtual environment directory in version control (e.g., using a requirements.txt file) ensures that everyone working on the project uses the same set of dependencies. It simplifies deployment and avoids unexpected issues due to dependency discrepancies.

4.Isolation:

Virtual environments provide a sandboxed environment where you can experiment with different packages or versions without worrying about affecting other projects or the system-wide Python installation

Virtual Environment:

It is a tool which helps python Developers and testers to keep project dependencies and modules which are required for different projects separate and independent from each other.
With this we can manage our projects and their dependencies far more easily and efficiently.
This allows us to have multiple independent python environments on the same system without them interfering with each other.

Uses of Virtual Environments:
Isolation
Dependency Management

How to create virtual environments in Python

virtualenv:
Install the virtualenv package:
pip install virtualenv
Create the python virtual environment:
virtualenv
Select the virtual environment:
Click ctrl + shift + p and select Python: Select Interpreter

Other way of using pipenv using terminal:

  1. Create the Virtual Environment by: pipenv - -python 3.12
  2. Install the packages pipenv install selenium pipenv install webdriver_manager
  3. Run the python file pipenv run python main.py

Virtual Environment for Python

What is Virtual Environment?
Virtual environments are particularly helpful if the program requires an individual environment, with a unique python version and specific module versions. It prevents package conflicts.
You can use virtualenv to create your isolated environment.

Why do you need a virtual environment?

The virtual environment is a way to keep the python configuration isolated from other projects.
One user has multiple virtual environments on a single computer, one for eahch project. Different libraries can be incorporated in each virtual environment.

virtual environment

How to create a virtual environment using Python3?

Step1:

Install Virtual Environment
pip3 install virtualenv
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 367kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

Step2:

In python 3, you can use this command 'python3 –m venv /path/to/create/the/virtual/env'.

-bash-4.2$ python3 -m venv test_venv
-bash-4.2$ ls
test_venv
-bash-4.2$

Step3: Activate virtual environment

-bash-4.2$ source test_venv/bin/activate
(test_venv) -bash-4.2$

Step4: Install the required libraries, using pip

For the example below,we have used 'flask' library. Flask lets you build web apps, you can make all kinds of things or even plot

(test_venv) -bash-4.2$ pip3 install flask
Collecting flask
Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
100% |████████████████████████████████| 102kB 3.8MB/s
Collecting Jinja2>=2.10.1 (from flask)
Using cached https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
Collecting itsdangerous>=0.24 (from flask)
Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Werkzeug>=0.15 (from flask)
Using cached https://files.pythonhosted.org/packages/ce/42/3aeda98f96e85fd26180534d36570e4d18108d62ae36f87694b476b83d6f/Werkzeug-0.16.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
Using cached https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: MarkupSafe, Jinja2, click, itsdangerous, Werkzeug, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.16.0 click-7.0 flask-1.1.1 itsdangerous-1.1.0
(test_venv) -bash-4.2$

In Python, "env" or "environment" refers to the environment where a Python program runs. This environment includes environment variables, Python packages, and dependencies that are available when the program is executed.

In the context of software development, the use of a "virtual environment" is highly recommended to avoid dependency issues and configuration errors. A virtual environment is an isolated environment in which Python packages and other dependencies can be installed separately from the system's global environment. This allows developers to work on multiple projects with different dependencies without worrying about conflicts between them. Python provides tools such as virtualenv and venv to create and manage virtual environments.

Here are the steps to create and use a virtual environment using the built-in venv module in Python:

Make sure that Python is already installed on your computer. You can check this by running the command python --version in the terminal.

Open a terminal or command prompt and navigate to the directory where you want to create the virtual environment.

Run the command python3 -m venv myenv to create a new virtual environment named "myenv". You can replace "myenv" with any name you like.

Once the virtual environment is created, activate it by running the command source myenv/bin/activate on Linux or macOS, or myenv\Scripts\activate on Windows.

You should now see the name of your virtual environment in the command prompt or terminal, indicating that it is active.

Install any packages or dependencies that you need for your project using the pip package manager. For example, you can run pip install numpy to install the NumPy package.

When you're done working in the virtual environment, you can deactivate it by running the command deactivate.

By creating and using virtual environments, you can avoid conflicts between packages and dependencies, and ensure that your project works correctly regardless of the system environment.

Top comments (0)