3

这是我第一次尝试 Capybara,我似乎无法找到为什么它找不到我的表单元素。

每次我试图找到一个表单元素时,

When /^they submit a resolution in Markdown$/ do
  visit "/resolutions/new"
  fill_in "title", :with => "Title"
  fill_in "libelle", :with => "Some text here\n\nNew paragraph"
  click_button "Submit"
end

它不知道它在哪里,

Scenario: Submission of a resolution                 # features/resolutions.feature:5
    Given the user is a member                       # features/step_definitions/user_steps.rb:26
    When they submit a resolution in Markdown        # features/step_definitions/resolution_steps.rb:8
      cannot fill in, no text field, text area or password field with id, name, or label 'title' found (Capybara::ElementNotFound)
      (eval):2:in `fill_in'
      ./features/step_definitions/resolution_steps.rb:10:in `/^they submit a resolution in Markdown$/'
      features/resolutions.feature:7:in `When they submit a resolution in Markdown'

即使它出现在页面上

<%= form_for(@resolution) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :libelle %><br />
    <%= f.text_area :libelle %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

它似乎已经正确渲染。

Started GET "/resolutions/new" for 127.0.0.1 at 2012-07-06 00:47:24 -0400
  [1m[36m (0.0ms)[0m  [1mSAVEPOINT active_record_1[0m
  [1m[35m (0.2ms)[0m  UPDATE "users" SET "last_sign_in_at" = '2012-07-06 04:47:24.980196', "current_sign_in_at" = '2012-07-06 04:47:24.980196', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2012-07-06 04:47:24.980658' WHERE "users"."email" = 'alexandre@membres.jndq.ca'
  [1m[36m (0.0ms)[0m  [1mRELEASE SAVEPOINT active_record_1[0m
Processing by ResolutionsController#new as HTML
  Rendered resolutions/_form.html.erb (3.8ms)
  Rendered resolutions/new.html.erb within layouts/application (4.9ms)
Completed 200 OK in 18ms (Views: 7.9ms | ActiveRecord: 0.2ms)
  [1m[35m (0.2ms)[0m  rollback transaction
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.0ms)[0m  SAVEPOINT active_record_1
  [1m[36mUser Exists (0.1ms)[0m  [1mSELECT 1 FROM "users" WHERE "users"."email" = 'normand@pq.ui' LIMIT 1[0m
  [1m[35mSQL (0.3ms)[0m  INSERT INTO "users" ("authentication_token", "clearance", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "facebook_uid", "id", "last_sign_in_at", "last_sign_in_ip", "name", "parent", "remember_created_at", "sign_in_count", "unconfirmed_email", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["authentication_token", nil], ["clearance", 9], ["confirmation_sent_at", nil], ["confirmation_token", nil], ["confirmed_at", nil], ["created_at", Fri, 06 Jul 2012 04:47:25 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "normand@pq.ui"], ["facebook_uid", nil], ["id", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["name", "Normand Portequi"], ["parent", nil], ["remember_created_at", nil], ["sign_in_count", 0], ["unconfirmed_email", nil], ["updated_at", Fri, 06 Jul 2012 04:47:25 UTC +00:00]]
  [1m[36m (0.0ms)[0m  [1mRELEASE SAVEPOINT active_record_1[0m

这似乎很明显,但我找不到它。我找到了这个问题,但答案毫无用处。即使在查看了 Devise+Cucumber 示例应用程序之后,我也找不到问题所在。

这正在杀死我和我的测试。有任何想法吗?

4

4 回答 4

4

我遇到了类似的问题,事实证明我需要做的是将“id:”字段添加到表单元素本身。

例如,您有:

<div class="field">
  <%= f.label :title %><br />
  <%= f.text_field :title %>
</div>

但是,如果您将其更改为:

<div class="field">
  <%= f.label :title %><br />
  <%= f.text_field :title, id:"title" %>
</div>

请注意,我在 text_field 元素上添加了一个“id”字段。我不确定为什么 Capybara 没有找到标签,但这对我有用。希望它可以帮助您(或其他人)。

于 2012-10-31T19:03:04.153 回答
4

尝试添加puts page.body之后visit "/resolutions/new",看看你的堆栈跟踪告诉你什么。您将能够看到表单字段的 id。例如,虽然我的一列名为“name”,但呈现的 html 中的 id 是“user_name”。

于 2012-10-01T10:36:22.137 回答
0

这是一个疯狂的猜测,但我认为你需要等待。不确定“fill_in”是否等待页面可用。我知道“查找”会自动等待。

尝试增加

Capybara.default_wait_time
于 2012-07-06T05:32:14.437 回答
0

如果您的 Rails 视图如下所示:

<%= form.text_field :first_name %>

尝试通过将第一部分大写,后跟一个空格,然后将其余部分小写来调用该元素。

fill_in "First name", with: 'Elaine'

这应该可以,不需要 id 属性。

于 2015-08-05T15:36:40.657 回答