15

Aug

2014

Getting Started with UI Automated tests using (Selenium + Python)

By: August 15, 2014

Introduction

What is Selenium?

Selenium does browser automation. Basically, Selenium tells a browser to click some element, populate and submit a form, navigate to a page or do any other form of user interaction.

Key features include:

  • Multiple Browser Support. Firefox, Chrome, Safari, Opera and yes, even Internet Explorer.
  • Multiple OS Support. Windows, OS X, Linux, Android and iOS
  • Multiple Language Support. Python, Ruby, Java, etc.
  • Free. As in beer, and freedom
  • For more features see: seleniumhq

Getting Started

Requirements:

Python

Selenium supports multiple languages but in this tutorial we are going to use Python (and specifically python 2.7). Most of the time Python is already shipped with your computer but just in case it is not, you can download it and install it from their website.

Bonus:

  • On OSX you can easily install Python using Homebrew.
  • On Windows you can use Ninite to install Python the easy way.

Pip

pip is a package management system used to install and manage software packages written in Python. To install pip follow this guide.

Virtualenv

Virtualenv provides an isolated working copy of Python which allows you to work on a specific project without worrying about affecting other projects. To install it follow this guide or simply do:

Other Python Packages:

There are other Python packaged that you'll need to installed.

  • selenium
  • nose

To install these package run this code on the CLI:

Web Browser:

For this tutorial you need to have Firefox installed on your machine. I am using Firefox 28 for this tutorial.

My Setup:

You can use any OS, programming language and browsers. Here is what I use:

  • OS: Mac OS X Mavericks
  • Python: python 2.7
  • Browser: Firefox 28
  • Selenium: Selenium 2.40

Writing the Test

An automated test is basically the same thing you'd do via manual testing - but it is done by the computer so it allows you to go do other things, as well as easily repeat your tests. The test code is going to run a series of instructions to interact with a web browser - mimicking how an actual end user would interact with an application. The script is going to navigate the browser, click a button, enter some text input, click a radio button, select a drop down, drag and drop, etc. In short, the code will test the functionality of the web application.

First Example

Make a Virtual Environment

To have an isolated environment, use virtualenv.

Write a simple test

For this test we will open the AWeber website at http://www.aweber.com using Firefox, and make sure that the title of the page is "AWeber Email Marketing Services & Software Solutions for Small Business".

Run the test

Clicking the element

For our next test, we will click the Order tab and check all available pricing plans.

First, find the order tab on from AWeber.com website and click it:

Next, find all elements of pricing. These elements will be placed in an array of elements.

To verify the content of these elements we need to get the text of each element.

Lastly, verify each element.

Here's the complete test function:

Verifying Element

Next we will verify that the default selected pricing is Monthly. For that we use the  is_selected() method.

Filling Text Input on a Form

Another useful function of Selenium is to fill out a form. In the following example we will search AWeber.com and verify that we get some results.

Full code


Fin

That's all folks!

  • http://www.belau.nl David Benjamin

    How does iis it work?

  • http://webdata-scraping.com/ Sawan Sanghvii

    NIce article.

  • white ninjitsu

    Thanks for sharing this article.

  • Errolfast

    Hello im trying to use selenium webdriver with python 3.5, but im trying to use textmate as an editor so i can run the code from there and I don't know how to 'link' webdriver to textmate

  • Thipawan Pairam

    in last line,I can't run it. cls.driver.quit()

  • Rich Kaye

    Can you clarify your question? I think you're saying that you are having trouble getting your python code to run b/c your 'textmate' environment can't find the selenium module? Is that the issue here?

    What exception are you seeing when you try to run your code from within textmate?