Paul Odeon
Paul Odeon

Reputation: 4544

Test you are on a page with Cucumber and Capybara

In The training wheels came off post by Aslak Hellesoy he says he has removed web_steps.rb and paths.rb from more recent versions of cucumber.

I can understand using the Capybara api instead of web_steps.rb, but how would you now test that you are on a specific page?

This is how I used to do it with paths.rb:

#admin_authentication.feature    
Then  I should be on the admin home page

# paths.rb
when /the admin home page/
  admin_root_path

# web_steps.rb
Then /^(?:|I )should be on (.+)$/ do |page_name|
  current_path = URI.parse(current_url).path
  if current_path.respond_to? :should
    current_path.should == path_to(page_name)
  else
    assert_equal path_to(page_name), current_path
  end
end

As a secondary question, should we be doing this at all?

Upvotes: 4

Views: 6472

Answers (2)

joshuanapoli
joshuanapoli

Reputation: 2509

Ordinarily, you should not test the page path in a Cucumber step. Cucumber scenarios are meant to be written from the user's perspective. The user is ordinarily only interested in the page's content, so the test should check the page content rather than the particular URL or controller.

Instead of writing a scenario from the developer's perspective like this:

Scenario: Administrator should be on the admin home page
  When I log in as an administrator
  Then I should be on the admin home page

Write it from the user's perspective like this:

Scenario: Administrator should have super-user tools
  When I log in as an administrator
  Then I should see the disable-abusive-user button

Upvotes: 8

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

I simply do like this, see if it works for you:

assert page.current_path == admin_root_path

Upvotes: 13

Related Questions