Let's talk testing with Selenium

Created by Anisha Narang | Red Hat Inc.

PyCon India 2013, Bangalore

Why Testing?

Test Automation

... is use of testing tool to:

  • To control the execution of tests
  • To compare the actual outcomes to expected outcomes
  • To report the test status(PASS/FAIL)


  • Selenium automates web browsers
  • Can be controlled by many programming languages and testing frameworks
Programming language :   python
Testing framework    :   unittest


Test Automation with Selenium

  • Regression testing
  • Validate the application flow
  • Test suites
  • Report generation

How to start?

  • Install Selenium

    pip install selenium

  • Record and Play with Selenium IDE

Selenium script looks like?

#!/usr/bin/env python
from selenium import webdriver
import unittest
Class Test(unittest.TestCase):
	def setUp(self):
		self.driver = webdriver.Firefox()
	def test_case(self):
		#write your test case here 
	def tearDown(self):
if __name__ == "__main__":


Building an

  1. Understand the functionality
  2. Write test plan and test cases
  3. Decide which test cases to automate
  4. Inspect the UI elements accordingly
  5. Start writing your test script

To automateWhat?

Some sample code...


{"name": "echo","id": 1},{"name": "print","id": 2},{"name": "hello world","id": 3}   

def  test_search():  
  with open(“test_data.json”) as data_file: 
    data_text = json.load(data_file) 
      for each in data_text: 
        search_text = each["name"] 
        var = driver.find_element_by_id("search_box")
        element = driver.find_elements_by_class_name('hl') 
        element_text = {z.text.lower() for z in element}
        self.assertIn(search_text.lower(), element_text, "Search result does not match")

Checkbox selection

def test_checkbox():
    checkbox = driver.find_element_by_id("id_1")
    self.assertTrue(checkbox.is_selected(),"Incorrect filter selection")
    self.assertTrue(checkbox.is_enabled(),"Checkbox is disabled")

  except NoSuchElementException:
    logging.info("No element present for checkbox")


def test_pagination():
  regexp= r"[A-Za-z0-9]+"
  page_numbers = re.findall(regexp,element.text)
  prev_element = driver.find_element_by_class_name('disabled')
  prev_element = prev_element.text
  self.assertEqual(prev_element, page_numbers[0], "'Prev' NOT disabled")
  page_1 = driver.find_element_by_class_name('active')
  page_1 = page_1.text
  self.assertEqual(page_1, page_numbers[1], "'1' NOT active")
  next_element = driver.find_element_by_link_text("Next")
  #similarly verify the last and next element


How to maintain your scripts?

Test reports

Generate reports using HTMLTestRunner.py

Demo time

Code available at: https://github.com/anarang/fusion

Best practices

Thank You

@anisha_narang | anarang@redhat.com

Slides available at: http://pyselenium-anisha.rhcloud.com