If the CSS selector on the page changes, you will need to change every test that uses the CSS selector.

Selenium calls are notoriously unreliable. They provide no retry logic to protect you from timing issues, which can cause intermittent test failures. In contrast, bok-choy‘s higher-level interface for browser interactions include robust error-checking and retry logic.

The page object will first check that the browser is on the correct page before trying to use the page. It will also retry if, for example, JavaScript modifies the <div> in between the time we retrieve it and when we get the element’s text (this would result in a run-time exception otherwise). Finally, if the CSS selector on the page changes, we can modify the page object, thus updating every test that interacts with the page.

Because the page object contains the assertion, the page object is less re-usable. If another test expects the page title to be something other than “Test Section”, it cannot re-use check_section_title().

Sometimes, tests fail because when they check the page too soon. Often, tests must wait for JavaScript on the page to finish manipulating the DOM, such as when adding elements or even attaching event listeners. In these cases, it is tempting to insert an explicit wait using time.sleep(). For example:

When you call do_foo(), the page will wait for section.bar to be present in the DOM.

Second, the page object can use a Promise to wait for the DOM to be in a certain state. For example, suppose that the page is ready when a “loading” message is no longer visible. You could check this condition using a Promise:

Page objects generally provide two ways of interacting with a page:
1. Querying the page for information.
2. Performing an action on the page.

In the second case, page objects should wait for the action to complete before returning. For example, suppose a page object has a method save_document() that clicks a Save button. The page then redirects to a different page. In this case, the page object should wait for the next page to load before returning control to the caller.