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
6. How to start?
1. Install Selenium
pip install -U selenium
2. Record and Play with Selenium IDE
7.
8. 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("www.example.com")
def test_case(self):
#write your test case here
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
9. 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
11. 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
12. 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")
13. 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
14. 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
15. Test reports
How important is reporting?
Can we generate HTML reports?
How to generate reports for automated tests?
Generate reports using HTMLTestRunner.py
18. 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