Web Automation: Selenium WebDriver and Python — Getting Started — Part 1

Asheesh Misra
9 min readJul 12, 2019

--

Photo by Pixabay from Pexels

Introduction

Python has gained a lot of traction in last decade and is getting very popular among testers also for automation testing, web scraping, security testing and automating other mundane tasks. In this multi part tutorial we will try to write automation scripts in Python using Selenium Webdriver.

For interested readers here are Part 2 and Part 3.

What the reader should already know

It is assumed that the reader knows basics of Python and has some exposure to Selenium Webdriver in Java/ C# or any other language binding. This is by no means a beginner’s tutorial for both automation and python.

Agenda

Ok, now that we are clear on the pre — requisites, let’s create the outlines of this tutorial. This would be a multi part tutorial, in which we will write automation script for search and add to cart/ delete from cart functionality of amazon. We will then refactor our script as we evolve in our learning also. Final product that we are aiming here is automating search feature in Amazon using POM (Page Object Model design pattern). Precisely, following cases are being aimed at, for the purpose of automation.

Now that we have our manual testcases ready, let us start with setting up Python and related tools to start writing automation scripts for these testcases.

Initial Setup

Install Python and Pip

We will be installing Python and other tools on Windows 10. The Python 3 installer for Windows can be downloaded from here. Installation should be pretty straightforward. A decent how to guide on installing Python 3 on Windows 10 is here. It is for 32-bit installer but the instructions stay the same of 64-bit installer as well.

“Don’t forget to select the checkbox to add Python to PATH.”

As per Wikipedia,

pip is a package-management system used to install and manage software packages written in Python. Many packages can be found in the default source for packages and their dependencies — Python Package Index (PyPI). pip is a recursive acronym for ‘Pip Installs Packages’ ”.

‘pip’ is what ‘npm’ is to node and ‘gem’ is to ‘ruby’. It lets us install the packages, dependencies, libraries we need for our purpose. Python installers installs it, by default. To confirm whether pip is installed on our system or not, we can run the following command:

pip — help command

We will use ‘pip’ in a moment (and later also) but first let’s create environment for our project.

Create Virtual Environments

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them (by geeks for geeks).

An excellent tutorial on creating virtual environment is here and it is strongly recommended to go through it. To quickly do it however, we will execute following commands in succession on the command prompt

> pip install virtualenv> pip install virtualenvwrapper-win

Now, using mkvirtualenv <environment-name> command we will create a virtual environment, then create project directory and then finally bind the environment to the project directory.

We can see that we are in our ‘amazon’ virtual environment currently. As long as we see amazon in parentheses at the command prompt, we are in our virtual environment. Any package or library that we install remain specific to our virtual environment.

We can deactivate the environment by typing in command deactivate or exiting the command prompt. However, if we want to work more on our project using this environment, we simply need to type workon <environment name>.

Installing Selenium

Before installing Selenium, let’s first visit Python Package Index website and find out the version of selenium package, available and also the related documentation.

So to install selenium, let us type in the following pip command, at command prompt (in our environment):

> pip install selenium

To confirm if the selenium package was installed correctly or not, we can do the following:

Observe that we were able to import selenium successfully. Another way(s) of checking whether Selenium package got installed successfully on our system (more importantly, our virtual environment), are

> pip check selenium> pip show selenium

pip check selenium checks for any broken requirements which might there due corrupt installation of Selenium.

pip show selenium displays the details of the selenium package installed in our environment. Notice that pip automatically installed selenium’s dependency urllib while installing selenium.

Now, let’s install VS Code to complete our setup.

Setup VS Code

VS Code is an excellent code editor from Microsoft. It can downloaded from here. The installation wizard is pretty straightforward. After installing VS Code, let’s open it. This can be done either from the ‘Start’ menu or from the command prompt. To open VS Code from command prompt, simply type code . .

After VS Code loads up, next thing that we will do is install python extensions for VS Code. To do so, we can either navigate here, click on ‘Install’ button and open let the link be opened in VS Code OR within VS Code open the ‘Extensions’ tab, search for ‘python’, click on the Microsoft’s extension and then click on ‘Install’ on the extension’s details page.

Next, we will install ChromeDriver.

Installing ChromeDriver

On the downloads page of Selenium’s official website, scroll down to the ‘Third Party Drivers, Bindings, and Plugins’ section and click on ‘Google Chrome Driver’ hyperlink. This will navigate us to the official ‘ChromeDriver — WebDriver for Chrome’ page. Here, we can download the chromedriver as per Chrome Web Browser’s version on our system. We will check our Chrome browser’s version before downloading ChromeDriver for it.

So, we will download ChromeDriver with the version 75.x.xxx.xx, on the Chromedriver’s site. Upon clicking on the relevant version link, we would be navigated to the following page from where we will download ‘chromedriver win32.zip’ file.

We will create a new folder in our project’s folder with the name ‘drivers’ and unzip the chromedriver’s zip file over there. This would finish our chromedriver’s installation. Now, we will write test for our first Testcase.

Let’s Automate

We will start writing script for the first testcase, in VSCode, now. Then we will run it. As we progress and create scripts for other testcases, we will continuously refactor our code to make it more clean, resilient, DRY (Don’t Repeat Yourself) and less fragile.

First Script

As mentioned above, our first testcase was simply ‘loading Amazon’s Home Page’. To do so, let’s move to VSCode, create a folder ‘tests’ within our project and then create a new file with the name AMZN_Search_TC_001.py. Now, key in the following code into this python file.

AMZN_Search_TC_001.py

Let’s try to understand the script above line by line.

  • First, we import webdriver from selenium because selenium.webdriver module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Chrome, IE and Remote.
  • Second, we create a variable to store url we want to visit.
  • Third, we provide our instance of webdriver, the path where chromedriver executable is present. Our webdriver instance will be using that executable to run the browser.
  • Fourth, we maximized the window size.
  • Fifth, we added an implicit wait to the script so that script does not error out immediately upon not finding an element we are looking for on the page being loaded, when the net speed is on a bit slower side (more on implicitly_wait() later).
  • Sixth, we navigate to the URL. WebDriver will wait until the page has fully loaded before returning the control back to our script.
  • Seventh, we assert that we have indeed loaded the correct URL by checking that the title of the webpage our script has loaded, contains ‘Amazon’ in it.
  • Eighth, we close the browser.

To run this script, either navigate to the project folder on command prompt (remember it is still open with our environment activated) OR open a terminal within VSCode by pressing shortcut key Ctrl + Shift + ` (please notice that ` is the ‘tick’ symbol on the key just below the Esc key on qwerty keyboard).

At the command terminal run the script using python command, like so

As it is evident from the command prompt screen above that our script ran flawlessly. That’s good. Now, to the second script.

Second Script

In the second test case, we will be ‘searching for a product’. So, let us again move to VSCode, create a new python file under ‘tests’ folder with the name ‘AMZN_Search_TC_002.py’. In this testcase, the pre-conditions would be the first testcase, therefore, most of the code written in first script will be part of this second script as well.

AMZN_Search_TC_002.py

Let us try to understand what does the script above do.

In the starting of the script, apart from importing webdriver module, we import Keys class from selenium.webdriver.common.keys. The Keys class provide keys in the keyboard like RETURN, F1, ALT etc.

Then we declare a variable to store search term and then use the variable throughout the script. This would come in handy when we change the search term and also when we later on refactor this script into a function that accepts a string as search term.

Next, we specify chromedriver executable’s path, then perform some basic setup functions and load the URL in browser window. After asserting that the Amazon’s home page has indeed loaded, we are clearing the search textbox, to be on the safer side.

Next, we are sending keys, which is similar to entering keys using keyboard. Having sent the search term to search textbox, we again use the send_keys() method of webdriver to send a special builtin key RETURN which is equivalent to pressing the ‘Enter’ key on keyboard.

We then conclude with performing assertions that we are indeed on the Search Results page and that the search term HAS returned some search results and finally.

Food for Thought

We have written automation test scripts for two of our manual testcases and have also executed them successfully.

However, there are few questions/ concerns to deal with. First, we are executing one test script at a time. While we can execute all our test scripts as a batch file (sample script below), another concern is that we are repeating a lot of code. We need to stick to DRY as we said when we started writing first automation script, above.

We will try to address these concerns in the the next part of our tutorial by introducing builtin python module unittestand POM (Page Object Model) in to our scripts.

Until then, keep learning, keep testing. Best wishes! 😇

And… if you have any suggestions on this tutorial or for me in general, please do leave in comments. I will surely try to improve 🙏.

Link to Part 2 and Part 3.

--

--

Asheesh Misra

ISTQB Certified Tester @Metacube, Jaipur| does Automation, Performance, API Testing| listens spiritual discourses and good music| continuous learner