【问题标题】:Capybara choose method Unable to find radio buttonCapybara 选择方法无法找到单选按钮
【发布时间】:2014-01-23 21:35:23
【问题描述】:

我有一个由简单表单生成的表单:

TL TR
<div class="form-group radio_buttons required user_register_temp_package">
  <label class="radio_buttons required control-label">
    <abbr title="zorunlu">
      *
    </abbr>
    Paket
  </label>
  <label class="radio">
    <input class="radio_buttons required" id="user_register_temp_attributes_domain_package_id_1" name="user[register_temp_attributes][domain_package_id]" type="radio" value="1">
    Small
  </label>
  <label class="radio">
    <input checked="checked" class="radio_buttons required" id="user_register_temp_attributes_domain_package_id_2" name="user[register_temp_attributes][domain_package_id]" type="radio" value="2">
    Medium
  </label>
  <label class="radio">
    <input class="radio_buttons required" id="user_register_temp_attributes_domain_package_id_3" name="user[register_temp_attributes][domain_package_id]" type="radio" value="3">
    Large
  </label>
</div>
TL TR

我有一个这样的简单规范:

# encoding: UTF-8
require 'spec_helper'

feature 'Register' do

  background do
    visit new_user_registration_path
  end

  scenario 'fill register form and register' do
    # TL TR
    choose('user_register_temp_attributes_domain_package_id_1')
    # TL TR
  end

end

我的 spec_helper.rb 是

ENV['RAILS_ENV'] ||= 'test'
require File

.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

Capybara.javascript_driver = :webkit
Capybara.default_selector = :css

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'

  # Capybara DSL
  config.include Capybara::DSL

  # Factory girl
  config.include FactoryGirl::Syntax::Methods

end

choose 方法的 Capybara API 说:

“找到一个单选按钮并将其标记为选中。单选按钮可以通过名称、ID 或标签文本找到。”

但是当我使用choose('user_register_temp_attributes_domain_package_id_1') 运行规范时,我得到了 Capybara::ElementNotFound: Unable to find radio button "user_register_temp_attributes_domain_package_id_1"

我尝试了下面的代码,但得到了Capybara::ElementNotFound: Unable to find css "user_register_temp_attributes_domain_package_id_1" 错误:

find('#user_register_temp_attributes_domain_package_id_1[value=1]').set(true)

fill_incheckclick_button 方法似乎没有问题。

问候。

【问题讨论】:

    标签: forms testing capybara bdd


    【解决方案1】:

    很可能底层驱动程序认为这个单选按钮是不可见的。默认情况下,Capybara 只找到可见元素(因为默认情况下 Capybara.ignore_hidden_elementstrue)所以它没有找到那个元素。

    试试:

    choose('user_register_temp_attributes_domain_package_id_1', visible: false)
    

    您可以通过向 Capybara 提交拉取请求来改进错误消息。

    【讨论】:

    • 感谢您的回答,但没有任何改变。问候。
    • @onurozgurozkan 请尝试page.execute_script("document.getElementById('user_register_temp_attributes_domain_package_id_1').checked = true")
    • 它有效。但是为什么选择不起作用是不合逻辑的。我将检查页面的 HTML 验证和更多的调试。非常感谢。
    • page.execute_script 是我找到单选按钮并选择它的唯一方法
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2018-05-15
    相关资源
    最近更新 更多