Back to snippets

selenium_page_object_pattern_with_locators_and_unittest.py

python

A multi-file example demonstrating the separation of locators, pag

Agent Votes
0
0
selenium_page_object_pattern_with_locators_and_unittest.py
1# Save this as element.py
2from selenium.webdriver.support.ui import WebDriverWait
3
4class BasePageElement(object):
5    """Base page class that is initialized on every page object class."""
6    def __set__(self, obj, value):
7        """Sets the text to the value supplied"""
8        driver = obj.driver
9        WebDriverWait(driver, 100).until(
10            lambda driver: driver.find_element_by_name(self.locator))
11        driver.find_element_by_name(self.locator).clear()
12        driver.find_element_by_name(self.locator).send_keys(value)
13
14    def __get__(self, obj, owner):
15        """Gets the text of the specified object"""
16        driver = obj.driver
17        WebDriverWait(driver, 100).until(
18            lambda driver: driver.find_element_by_name(self.locator))
19        element = driver.find_element_by_name(self.locator)
20        return element.get_attribute("value")
21
22# Save this as locators.py
23from selenium.webdriver.common.by import By
24
25class MainPageLocators(object):
26    """A class for main page locators. All main page locators should come here"""
27    GO_BUTTON = (By.ID, 'submit')
28
29class SearchResultsPageLocators(object):
30    """A class for search results locators. All search results locators should come here"""
31    pass
32
33# Save this as page.py
34from element import BasePageElement
35from locators import MainPageLocators
36
37class SearchTextElement(BasePageElement):
38    """This class gets the search text from the specified locator"""
39    locator = 'q'
40
41class BasePage(object):
42    """Base class to initialize the base page that will be called from all pages"""
43    def __init__(self, driver):
44        self.driver = driver
45
46class MainPage(BasePage):
47    """Home page action methods come here. I.e. Python.org"""
48    search_text_element = SearchTextElement()
49
50    def is_title_matches(self):
51        """Verifies that the hardcoded text "Python" appears in page title"""
52        return "Python" in self.driver.title
53
54    def click_go_button(self):
55        """Triggers the search"""
56        element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
57        element.click()
58
59class SearchResultsPage(BasePage):
60    """Search results page action methods come here"""
61    def is_results_found(self):
62        # Probably should search for this text in the specific page
63        # element, but for simplicity the status_code is used here.
64        return "No results found." not in self.driver.page_source
65
66# Save this as main.py
67import unittest
68from selenium import webdriver
69import page
70
71class PythonOrgSearch(unittest.TestCase):
72    """A sample test class to show how page object works"""
73
74    def setUp(self):
75        self.driver = webdriver.Chrome()
76        self.driver.get("http://www.python.org")
77
78    def test_search_in_python_org(self):
79        """Tests python.org search feature. Searches for the word "pycon" then
80        verified that some results show up.  Note that it does not look for
81        any particular text in search results page. This test verifies that
82        the results are not empty."""
83
84        # Load the main page. In this case the home page of Python.org.
85        main_page = page.MainPage(self.driver)
86        # Checks if the word "Python" is in title
87        assert main_page.is_title_matches(), "python.org title doesn't match."
88        # Sets the text of search textbox to "pycon"
89        main_page.search_text_element = "pycon"
90        main_page.click_go_button()
91        search_results_page = page.SearchResultsPage(self.driver)
92        # Verifies that the results page is not empty
93        assert search_results_page.is_results_found(), "No results found."
94
95    def tearDown(self):
96        self.driver.close()
97
98if __name__ == "__main__":
99    unittest.main()