【问题标题】:Rails 4 + Cucumber: Can't find checkbox element on page?Rails 4 + Cucumber:在页面上找不到复选框元素?
【发布时间】:2015-10-15 00:53:27
【问题描述】:

Controller 中new 方法中的代码:

...
@categories = Category.all
...

new.html.erb 视图中的代码:

<% unless @categories.nil? %>
    <% @categories.each do |t| %>
        <%= t.name %>
        <%= check_box_tag "categories[#{t.id}]", t.name %>

        <br />
    <% end %>
<% end %>

被渲染的 HTML 是:

  Entertainment
  <input id="categories_6" name="categories[6]" type="checkbox" value="Entertainment" />

  <br />
  Health
  <input id="categories_5" name="categories[5]" type="checkbox" value="Health" />

  <br />
  Politics
  <input id="categories_3" name="categories[3]" type="checkbox" value="Politics" />

  <br />
  Sports
  <input id="categories_8" name="categories[8]" type="checkbox" value="Sports" />

  <br />
  Tech
  <input id="categories_4" name="categories[4]" type="checkbox" value="Tech" />

  <br />
  Travel
  <input id="categories_7" name="categories[7]" type="checkbox" value="Travel" />

  <br />
  United States
  <input id="categories_1" name="categories[1]" type="checkbox" value="United States" />

  <br />
  World
  <input id="categories_2" name="categories[2]" type="checkbox" value="World" />

  <br />

在我的 Cucumber 测试中,我希望能够检查我列出的类别,因此我有以下作为我的场景/步骤:

步骤:

When /^(?:|I )check "([^"]*)"$/ do |field|
  check(field)
end

场景:

@javascript 
Scenario: a signed in user can post an article with an associated category
  Then I should see "Sign out"
  Then I should see "New Article"
  Then I should see "Hello"
  When I follow "New Article"
  And I fill in "Url" with "Hello_World_Article_URL.com"
  And I fill in "Initial comment" with "My first comment"
  And I check "categories_6"
  And I press "Create Article"
  Then I should see "Article created!"
  Then article url "Hello_World_Article_URL.com" should exist

但是,这在 And I check "categories_6" 步骤失败,报告 Capybara ElementNotFound 错误。我不清楚为什么通过“id”引用复选框不能正常工作。有没有人了解我可能做错了什么或者我可以如何解决这个问题?

谢谢。

【问题讨论】:

  • 当您使用复选框名称或标签文本而不是 id 时,是否会出现相同的错误?
  • 是的,所有这些我都遇到同样的错误。

标签: ruby-on-rails cucumber


【解决方案1】:

试试这个:

When (/^(?:|I )check "([^"]*)"$/) do |field|
  check(field)
end

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2018-12-09
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多