Let's talk testing with Selenium –



Let's talk testing with Selenium –

1 2


pyselenium

Presentation slides for talk at PyCon 2013, Bangalore

On Github anarang / pyselenium

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

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

http://selenium-python.readthedocs.org

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()
		self.driver.get("www.example.com")
	
	def test_case(self):
		#write your test case here 
	
	def tearDown(self):
		self.driver.quit()
        	
if __name__ == "__main__":
    unittest.main()

Building an

Understand the functionality Write test plan and test cases Decide which test cases to automate Inspect the UI elements accordingly Start writing your test script

To automateWhat?

Some sample code...

Search

#test_data.json
[
{"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")
        var.clear()
        var.send_keys(search_text) 
        driver.find_element_by_name("search").click() 
        element = driver.find_elements_by_class_name('hl') 
        element_text = {z.text.lower() for z in element}
        for e in element_text:
          self.assertEqual(search_text.lower(), e, "Search result does not match")
             		

Checkbox selection

def test_checkbox():
  try:
    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")
					

Pagination

def test_pagination():
  element=driver.find_element_by_class_name('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")
  #similarly verify the 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

http://goo.gl/UbAOW2