Test Automation overview

The use of special software to control the execution of tests and compare the expected results with the outcome provided by the tests, is called test automation.
With automated tests we can automate repeated actions which are time-consuming when performed manually, and ensure the functionality of our code. Test automation is critical for software development teams which produce software in short cycles in order to ensure that the software can be reliably released.

What are the advantages that automated tests offer?

This is a valid question that some may think of. There are several reasons why, most of the time, automated tests should be used. First of all, automated tests are less time-consuming; once the tests are written, the automated tests are performed much faster than the time it takes to perform the corresponding manual tests. This is a significant time-saving that translates directly into cost savings. Moreover, automated tests are more reliable; once a test is written and added to the test suite, it will be executed each time that the suite runs and will not be forgotten. On the other hand, a manual tester may forget to perform some tests or intentionally skip them considering that the updated code hasn’t affected those tests. Finally, automated tests relieve software testers from highly repetitive, low-value test work. However, we should mention that not all tests can be automated and that automated tests can’t fully replace software testers.

How we test our site using automated tests

Codebender uses Selenium tests for testing the functionality of the codebender website. The tests are written in Python 2, utilizing pytest as a testing framework and Selenium for browser automation.

What is Selenium tool?

Selenium is an open source project that contains a variety of tools and libraries enabling web browser automation for testing web applications more efficiently. One of the most important advantages that Selenium has is the capability to operate across different browsers and operating systems.

There are four tools available on Selenium suite:

  • Selenium Integrated Development Environment

    This is a Firefox plugin that lets testers record their actions as they follow the workflow that they need to test.

  • Selenium Remote Control

    Selenium RC was the main Selenium framework, before the Selenium WebDriver tool. It is a testing framework that allows more than simple browser actions and makes use of the full power of programming languages (such as Java, C#, PHP, Python, Ruby, and PERL) to create more complex tests.

  • Selenium WebDriver

    It is the merge of Selenium Remote Control with Webdriver, that resulted in Selenium WebDriver. This is the successor to Selenium RC which sends commands directly to the browser and retrieves results.

  • Selenium Grid

    This is a tool used to run parallel tests across different machines and different browsers simultaneously which results in minimized execution time.

Selenium tests suite that is used to test codebender, uses Selenium Webdriver tool.

Test Scenario: Validate the login page

We will try to test our login page using a simple code that will perform the following actions:

  • Launch the browser and open codebender.cc site.
  • Click on the Log in which opens the login form.
  • Enter the username and password.
  • Click on the Login button.
  • Verify that you are in your user’s homepage.
  • Log out.
  • Close the browser.

Prerequisites

In order to be able to run Selenium tests written in Python, you need to install Python, pip and Selenium programs.

Configuration on Linux

Most Linux distributions include Python as a standard part of the system, so you will only need to add pip and Selenium. Python-pip is a tool for installing and managing Python packages and can be installed by executing the following command:
sudo apt-get install python-pip
Once the pip tool is installed execute the following command to install Selenium:
pip install selenium
You are ready to move on!

Configuration on Mac OS X

Mac OS X distributions have installed by default Python and pip, so you will only need to add Selenium. Open a terminal and run the following command to install Selenium:
pip install selenium
That’s all!

Configuration on Windows

When on Windows OS, first you should download and install Python (for our example we will use Python2.7). As soon as you install Python, add it to home path. Download get-pip.py, being careful to save it as a .py file. Then, open a command prompt as Administrator and run:
python get-pip.py
When the pip tool’s installation finishes, add pip.exe to home path and run: pip install -U selenium or navigate to the folder where pip.exe is installed, open a command prompt in there and run pip install -U selenium
All tools needed are now installed and you can start writing your test code.

Simple Selenium code that tests login in codebender site

Now let’s get to the most interesting part; the implementation of a code that will check the login process in the codebender site. In our example we will use Python language, which is the one used in all codebender’s Selenium tests suite.

Here’s how the code will look:

login.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://staging.codebender.cc/")

login_button = driver.find_element_by_id('login_btn')
login_button.send_keys(Keys.ENTER)
user_field = driver.find_element_by_id('username')
user_field.send_keys('username')
pass_field = driver.find_element_by_id('password')
pass_field.send_keys('password')
do_login = driver.find_element_by_id('_submit')
do_login.send_keys(Keys.ENTER)
verify_login = driver.find_element_by_css_selector('.navbar-text')
assert 'Logged in as' in verify_login.text
do_logout = driver.find_element_by_id('logout')
do_logout.send_keys(Keys.ENTER)

driver.close()
print ("Test success")

Code Walkthrough

Import Statements:

from selenium import webdriver : The selenium.webdriver module provides all the WebDriver implementations required to instantiate a new web browser.
from selenium.webdriver.common.keys import Keys : The Keys class provide keys in the keyboard like RETURN, F1, ALT etc.

Object Instantiation:

driver = webdriver.Firefox() : The instance of Firefox WebDriver is created.

Launching the Web browser:

driver.get("https://staging.codebender.cc/") : A fresh webdriver instance is launched and is redirected to the application URL.

find_element_by_* methods:

login_button = driver.find_element_by_id('login_btn') : WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. For example, here the element is located by its id.
verify_login = driver.find_element_by_css_selector('.navbar-text') : In this case the element is located by a css selector, it’s class.

send_keys command:

login_button.send_keys(Keys.ENTER) : The send_keys comand is used to enter/type the specified value (within the parentheses ) in the textbox.

assert statement:

assert 'Logged in as' in verify_login.text : The next line is an assertion to confirm that element verify_login contains ‘Logged in as’ text in it.

Close the Web Browser:

driver.close() : The close() is used to close the current browser window.

Print statement:

print ("Test success") : This prints “Test success” on the terminal from which the test was run.

Test execution

On Linux and Mac OS

Open a terminal, navigate to the directory where your test code is located and run:
python login.py
As you can see in the example above we used Firefox as the selected test browser. If you want to run selenium using Chrome browser you need to download chromedriver, unzip it and copy the chromedriver to /usr/bin folder. Then update your code as shown below:

# driver = webdriver.Firefox()
driver = webdriver.Chrome()

On Windows OS

Open a command prompt, navigate to the directory where your test code is located and run:
python login.py
Again, if you wish to run Selenium using Chrome browser you need to download chromedriver and unzip it. Then update your code as shown below:

# driver = webdriver.Firefox()
driver = webdriver.Chrome('C:\path\to\chromedriver')

Conclusion

In this blogpost we have just explained the basics of a simple Selenium automated test. There is much more that someone can explore and experiment with. Thanks for stopping by and reading!

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required