【问题标题】:Cucumber `press button` failure (Capybara::ElementNotFound)黄瓜“按下按钮”失败(Capybara::ElementNotFound)
【发布时间】:2011-07-11 22:53:06
【问题描述】:

我是一个相对新手,正在启动一个新的 Ruby on Rails 应用程序。我首先按照https://github.com/intridea/omniauth、http://www.communityguides.eu/articles/16、http://intridea.com/2011/1/31/easy-rails-admin-login-with-google-apps-and-omniauth?blog=company 的指令组合开始。当一切似乎都正常工作时,我开始编写我的第一个黄瓜功能和步骤。我能够启动并运行几个步骤,但我一直陷入我认为内置的步骤中。我有一个包含两个submit_tags 的表单,但我无法成功获得一个场景通过一个基本的And I press "button" 步骤。

可能相关的宝石:

导轨(3.1.0.rc4) 水豚 (1.0.0) 黄瓜 (1.0.1) 黄瓜导轨 (1.0.2) nokogiri (1.4.7) 小黄瓜 (2.4.5) 机架测试(0.6.0) 硒网络驱动程序(0.2.2)

有问题的表格部分:

<%= form_tag :controller => "services", :action => "newaccount" do %>
  <%= submit_tag "confirm", :id => "confirm", :title => "confirm", :value => "confirm", :text => "confirm", :name => "confirm" %> 
  <%= submit_tag "cancel", :id => "cancel", :title => "cancel", :value => "cancel", :text => "cancel", :name => "cancel"  %>
<% end %>

有问题的场景:

Scenario: I register with a valid and currently active google account
    Given I am not registered
     When I sign in with a valid and currently active google account
      And I press "confirm"  # <-- THE PROBLEMATIC STEP
     Then I should see "Your account has been created and you have been signed in!"

我认为这是相关的 web_step(直接来自我根本没有编辑的默认 web_steps.rb):

When /^(?:|I )press "([^"]*)"$/ do |button|
  click_button(button)
end

相关黄瓜产量:

Scenario: I register with a valid and currently active google account            # features/auth_and_auth/initial_tests.feature:6
  Given I am not registered                                                      # features/step_definitions/authentication_steps.rb:1
  When I sign in with a valid and currently active google account                # features/step_definitions/authentication_steps.rb:5
  And I press "confirm"                                                          # features/step_definitions/web_steps.rb:52
    no button with value or id or text 'confirm' found (Capybara::ElementNotFound)
    (eval):2:in `click_button'
    ./features/step_definitions/web_steps.rb:53:in `/^(?:|I )press "([^"]*)"$/'
    features/auth_and_auth/initial_tests.feature:9:in `And I press "confirm"'
  Then I should see "Your account has been created and you have been signed in!" # features/step_definitions/web_steps.rb:105

相关html输出:

<input id="confirm" name="confirm" text="confirm" title="confirm" type="submit" value="confirm">
<input id="cancel" name="cancel" text="cancel" title="cancel" type="submit" value="cancel">

很明显,我计算了value、id、text,以及name 和title。我还看到一个帖子说输入类型必须指定为submit,它似乎是。我用confirm 按钮和cancel 按钮都试过了。

在搜索了我所知道的所有地方,并尝试了每一个看起来甚至无关紧要的建议之后,我陷入了僵局。我错过了什么?

【问题讨论】:

  • 在调用该步骤时,您确定 cucumber 实际上在正确的页面上吗? Capybara 有一个非常有用的 'save_and_open_page' 方法。尝试将它放在 web_steps.rb 中的 click_button 调用之上,并检查它是否真的在正确的页面上。

标签: ruby-on-rails cucumber bdd capybara ruby-on-rails-3.1


【解决方案1】:

我不确定下面的代码是否是处理我遇到的问题的最佳方法,但它正在通过相关步骤。

当 /^(?:|I )按 "([^"]*)"$/ 做 |button| # click_button(button) # 失败的原始 web_steps.rb 版本 %{I press (button)} # 我的修改版通过 结尾

我仍然希望有任何反馈:

  • 为什么原来的 web_steps.rb 版本失败了,
  • 这是否是一种适当的方法,以及
  • 如果有更“rails”的方式来处理这个问题。

【讨论】:

  • 感谢@MacSean,它也对我有用。自从过去 3 天以来,我一直在寻找这个解决方案。你让我很开心 :)
猜你喜欢
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
相关资源
最近更新 更多