Web Automation with Selenium & Python Part 1 [Setup & Installation]

Web Automation with Selenium & Python Part 1: [Setup & Installation]

Once a wise man said, "Enjoy the journey because the destination is a mirage". And in our journey of web automation testing, it will be divided by many episodes.
Once a wise man said, "Enjoy the journey because the destination is a mirage". And in our journey of web automation testing, it will be divided by many episodes.

Once a wise man said, “Enjoy the journey because the destination is a mirage”. And in our journey of web automation testing, it will be divided by many episodes. And in our journey, we will use python and selenium to fight the battle together. In this episode, we will get our gears, set the environments, and forge our weapons. And in every episode, we will learn new features of selenium. By the end of this journey, we will complete an automation testing of a web application. We will make a complete automation testing project in a modular way using object-oriented programming. Be strong. Be brave and be patient.

What is Web Automation?

Imagine a Quality Assurance is checking a login feature of a web application by typing email and password by his/her own hands and submitting the login button. Again and again. Checking if the logo is all right in the login page and it’s redirecting to the dashboard. Not that difficult right?

But imagine a feature that requires multiple forms to submit and each form has many input fields. Can he/she do that again and again? Well, It is humanly possible but in the software industry should we really do that? Wasting a huge amount of time where time is money? The answer is no. Here comes the term called web automation.

In web automation, by writing scripts we can automate those human interactions in a web application. Clicking a menu in the navbar, submitting a form, going to a link can be automated by that script.

Impact of web automation in a QA’s work and personal life

Impact of web automation in a QA's work and personal life

 

Do you remember the character Wonder Women from the DC universe? Let’s imagine her as a QA in a software company. Her daily job is to test all the features of a web application called “Call Your Super Hero”. She is clicking buttons, submitting forms approving things from the admin panel, uploading images, calculating numbers from transactions all day long. She has been doing that for months. Her days are getting dull. Doing the same boring tasks every day. Losing creativity. Lost the way of thinking out of the box.

Then one day she realized that she needs some changes in her life. She immediately called General Steven Rockwell Trevor, who was her boyfriend and a software engineer in another parallel DC universe. She was crying for her recent life. Then General Steven Rockwell Trevor gave her a wonderful solution. He told her to learn web automation using selenium with python.

Then after learning python and selenium, she started to automate her every day’s boring tasks. And guess what she is doing all day now? Dating General Steven Rockwell Trevor and enjoying all the river views, green fields. Enjoying Starbucks coffee. Netflix movies. Developers of her company now pushing new features every week and she is writing scripts for automated tests for 2/3 days and enjoying the rest of the week and weekend by doing awesome things.

Well, General Steven Rockwell Trevor got fired for dating all day and for not completing his tasks. but that’s okay. Web automation wins!

Awesome, What do I need to do that?

  • Web automation tool. We will use Selenium in this series.
  • Web driver for chrome
  • A programming language. We will use Python3. So a basic knowledge of python3 is required.
  • Linux OS, as we will use Linux.
  • And last but not least, You must have pip and venv installed in your system

Setting up the virtual environment:

New need a web driver first. And its chromium web driver. It’s a separate executable that the Selenium web driver uses to control Chrom. Let’s install a chromium web driver first by running the below command.

sudo apt-get install chromium-chromedriver

Let’s set up our virtual environment first. Run the below command in your desired directory.

python3 -m venv web_automation

Wait for a minute. Why do we need a virtual environment for python projects? Because as the documentation says,

“Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict, and installing either version 1.0 or 2.0 will leave one application unable to run. The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.”

Reference: https://docs.python.org/3/tutorial/venv.html

 

Now let’s activate the virtual environment by running bellow command

. web_automation/bin/activate

This will activate the virtual environment by executing activate shell script which is inside the bin folder of our virtual environment.

You will find a new directory called web_automation now. This is our virtual environment. Now let’s go into the directory.

cd web_automation

Now install python selenium package using pip. Why we will use Selenium? Because it’s the best for this job.

pip install selenium

Let’s create and move into our project directory inside the virtual environment.

mkdir selenium_web_automation && cd selenium_web_automation

Nice, you have successfully set up all the environments and packages required to start your web automation journey. But after doing all this shouldn’t we see some results if everything will work or not?
Let’s create a python file called automation.py inside selenium_web_automation folder. And write these codes bellow in it.

 

I really want you to type codes by hand. Try not to copy-paste in the beginning. After typing run this code get amazed by the power of the selenium web automation tool. We will explore it a lot soon. Stay tuned. See you in the next episode.

References:
https://www.selenium.dev/
https://docs.python.org/3/tutorial/venv.html