Apie
Apie

Reputation: 6431

Using SitePrism with Rspec and Capybara feature specs

I recently discovered SitePrism via the rubyweekly email. It looks amazing. I can see its going to be the future.

The examples I have seen are mostly for cucumber steps. I am trying to figure out how one would go about using SitePrism with rspec.

Assuming @home_page for the home page, and @login_page for the login_page I can understand that

@home_page.load # => visit @home.expanded_url

however, the part I am not sure about, is if I think click on for example the "login" link, and the browser in Capybara goes to the login page - how I can then access an instance of the login page, without loading it.

    @home_page = HomePage.new
    @home_page.load
    @home.login_link.click
    # Here I know the login page should be loaded, so I can perhaps do 

    @login_page = LoginPage.new
    @login_page.should be_displayed
    @login_page.email_field.set("[email protected]")
    @login_page.password_field.set("password")
    @login_page.submit_button.click

etc...

That seems like it might work. So, when you know you are supposed to be on a specific page, you create an instance of that page, and somehow the capybara "page" context, as in page.find("a[href='/sessions/new']") is transferred to the last SitePrism object?

I just feel like I am missing something here. I'll play around and see what I can figure out - just figured I might be missing something. I am looking through the source, but if anyone has figured this out... feel free to share :)

Upvotes: 3

Views: 2628

Answers (1)

Nat Ritmeyer
Nat Ritmeyer

Reputation: 5678

What you've assumed turns out to be exactly how SitePrism works :) Though you may want to check the epilogue of the readme that explains how to save yourself from having to instantiate page objects all over your test code. Here's an example:

# our pages

class Home < SitePrism::Page
  #...
end

class SearchResults < SitePrism::Page
  #...
end

# here's the app class that represents our entire site:

class App
  def home
    Home.new
  end

  def results_page
    SearchResults.new
  end
end

# and here's how to use it:

#first line of the test...
@app = App.new
@app.home.load
@app.home.search_field.set "sausages"
@app.home.search_button.click
@app.results_page.should be_displayed

Upvotes: 4

Related Questions