@@ -31,18 +31,29 @@ and ensure some results are found.
31
31
import page
32
32
33
33
class PythonOrgSearch(unittest.TestCase):
34
+ """A sample test class to show how page object works"""
34
35
35
36
def setUp(self):
36
37
self.driver = webdriver.Firefox()
37
38
self.driver.get("http://www.python.org")
38
39
39
40
def test_search_in_python_org(self):
41
+ """
42
+ Tests python.org search feature. Searches for the word "pycon" then verified that some results show up.
43
+ Note that it does not look for any particular text in search results page. This test verifies that
44
+ the results were not empty.
45
+ """
46
+
47
+ #Load the main page. In this case the home page of Python.og.
40
48
main_page = page.MainPage(self.driver)
49
+ #Checks if the word "Python" is in title
41
50
assert main_page.is_title_matches(), "python.org title doesn't match."
42
- main_page.search_text_element = "pycon"
43
- main_page.click_go_button()
51
+ #Sets the text of search textbox to "pycon"
52
+ main_page.search_text_element = "pycon"
53
+ main_page.click_go_button()
44
54
search_results_page = page.SearchResultsPage(self.driver)
45
- assert search_results_page.is_results_found(), "No results found."
55
+ #Verifies that the results page is not empty
56
+ assert search_results_page.is_results_found(), "No results found."
46
57
47
58
def tearDown(self):
48
59
self.driver.close()
@@ -59,29 +70,37 @@ The ``page.py`` will look like this::
59
70
from locators import MainPageLocators
60
71
61
72
class SearchTextElement(BasePageElement):
73
+ """This class gets the search text from the specified locator"""
62
74
75
+ #The locator for search box where search string is entered
63
76
locator = 'q'
64
77
65
78
66
79
class BasePage(object):
80
+ """Base class to initialize the base page that will be called from all pages"""
67
81
68
82
def __init__(self, driver):
69
83
self.driver = driver
70
84
71
85
72
86
class MainPage(BasePage):
87
+ """Home page action methods come here. I.e. Python.org"""
73
88
89
+ #Declares a variable that will contain the retrieved text
74
90
search_text_element = SearchTextElement()
75
91
76
92
def is_title_matches(self):
93
+ """Verifies that the hardcoded text "Python" appears in page title"""
77
94
return "Python" in self.driver.title
78
95
79
96
def click_go_button(self):
97
+ """Triggers the search"""
80
98
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
81
99
element.click()
82
100
83
101
84
102
class SearchResultsPage(BasePage):
103
+ """Search results page action methods come here"""
85
104
86
105
def is_results_found(self):
87
106
# Probably should search for this text in the specific page
@@ -97,14 +116,17 @@ The ``element.py`` will look like this::
97
116
98
117
99
118
class BasePageElement(object):
119
+ """Base page class that is initialized on every page object class."""
100
120
101
121
def __set__(self, obj, value):
122
+ """Sets the text to the value supplied"""
102
123
driver = obj.driver
103
124
WebDriverWait(driver, 100).until(
104
125
lambda driver: driver.find_element_by_name(self.locator))
105
126
driver.find_element_by_name(self.locator).send_keys(value)
106
127
107
128
def __get__(self, obj, owner):
129
+ """Gets the text of the specified object"""
108
130
driver = obj.driver
109
131
WebDriverWait(driver, 100).until(
110
132
lambda driver: driver.find_element_by_name(self.locator))
@@ -119,7 +141,9 @@ The ``locators.py`` will look like this::
119
141
from selenium.webdriver.common.by import By
120
142
121
143
class MainPageLocators(object):
144
+ """A class for main page locators. All main page locators should come here"""
122
145
GO_BUTTON = (By.ID, 'submit')
123
146
124
147
class SearchResultsPageLocators(object):
148
+ """A class for search results locators. All search results locators should come here"""
125
149
pass
0 commit comments