Page Objects

On this week's episode, Chris is again joined by Josh Clayton, Boston Development Director and TDD master, this time to discuss the power of page objects for cleaning up feature specs. What Are Page Objects Page objects encapsulate and abstract ...
This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/page-objects

The ‘Experience’ approach is interesting, I haven’t considered that before.

It doesn’t look like page objects are currently used anywhere in the Upcase project specs, just wondering if there’s a particular reason for that?

Hi @andyw8, thanks for questions. No particular reason for the lack of page objects in Upcase. I believe part of it may be the gradual growth of the code base over the many years it has existed, although as we said in the video page objects are ideally refactored to.

While preparing for the video @joshclayton and I did discuss trying to add a page object and refactor a feature spec on Upcase and agreed it would be a nice improvement, but we wanted to make sure we had sufficient time to cover everything in the episode and thus chose not to.

Hello, i’m starting to use page objects in my app, but how can i make the failure messages more descriptive, for example, i have the following.

def has_success_message? has_css?(".alert-info", text: success_message) end
But getting the following failure

Failure/Error: expect(new_sale).to have_success_message expected #has_success_message? to return true, got false

I would love to have the failure that give me for instance

Failure/Error: expect(page).to have_content "Failure" expected to find text "Failure" in "Toggle navigation Baron's Sign in as email1@example.com New SaleBack Please review the problems below: * Store can't be blank Customer * First Name * Last Name * Tax Id Address Details Product Quantity Price Employee Subtotal $85.00 Remove Item Add Item Total Notes Subtotal $85.00 Discount Total $85.00 Payments Credit Card Add CC Cash Cash Change $0.00"

Thank you for your help!! @christoomey @joshuaclayton

Imo there’s only one way to find out, why spec is failing - add binding.pry (or some other debugger command) and just check, what the page contains through page.text or sth :slight_smile:

Unfortunately this is one of the limitations of page objects (really this is related to extracting matcher methods, not specific to page objects). That said, while this will make TDD a bit more difficult, the idea with page objects and these sort of test refactorings is to optimize for readability and maintainability. In that light, despite the less clear failure messages, I still feel that page objects and extracted helper methods are a big win.

In the end, when specs do fail, it tends to be very straightforward to trace that through to the helper method and sort things out. And, as @matisnape said, binding.pry is always your friend :slight_smile:

How can you add mocking and/or stubbing language to a Page Object?

For example, when I add a method in a Page Object that contains allow_any_instance_of, allow_any_instance_of is undefined and unavailable.

What would need to be required or included, so that you can call mocking and stubbing methods from within a Page Object?

I’ve tried things like requiring the rails helper and configuring any framework to use rspec mocks, but to no avail.

Thanks!