Browsing Tag

Linux

Selenium Webdrive in Python Tutorial

Tutorial: Getting Started with Selenium in Python

The best things in life are free, and for Software Testers this is particular true thanks to Selenium. Being a portable testing framework for web applications, Selenium allows us to interact with web elements either without coding skills at all (Recording Tool) or with an easy to learn test domain-specific language (Webdriver). It is perfect for automating the boring stuff!

I’ll show how easy is to get started in Automation Testing using basically only Selenium and Python.

This tutorial assumes that the user is running a similar working environment with the following software:

  • Debian based Linux distribution, like Ubuntu
  • Python 2.7.x
  • Python Pip
  • Firefox 54

Don’t get discouraged if you have an operative system different than Linux. Python and other tools can be installed in other Operative Systems. One of the greatest advantages of these type of software is that they are multi-platform!

Creating an isolated Python environment with Virtualenv

Virtualenv is a tool that allows to create an environment that has it’s own isolated installation of Python and libraries. This is useful if we want to avoid having compatibility issues between projects that require different version of a particular library or simply because we don’t want to mess our global installation.

To install Virtualenv, open a new terminal and type the following command:

pip install virtualenv

Then we create a new folder for your Virtual Environment:

virtualenv selenium

To start using your new virtual environment enter (within your project folder):

source bin/activate

To exit from the your virtual environment, simply type:

deactivate

Installing Selenium with pip

Once our Virtual Environment is up and running, we can proceed to install Selenium via pip:

pip install selenium

Grabbing Geckodriver

In order to run our tests in Firefox, first we must install the Geckodriver. To check the latest version available visit:

https://github.com/mozilla/geckodriver/releases

Download the version corresponding to your Operative System and Architecture using wget, example:

wget https://github.com/mozilla/geckodriver/releases/download/v0.17.0/geckodriver-v0.17.0-linux64.tar.gz

Then we uncompress the file:

tar -xvzf geckodriver*

Also we make the file executable:

chmod +x geckodriver

Finally we move the Geckodriver to /usr/local/bin/ so it is accesible for Selenium to use:

sudo mv geckodriver /usr/local/bin/

Running a quick test

Now we are ready to test our environment, let’s write a small script that opens a new browser and navigates to Google:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://google.com')

Feel free to use your favorite code editor, since is a simple script you can even use nano. Once you’re done writing your script, save your changes and go back to Terminal and run your script:

python demo.py

If all went well a new firefox window should open and the browser should display google’s homepage. Congratulations! You wrote your first Selenium Script! Stay tune for more tutorials.