How to install Selenium on Linux and automate your web tests

Selenium is a web testing toolkit that allows you to test a web application in any browser of your choice. It does this with the help of a software called WebDriver which allows you to emulate a web browser and test your web application in it.

In this post I am going to show you how to test a simple CRUD application, by automating link clicking and form filling.

Software versions
Selenium (2.44.0)
Python 3.4.2
Linux kernel 3.13.0-37-generic and Mint 17.1 for Desktop.

Installing Selenium on Linux
To install the Selenium Bindings for Python on Linux use pip.

[leo@selenium-on-linux]$ virtualenv -p /usr/bin/python3 venv-3.4
[leo@selenium-on-linux]$ source venv-3.4/bin/activate
[leo@selenium-on-linux]$ pip3 install selenium

The webdriver for Firefox is installed along with Selenium but if you want to test your applications on chrome then you need to download the chrome driver and ensure chrome is installed on your Linux distro.

Getting started with the code.
The first thing you need to do is create an instance of the webdriver.

[leo@selenium-on-linux]$gedit selenium-tests.py
from selenium import webdriver

driver=webdriver.Chrome('/home/leo/Downloads/chromedriver')

Next you need to specify the url of the web application you need to test. I will be using a custom CMS application which is used to create, read, update and delete posts for this demonstration. Below is the screenshot of the app.

Install Selenium on Linux

driver.get("http://url-for-posts.com")
assert "Post Page Title" in driver.title

The above code will fetch the web page and check if the web page has been successfully fetched by matching the content in the page title.

Now let’s test the “Create”, i.e add a post.

  link=driver.find_element_by_link_text("Add new")
  NewWindow=link.click()
  assert "Save" in driver.page_source

In the code above I use “find_element_by_link_text()” to perform actions on the anchor HTML element which contains the text “Add new”. You can get elements with their ID or name or even by class name. You can click on elements with the “click()” method.

On clicking the “Add new” link you get the page to create a post.

Install Selenium on Linux

Here we need to fill in the title, body, category, select whether the post will be published and then click on the “Save” button to add the post.

So let’s fill in the form with some text

#Add posts
        title=driver.find_element_by_name("title")
        title.send_keys("Your Title text here")

        content=driver.find_element_by_name("content")
        content.send_keys("Your Body text here")

        category=driver.find_element_by_name("category")
        category.send_keys("Your category text here")

Below is the html of the form elements.

<input type="text" name="title" required/>
<input type="text" name="content" required/>
<input type="text" name="category" required/>
<select name="published">
   <option value="No">No</option>
   <option value="Yes" selected="">Yes</option>                            
    </select>
<button class="button " type="submit">Save</button>

As I mention earlier you can fetch an element by it’s name with “find_element_by_name(“element name”)”. You can then perform an action on it like fill it with text using “send_keys()”.

For the “select” element we will need to import the “Select” class.

from selenium.webdriver.support.ui import Select

 select = Select(driver.find_element_by_name('published'))
 published=select.select_by_value("Your option value")

Now we need to click on Save, however the “Save” button does not have a name attribute hence we need to use the class name.

 button=driver.find_element_by_class_name('button')
 success=button.click()
 assert "Add was successful" in driver.page_source

Similarly the code for update is

 #Edit Posts
        link=driver.find_element_by_link_text("Edit")
        newindow=link.click()
        assert "Save" in driver.page_source

        title=driver.find_element_by_name("title")
        title.send_keys("Selenium web test edit")
        content=driver.find_element_by_name("content")
        content.send_keys("Selenium web test edit")
        category=driver.find_element_by_name("category")
        category.send_keys("Selenium web test edit")

        select = Select(driver.find_element_by_name('published'))
        published=select.select_by_value("0")

        button=driver.find_element_by_class_name('button')
        success=button.click()
        assert "Update was successful" in driver.page_source

And the code for delete is

 #Delete Posts
 link=driver.find_element_by_link_text("Delete")
 NewWindow=link.click()
 assert "Post was deleted successfully" in driver.page_source

The complete source code is available at https://github.com/Leo-g/Selenium/blob/master/python-selenium.py

You can view the code in action in the video below.

Comments and feedback is welcome.

Ref (http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-to-write-tests)

How to start building websites on Linux with Django

How to start building websites on Linux with DjangoDjango is a web framework for python. It provides you with the basic components that are required to build your website like HTML templates, User Authentication, Web Server etc. In this tutorial I will describe how you can install Django 1.7 on Linux with the latest version of Python and PostgreSQl. Software Versions Linux(CentOS 6.5)... Read More »

How to build CRUD app with Python, Flask, SQLAlchemy and MySQL

In this post I will briefly describe, how you can you build a database driven CRUD (Create, Read, Update, Delete) app on Linux with Python, Flask, SQLAlchemy and MySQL. I used this process to create a blog and hence the examples below will describe how to store and modify posts in a MySQL database. Software... Read More »

Managing Linux server configs with the SaltStack

Managing Linux server configs with the SaltStackI came across Salt while searching for an alternative to Puppet. I like puppet, but I am falling in love with Salt :). This maybe a personal opinion but I found Salt easier to configure and get started with as compared to Puppet. Another reason I like Salt is that it let’s you manage your... Read More »

Creating your first Linux App with Python 3 and Flask

Creating your first  Linux  App with  Python 3 and FlaskWhether playing on Linux or working on Linux there is a good chance you have come across a program written in python. Back in college I wish they thought us Python instead of Java, it’s fun to learn and useful in building practical applications like the yum package manager. In this tutorial I will take... Read More »

How to install an Opensource VPN Server on Linux

How to install an Opensource VPN  Server on LinuxOne of the most concerning thoughts I have while browsing, Is how can I ensure that my data remains private and secure ? In my search for answers, I came cross a number of ways in which you can remain anonymous like using a proxy website. But still using a third party service was not... Read More »

Using Foreman, an Opensource Frontend for Puppet

Using Foreman, an Opensource Frontend for PuppetThe recent vulnerability in bash, got me running to update bash. It’s easy when you have maybe one or two Linux servers, but what do you do if you have 100’s or even thousands or servers? You need to use a server configuration and management tool like puppet. However, instead of using the command line,... Read More »

Using GIT to backup your website files on linux

Using GIT to backup your website files on linuxWell not exactly Git but a software based on Git known as BUP. I generally use rsync to backup my files and that has worked fine so far. The only problem or drawback is that you cannot restore your files to a particular point in time. Hence, I started looking for an alternative and found... Read More »

How to install Ruby, Ruby on Rails and Phusion Passenger on CentOS

Lately, I have been writing apps in ruby instead of PHP, as it has a low learning curve especially if you know bash scripting and an awesome framework called Rails. Ruby on Rails was so easy to work with that I got my app up and running in 48 hours. You can check it out... Read More »

How to configure and secure your Postfix email server

How to configure and secure your Postfix email serverOnce you have your application server up and running, you are going to need a good email server to deliver your emails. I have been using postfix for all my servers and below is the configuration I generally use. Installation of Postfix on CentOS 6 yum install postfix Sendmail is installed by default, so it... Read More »