Your SlideShare is downloading. ×

Let's talk testing with Selenium


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Let's talk testing with SeleniumCreated by Anisha Narang | Red Hat Inc. PyCon India 2013, Bangalore
  • 2. Why Testing?
  • 3. 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)
  • 4. Selenium Selenium automates web browsers Can be controlled by many programming languages and testing frameworks Programming language : python Testing framework : unittest
  • 5. Test Automation with Selenium Regression testing Validate the application flow Test suites Report generation
  • 6. How to start? 1. Install Selenium pip install -U selenium 2. Record and Play with Selenium IDE
  • 7. Selenium script looks like? #!/usr/bin/env python from selenium import webdriver import unittest Class Test: def setUp(self): self.driver = webdriver.Firefox() self.driver.get("") def test_case(self): #write your test case here def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
  • 8. Building an 1. Understand the functionality 2. Make a manual test run 3. Decide which test cases to automate 4. Inspect the UI elements accordingly 5. Set the expectations 6. Design a suitable logic for the test code 7. Start writing your test script
  • 9. To automateWhat?
  • 10. Some sample code snippets Search def test_search: with open(“file.json”) as data_file: data_text = json.load(data_file) for each in data_text: search_text = each["name"] driver.find_element_by_id("_id1").clear() driver.find_element_by_id("_id1").send_keys(search_text) driver.find_element_by_name("search_id").click() element = driver.find_elements_by_class_name('results_id') element_text = {z.text.lower() for z in element} self.assertIn(search_text.lower(), element_text, “Search result does n
  • 11. Checkbox selection def test_checkbox: checkbox = driver.find_element_by_id(“id_1”) if checkbox: self.assertTrue(checkbox.is_selected(),"Incorrect filter selection” self.assertTrue(checkbox.is_enabled(),"Checkbox is disabled")
  • 12. 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], "The element 'Prev' is NOT page_1 = driver.find_element_by_class_name('active') page_1 = page_1.text self.assertEqual(page_1, page_numbers[1], "The element '1' is NOT active on next_element = driver.find_element_by_link_text("Next") #similarly verify the last and next element
  • 13. Maintaining test scripts Structure your test project Decide the format for input test data Keep the code DRY Decide which test cases to cover in one test script depending on the flow of the application Use logging module to keep track of the events taking place during the test run
  • 14. Test reports How important is reporting? Can we generate HTML reports? How to generate reports for automated tests? Generate reports using
  • 15. Demo time
  • 16. Best practices Decide which test cases to automate Create good quality test data Design stable logic for test scripts Create automated tests that are resistant to changes in the UI Generate test reports
  • 17. END|