ruby-on-rails - 为什么 capybara 找不到表单元素?

标签 ruby-on-rails ruby rspec capybara

我们正在尝试将 capybara 引入我们的 rspec 示例中,但还不太顺利。我不知道这是否是 confusion between capybara and rails integration testing 的一部分,但是……

这是 rspec 示例:

require 'spec_helper'

describe "planning a trip", :type => :feature do
  before :each do
    FactoryGirl.create(:user)
  end

  it "creates a new trip" do
    visit '/trips/new'
    save_and_open_page
    within("#new_trip") do
      fill_in '#trip_from_place_nongeocoded_address', :with => '730 w peachtree st, atlanta, ga'
      fill_in '#trip_to_place_nongeocoded_address', :with => 'georgia state capitol, atlanta, ga'
    end
    click_link 'Plan it'
    expect(page).to have_content 'Success'
  end
end

这是 HTML 的相关部分,来自 Capybara 的 save_and_open_page:

  <form accept-charset="UTF-8" action="/trips" class=
  "simple_form form-horizontal" id="new_trip" method="post"
  novalidate="novalidate">

    <div class=
    "control-group string required trip_from_place_nongeocoded_address">
    <label class="string required control-label" for=
    "trip_from_place_nongeocoded_address"><abbr title=
    "required">*</abbr> From</label>

      <div class="controls">
        <input class="string required" id=
        "trip_from_place_nongeocoded_address" name=
        "trip[from_place][nongeocoded_address]" placeholder=
        "Enter address" size="50" type="text">
      </div>
    </div>
[...etc...]

但是运行 rspec 示例失败了:

  1) planning a trip creates a new trip
     Failure/Error: fill_in '#trip_from_place_nongeocoded_address', :with => '730 w peachtree st, atlanta, ga'
     Capybara::ElementNotFound:
       Unable to find field "#trip_from_place_nongeocoded_address"
     # ./spec/features/plan_a_trip_spec.rb:12:in `block (3 levels) in <top (required)>'
     # ./spec/features/plan_a_trip_spec.rb:11:in `block (2 levels) in <top (required)>'

建议?

最佳答案

据我所知,如果您的输入有:

id="trip_from_place_nongeocoded_address"

然后,您需要:

fill_in 'trip_from_place_nongeocoded_address'

...没有#

Capybara: How do I fill in a input field by its ID

关于ruby-on-rails - 为什么 capybara 找不到表单元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976959/

相关文章:

ruby-on-rails - 如何在命名范围内有多个条件?

ruby-on-rails - 接下来,使用命名范围的先前记录

ruby-on-rails - 阅读 Rails session secret 的最佳方式是什么?

arrays - 哈希数组之间的交集,但返回完整的哈希

ruby-on-rails - 寻找RSpec的GUI

ruby - 使用 XPath 解析文本电子邮件地址,NOT//A[startswith(@href, 'mailto:' )]

mysql - 异常的 ActiveRecord 行为

ruby - 在 OS X 上安装 Homebrew 时出错

ruby-on-rails-3 - BDD/TDD (Rails/Rspec) 入门

ruby - 我如何在不使用 ruby​​ 中的 sleep() 的情况下 rspec/test 一个 updated_at 字段?