简体   繁体   中英

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?

我只是这样做,看看它是否适合你:

assert page.current_path == admin_root_path

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM