Search code examples
seleniumcucumberwebrat

How to use Page Object pattern with Cucumber and Webrat / Selenium?


I'm looking at a cucumber test suite that is somewhat brittle right now; many small steps, and no knowledge of the page itself.

I'd like to factor out the logic involved in the step definitions inside a series of Selenium PageObjects. As seen here.

However, because I'm using Webrat and not Selenium, everything has to go through the Webrat model. So I cannot do

class MyPage < Selenium::WebPage

end

because that adds a direct dependency.

So I have to route everything through Webrat while still maintaining the Selenium Page object goodness. No documentation on this that I can see: if someone has anything on Webrat + PageModel I'd love to see it.


Solution

  • Turns out the answer is:

    class MyPage < BasePage
      def visit
        @world.visit "/"
      end
    
    end
    
    class BasePage
      def initialize(world)
        @world = world
      end
    end
    

    And then in a step definition:

    Given /I am awesome/ do
      page = MyPage.new(self)
      page.visit
    end