You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Passed text through spellchecker to identify & fix mistakes
I fixed the mistakes identified by the advanced spell-checker Grammarly.
* Update details.en.md
Passed through spell checker to fix errors and improve structure.
* New headings, grammar/punctuation fixes
Added new headings to very long text on page object models help.
Fixed spelling, grammar, and punctuation errors.
* Update fresh_browser_per_test.en.md
Added a comma to the first senctence
* Removed mistakenly duplicated paragraph
Copy file name to clipboardExpand all lines: website_and_docs/content/documentation/test_practices/encouraged/page_object_models.en.md
+21-17Lines changed: 21 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -13,33 +13,33 @@ the [Selenium wiki](https://github.com/SeleniumHQ/selenium/wiki/PageObjects)
13
13
14
14
## Overview
15
15
16
-
Within your web app's UI there are areas that your tests interact with.
17
-
A Page Object simply models these as objects within the test code.
16
+
Within your web app's UI, there are areas where your tests interact with.
17
+
A Page Object only models these as objects within the test code.
18
18
This reduces the amount of duplicated code and means that if the UI changes,
19
-
the fix need only be applied in one place.
19
+
the fix needs only to be applied in one place.
20
20
21
-
Page Object is a Design Pattern which has become popular in test automation for
21
+
Page Object is a Design Pattern that has become popular in test automation for
22
22
enhancing test maintenance and reducing code duplication. A page object is an
23
23
object-oriented class that serves as an interface to a page of your AUT. The
24
24
tests then use the methods of this page object class whenever they need to
25
25
interact with the UI of that page. The benefit is that if the UI changes for
26
26
the page, the tests themselves don’t need to change, only the code within the
27
-
page object needs to change. Subsequently all changes to support that new UI
27
+
page object needs to change. Subsequently, all changes to support that new UI
28
28
are located in one place.
29
29
30
30
### Advantages
31
31
32
-
* There is a clean separation between test code and pagespecific code such as
32
+
* There is a clean separation between the test code and page-specific code, such as
33
33
locators (or their use if you’re using a UI Map) and layout.
34
-
* There is a single repository for the services or operations offered by the page
34
+
* There is a single repository for the services or operations the page offers
35
35
rather than having these services scattered throughout the tests.
36
36
37
-
In both cases this allows any modifications required due to UI changes to all
38
-
be made in one place. Useful information on this technique can be found on
37
+
In both cases, this allows any modifications required due to UI changes to all
38
+
be made in one place. Helpful information on this technique can be found on
39
39
numerous blogs as this ‘test design pattern’ is becoming widely used. We
40
-
encourage the reader who wishes to know more to search the internet for blogs
40
+
encourage readers who wish to know more to search the internet for blogs
41
41
on this subject. Many have written on this design pattern and can provide
42
-
useful tips beyond the scope of this user guide. To get you started, though,
42
+
helpful tips beyond the scope of this user guide. To get you started,
43
43
we’ll illustrate page objects with a simple example.
44
44
45
45
### Examples
@@ -178,6 +178,7 @@ There is a lot of flexibility in how the page objects may be designed, but
178
178
there are a few basic rules for getting the desired maintainability of your
179
179
test code.
180
180
181
+
## Assertions in Page Objects
181
182
Page objects themselves should never make verifications or assertions. This is
182
183
part of your test and should always be within the test’s code, never in an page
183
184
object. The page object will contain the representation of the page, and the
@@ -191,6 +192,7 @@ instantiating the page object. In the examples above, both the SignInPage and
191
192
HomePage constructors check that the expected page is available and ready for
192
193
requests from the test.
193
194
195
+
## Page Component Objects
194
196
A page object does not necessarily need to represent all the parts of a
195
197
page itself. The same principles used for page objects can be used to
196
198
create "Page _Component_ Objects" that represent discrete chunks of the
@@ -202,6 +204,7 @@ pages. If a page in the AUT has multiple components, or common
202
204
components used throughout the site (e.g. a navigation bar), then it
203
205
may improve maintainability and reduce code duplication.
204
206
207
+
## Other Design Patterns Used in Testing
205
208
There are other design patterns that also may be used in testing. Some use a
206
209
Page Factory for instantiating their page objects. Discussing all of these is
207
210
beyond the scope of this user guide. Here, we merely want to introduce the
@@ -211,11 +214,12 @@ reader to search for blogs on these topics.
211
214
212
215
## Implementation Notes
213
216
214
-
PageObjects can be thought of as facing in two directions simultaneously. Facing towards the developer of a test, they represent the **services** offered by a particular page. Facing away from the developer, they should be the only thing that has a deep knowledge of the structure of the HTML of a page (or part of a page) It's simplest to think of the methods on a Page Object as offering the "services" that a page offers rather than exposing the details and mechanics of the page. As an example, think of the inbox of any web-based email system. Amongst the services that it offers are typically the ability to compose a new email, to choose to read a single email, and to list the subject lines of the emails in the inbox. How these are implemented shouldn't matter to the test.
215
217
216
-
Because we're encouraging the developer of a test to try and think about the services that they're interacting with rather than the implementation, PageObjects should seldom expose the underlying WebDriver instance. To facilitate this, methods on the PageObject should return other PageObjects. This means that we can effectively model the user's journey through our application. It also means that should the way that pages relate to one another change (like when the login page asks the user to change their password the first time they log into a service, when it previously didn't do that) simply changing the appropriate method's signature will cause the tests to fail to compile. Put another way, we can tell which tests would fail without needing to run them when we change the relationship between pages and reflect this in the PageObjects.
218
+
PageObjects can be thought of as facing in two directions simultaneously. Facing toward the developer of a test, they represent the **services** offered by a particular page. Facing away from the developer, they should be the only thing that has a deep knowledge of the structure of the HTML of a page (or part of a page) It's simplest to think of the methods on a Page Object as offering the "services" that a page offers rather than exposing the details and mechanics of the page. As an example, think of the inbox of any web-based email system. Amongst the services it offers are the ability to compose a new email, choose to read a single email, and list the subject lines of the emails in the inbox. How these are implemented shouldn't matter to the test.
217
219
218
-
One consequence of this approach is that it may be necessary to model (for example) both a successful and unsuccessful login, or a click could have a different result depending on the state of the app. When this happens, it is common to have multiple methods on the PageObject:
220
+
Because we're encouraging the developer of a test to try and think about the services they're interacting with rather than the implementation, PageObjects should seldom expose the underlying WebDriver instance. To facilitate this, methods on the PageObject should return other PageObjects. This means we can effectively model the user's journey through our application. It also means that should the way that pages relate to one another change (like when the login page asks the user to change their password the first time they log into a service when it previously didn't do that), simply changing the appropriate method's signature will cause the tests to fail to compile. Put another way; we can tell which tests would fail without needing to run them when we change the relationship between pages and reflect this in the PageObjects.
221
+
222
+
One consequence of this approach is that it may be necessary to model (for example) both a successful and unsuccessful login; or a click could have a different result depending on the app's state. When this happens, it is common to have multiple methods on the PageObject:
219
223
220
224
```
221
225
public class LoginPage {
@@ -253,9 +257,9 @@ public void testMessagesAreReadOrUnread() {
253
257
}
254
258
```
255
259
256
-
Of course, as with every guideline there are exceptions, and one that is commonly seen with PageObjects is to check that the WebDriver is on the correct page when we instantiate the PageObject. This is done in the example below.
260
+
Of course, as with every guideline, there are exceptions, and one that is commonly seen with PageObjects is to check that the WebDriver is on the correct page when we instantiate the PageObject. This is done in the example below.
257
261
258
-
Finally, a PageObject need not represent an entire page. It may represent a section that appears many times within a site or page, such as site navigation. The essential principle is that there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page.
262
+
Finally, a PageObject need not represent an entire page. It may represent a section that appears frequently within a site or page, such as site navigation. The essential principle is that there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page.
259
263
260
264
## Summary
261
265
@@ -343,4 +347,4 @@ public class LoginPage {
343
347
344
348
## Support in WebDriver
345
349
346
-
There is a PageFactory in the support package that provides support for this pattern, and helps to remove some boiler-plate code from your Page Objects at the same time.
350
+
There is a PageFactory in the support package that provides support for this pattern and helps to remove some boiler-plate code from your Page Objects at the same time.
0 commit comments