From Martin Fowler, the article was originally translated by ThoughtWorks engineer Bowen on his blog and recommended by the translator to the Infoq Chinese station blog for a column. With the permission of Martin Fowler himself, the Infoq Chinese language station has been revised and is here to share.
When you are writing a test for a Web page, you need to manipulate the elements on the Web page to click on the link or validate the displayed content. However, if you manipulate HTML elements directly in your test code, your code is vulnerable because the UI changes frequently. A Page object can encapsulate an HTML page or part of a page, and you can manipulate page elements with the application-specific APIs provided, without the need to search around in HTML.
A basic rule of thumb for the Page object is that the Page object can be done by the software client, whatever the human could do. It should also provide an easily programmable interface and hide the lower parts of the window. So accessing a text box should be done using an access method (accessor methods) to get and return the string, the checkbox should use a Boolean value, and the button should be represented as a behavior-oriented method name. The Page object should encapsulate the behavior of all query and manipulation data on a GUI control as a method. A good rule of thumb is that even if you change the control, the Page object's interface should not change.
Although the term is a "page" object, it does not mean creating an object for each page, such as a page with significant elements that can be independent of a Page object [1]. So, a page that displays multiple albums can have an album page object that contains several photo album page objects. There may also be a header Page object and a Footer Page object. That is, if the hierarchy of some complex UI is used to organize the UI, then it should not be related to the Page object. The rule of thumb is to make it meaningful to the users of the application by modeling the page.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/