3
3
Page Objects
4
4
------------
5
5
6
- This chapter is a tutorial introduction to page objects design pattern. A page
7
- object represents an area in the web application user interface that your test
8
- is interacting .
6
+ This chapter is a tutorial introduction to the Page Objects design pattern. A
7
+ page object represents an area where the test interacts within the web
8
+ application user interface .
9
9
10
10
Benefits of using page object pattern:
11
11
12
- * Creating reusable code that can be shared across multiple test cases
12
+ * Creating reusable code that can share across multiple test cases
13
13
* Reducing the amount of duplicated code
14
14
* If the user interface changes, the fix needs changes in only one place
15
15
16
16
17
17
Test case
18
18
~~~~~~~~~
19
19
20
- Here is a test case which searches for a word in python.org website and ensure
21
- some results are found.
20
+ Here is a test case that searches for a word on the `python.org ` website and
21
+ ensures some results. The following section will introduce the `page ` module
22
+ where the page objects will be defined.
22
23
23
24
::
24
25
@@ -34,11 +35,10 @@ some results are found.
34
35
self.driver.get("http://www.python.org")
35
36
36
37
def test_search_in_python_org(self):
37
- """
38
- Tests python.org search feature. Searches for the word "pycon" then verified that some results show up.
39
- Note that it does not look for any particular text in search results page. This test verifies that
40
- the results were not empty.
41
- """
38
+ """Tests python.org search feature. Searches for the word "pycon" then
39
+ verified that some results show up. Note that it does not look for
40
+ any particular text in search results page. This test verifies that
41
+ the results were not empty."""
42
42
43
43
#Load the main page. In this case the home page of Python.org.
44
44
main_page = page.MainPage(self.driver)
@@ -61,9 +61,9 @@ some results are found.
61
61
Page object classes
62
62
~~~~~~~~~~~~~~~~~~~
63
63
64
- The page object pattern intends creating an object for each web page. By
65
- following this technique a layer of separation between the test code and
66
- technical implementation is created .
64
+ The page object pattern intends to create an object for each part of a web page.
65
+ This technique helps build a separation between the test code and the actual
66
+ code that interacts with the web page .
67
67
68
68
The ``page.py `` will look like this::
69
69
@@ -78,7 +78,8 @@ The ``page.py`` will look like this::
78
78
79
79
80
80
class BasePage(object):
81
- """Base class to initialize the base page that will be called from all pages"""
81
+ """Base class to initialize the base page that will be called from all
82
+ pages"""
82
83
83
84
def __init__(self, driver):
84
85
self.driver = driver
@@ -92,10 +93,12 @@ The ``page.py`` will look like this::
92
93
93
94
def is_title_matches(self):
94
95
"""Verifies that the hardcoded text "Python" appears in page title"""
96
+
95
97
return "Python" in self.driver.title
96
98
97
99
def click_go_button(self):
98
100
"""Triggers the search"""
101
+
99
102
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
100
103
element.click()
101
104
@@ -122,6 +125,7 @@ The ``element.py`` will look like this::
122
125
123
126
def __set__(self, obj, value):
124
127
"""Sets the text to the value supplied"""
128
+
125
129
driver = obj.driver
126
130
WebDriverWait(driver, 100).until(
127
131
lambda driver: driver.find_element_by_name(self.locator))
@@ -130,6 +134,7 @@ The ``element.py`` will look like this::
130
134
131
135
def __get__(self, obj, owner):
132
136
"""Gets the text of the specified object"""
137
+
133
138
driver = obj.driver
134
139
WebDriverWait(driver, 100).until(
135
140
lambda driver: driver.find_element_by_name(self.locator))
@@ -141,17 +146,20 @@ Locators
141
146
~~~~~~~~
142
147
143
148
One of the practices is to separate the locator strings from the place where
144
- they are being used. In this example, locators of the same page belong to same
145
- class.
149
+ they are getting used. In this example, locators of the same page belong to the
150
+ same class.
146
151
147
152
The ``locators.py `` will look like this::
148
153
149
154
from selenium.webdriver.common.by import By
150
155
151
156
class MainPageLocators(object):
152
157
"""A class for main page locators. All main page locators should come here"""
158
+
153
159
GO_BUTTON = (By.ID, 'submit')
154
160
155
161
class SearchResultsPageLocators(object):
156
- """A class for search results locators. All search results locators should come here"""
162
+ """A class for search results locators. All search results locators should
163
+ come here"""
164
+
157
165
pass
0 commit comments