Skip to content

Commit 7bf0cb9

Browse files
committed
docs: update all relevant page_object_models.*.md
1 parent 70431fc commit 7bf0cb9

File tree

3 files changed

+194
-132
lines changed

3 files changed

+194
-132
lines changed

website_and_docs/content/documentation/test_practices/encouraged/page_object_models.ja.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ A Page Object only models these as objects within the test code.
1919
This reduces the amount of duplicated code and means that if the UI changes,
2020
the fix needs only to be applied in one place.
2121

22-
Page Object is a Design Pattern that has become popular in test automation for
22+
Page Object Model is a Design Pattern that has become popular in test automation for
2323
enhancing test maintenance and reducing code duplication. A page object is an
2424
object-oriented class that serves as an interface to a page of your AUT. The
2525
tests then use the methods of this page object class whenever they need to
@@ -44,6 +44,7 @@ helpful tips beyond the scope of this user guide. To get you started,
4444
we’ll illustrate page objects with a simple example.
4545

4646
### Examples
47+
4748
First, consider an example, typical of test automation, that does not use a
4849
page object:
4950

@@ -75,7 +76,7 @@ must change.
7576
* The ID-locators would be spread in multiple tests, in all tests that had to
7677
use this login page.
7778

78-
Applying the page object techniques, this example could be rewritten like this
79+
Applying the page object model, this example could be rewritten like this
7980
in the following example of a page object for a Sign-in page.
8081

8182
```java
@@ -184,6 +185,7 @@ there are a few basic rules for getting the desired maintainability of your
184185
test code.
185186

186187
## Assertions in Page Objects
188+
187189
Page objects themselves should never make verifications or assertions. This is
188190
part of your test and should always be within the test’s code, never in a page
189191
object. The page object will contain the representation of the page, and the
@@ -198,6 +200,7 @@ HomePage constructors check that the expected page is available and ready for
198200
requests from the test.
199201

200202
## Page Component Objects
203+
201204
A page object does not necessarily need to represent all the parts of a
202205
page itself. This was [noted by Martin Fowler](https://martinfowler.com/bliki/PageObject.html#footnote-panel-object) in the early days, while first coining the term "panel objects".
203206

@@ -280,7 +283,7 @@ public class ProductsPage extends BasePage {
280283
.stream()
281284
.filter(condition) // Filter by product name or price
282285
.findFirst()
283-
.orElseThrow();
286+
.orElseThrow(() -> new RuntimeException("Product not found")); // Error thrown during actual test run
284287
}
285288
}
286289
```
@@ -348,7 +351,7 @@ public class ProductsTest {
348351
}
349352
```
350353

351-
The page and component are represented by their own objects. Both objects only have methods for the **services** they offer, which matches the real-world application in object-oriented programming.
354+
The page and component are represented by their own objects. Both objects only have methods for the **services** they offer, which matches the real-world application as is the core principle of object-oriented programming. When applications are built, they are not made of a massive page entity. They are built with components contained in a page. Page Component Objects implement the same approach.
352355

353356
You can even
354357
nest component objects inside other component objects for more complex
@@ -357,6 +360,7 @@ components used throughout the site (e.g. a navigation bar), then it
357360
may improve maintainability and reduce code duplication.
358361

359362
## Other Design Patterns Used in Testing
363+
360364
There are other design patterns that also may be used in testing. Discussing all of these is
361365
beyond the scope of this user guide. Here, we merely want to introduce the
362366
concepts to make the reader aware of some of the things that can be done. As
@@ -365,12 +369,11 @@ reader to search for blogs on these topics.
365369

366370
## Implementation Notes
367371

372+
Page Objects 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.
368373

369-
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.
370-
371-
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.
374+
Because we're encouraging the developer of a test to try and think about the services they're interacting with rather than the implementation, Page Objects should seldom expose the underlying WebDriver instance. To facilitate this, **methods on the Page Object may return another Page Object, another Page Component Object, or even itself (this)**. 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 Page Objects.
372375

373-
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:
376+
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 Page Object:
374377

375378
```java
376379
public class LoginPage {
@@ -388,7 +391,7 @@ public class LoginPage {
388391
}
389392
```
390393

391-
The code presented above shows an important point: the tests, not the PageObjects, should be responsible for making assertions about the state of a page. For example:
394+
The code presented above shows an important point: the tests, not the Page Objects, should be responsible for making assertions about the state of a page. For example:
392395

393396
```java
394397
public void testMessagesAreReadOrUnread() {
@@ -408,17 +411,17 @@ public void testMessagesAreReadOrUnread() {
408411
}
409412
```
410413

411-
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.
414+
Of course, as with every guideline, there are exceptions, and one that is commonly seen with Page Objects is to check that the WebDriver is on the correct page when we instantiate the Page Object. This is done in the example below.
412415

413-
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.
416+
Finally, a Page Object need not represent an entire page and can be composed of Page Object Components. These components 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.
414417

415418
## Summary
416419

417-
* The public methods represent the services that the page offers
418-
* Try not to expose the internals of the page
420+
* The public methods represent the services that the page or component offers
421+
* Try not to expose the internals of the page or component
419422
* Generally don't make assertions
420-
* Methods return other PageObjects
421-
* Need not represent an entire page
423+
* Methods return other Page Objects, Page Component Objects, or optionally themselves (for fluent syntax)
424+
* Need not represent an entire page all the time
422425
* Different results for the same action are modelled as different methods
423426

424427
## Example
@@ -492,4 +495,4 @@ public class LoginPage {
492495
return submitLogin();
493496
}
494497
}
495-
```
498+
```

0 commit comments

Comments
 (0)