gpt4 book ai didi

capybara - 使用 simple_form 时如何在 capybara 测试中选中复选框

转载 作者:行者123 更新时间:2023-12-03 03:10:37 24 4
gpt4 key购买 nike

尝试在简单页面中测试某些功能,但在使用 page.check 时收到此错误:

    Failures:

1) searching for stores serving a taco and a sauce: Adobada tacos with Chile de Arbol sauce
Failure/Error: page.check 'Adobada'

Capybara::ElementNotFound:
Unable to find checkbox "Adobada"
# ./spec/views/store_search_spec.rb:8:in `block (2 levels) in <top (required)>'

这是我的 HTML:

<%= simple_form_for :stores, url: stores_path, method: :get do |f| %>

<%= f.label :tacos, 'Select Tacos to Find: ', class: 'label' %>
<div class='taco-select checkboxes'>
<%= f.collection_check_boxes :tacos, Taco.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>

<%= f.label :salsa, 'Select Sauces to Find: ', class: 'label' %>
<div class='salsa-select checkboxes'>
<%= f.collection_check_boxes :salsas, Salsa.order(:name), :id, :name,
:include_hidden => false, :item_wrapper_class => 'checkbox_container' %>
</div>

<%= f.submit 'Search!' %>

<% end %>

这是我的测试:

require 'rails_helper'

feature 'searching for stores', %(serving a taco and a sauce:) do

scenario 'Adobada tacos with Chile de Arbol sauce' do
visit root_path

page.check 'Adobada'
page.check 'Chile de Arbol'

click_button 'Search!'

expect(page).to have_content "Store"
expect(page).to have_content 'City'

end
end

我想测试当某些复选框设置为 true 时,页面中会呈现特定内容。不知道如何解决这个问题。

最佳答案

尝试使用这些,它们应该适合您:

find(:css, "#YOUR_CHECKBOX_ID").set(true)

page.check('YOUR_CHECKBOX_ID')

或者简单地

find('THE CSS SELECTOR OF YOUR CHECKBOX').click
find(:xpath, 'THE XPATH OF YOUR CHECKBOX').click

关于capybara - 使用 simple_form 时如何在 capybara 测试中选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com