Feature tests can oftentimes get long, complicated, and hard to maintain. One common strategy that I use to break them apart is to use "page object classes". These classes may contain methods to find elements on the page, methods to interact with those elements, and methods to make assertions about what the user sees after those interactions are made.
This gist demonstrates what these page object classes look like. One thing to note is that Capybara methods like find
and RSpec methods such as expect
are automatically available in the class. This is made possible by having the class inherit from SimpleDelegator and then having the test pass self
to the class initializer when an object is created. self
is the context in which the test is running, and by receiving it, the page object can access method that the feature test can access. Now, there's a second way you could accomplish this: you could include RSpec::Matchers and Capybara::DSL instead of inheriting from SimpleDelegator. But this approach has