用简单的形式和黄瓜解决(Capybara :: ElementNotFound)

时间:2013-02-22 13:36:51

标签: ruby-on-rails-3 cucumber capybara simple-form webrat

我正在使用黄瓜进行测试,我正在使用simple_form作为联系表单。 我收到以下错误:

Given I am on the login page 
And I fill in "student_name" with "sadik" 
    Unable to find field "student_name" (Capybara::ElementNotFound)

我的步骤:

Scenario: Add a student to the database
  Given I am on the login page
  And I fill in "student_name" with "sadik"
  When I press "OK"
  ...

表格:

<%= simple_form_for @student do |f| %>
  <%= f.input :name, label: 'student_name' %>,
    :input_html => { :field => 'student_name' } %>
  <%= f.button :submit %>
<% end %>

我也尝试过id和name而不是label。但它没有效果。

但第一步是正确的:

When /^I go to (.+)$/ do |page_name|
  visit path_to(page_name)
end

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field.gsub(' ', '_'), :with => value)
end

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您的语法和标签似乎不正确。

对于表单,请尝试:

<%= simple_form_for @student do |f| %>
   <%= f.input :name, as: :text, label: 'Student Name' %>
   <%= f.button :submit %>
<% end %>

最后一步尝试:

When /^I fill in student_name with "([^\"]*)"$/ do |value|
  fill_in "Student Name", :with => value
end

编辑:您可能还需要更改此行并删除引号:

And I fill in student_name with "sadik"