使用cabybara进行BDD测试:未找到按钮

时间:2012-05-01 07:45:57

标签: ruby-on-rails cucumber capybara bdd

我有一个简单的功能:

Feature: User can see the welcome page
Scenario: User can see the worker page
    Given I am on the welcome page  
    Then I should see the Worker button
    When I click on the  Worker button
    Then I should be on the Worker page

Scenario: User can see the requester page 
    Given I am on the welcome page  
    Then I should see the Requester button 
    When I click on the Requester button 
    Then I should be on the Requester page 

奇怪的是我可以通过然后我应该看到工人按钮但是无法通过点击测试。

这是我的步骤定义

Then /^I should see the (.+) button$/ do |button_name|
  page.has_button? button_name
end

When /^I click on the (.*) button$/ do |button_name|
  #page.has_button? button_name
  click_on button_name
end

这是我的欢迎页面

  div.row
    %div.span6
      = button_to 'Worker', workers_path, :class => "btn btn-large btn-primary"
    %div.span6                   
      = button_to 'Requester', requesters_path, :class => "btn btn-large btn-primary"

是has_button吗? Cabybara或Cucumber的方法?

2 个答案:

答案 0 :(得分:1)

无论页面上的内容如何,​​您的Then步骤都将始终通过,因为has_button?返回true或false,这两种方法都不会导致Cucumber步骤失败。要实现这一点,您的步骤必须抛出异常,最好通过使用:

来实现
page.should have_button button_name

答案 1 :(得分:0)

has_button?是Capybara提供的方法。记录在案here

而不是click_on button_name尝试click_button button_name