Capybara::ElementNotFound: 无法找到未禁用的复选框

Capybara::ElementNotFound: Unable to find checkbox that is not disabled

我已经检查了我能找到的与此问题相关的所有 SO 问题。

如果我删除 PlanTemplate 必须具有类别的验证,则表单的其余部分有效并且测试通过。

我在别处的测试脚本中创建了一个名为 'Planets' 的类别。

我刚刚让水豚截图工作,现在更新它以包含文本 - 它似乎没有出现在水豚中,但出现在本地 - 我无法从我的代码中看出为什么它会被隐藏吗?

完整错误:

  1) PlanTemplate can be created by admin admin can create new plan
     Failure/Error: expect(page).to have_content('Planets')
       expected to find text "Planets" in "window.addEventListener(\"load\", function(){ window.cookieconsent.initialise({ \"palette\": { \"popup\": { \"background\": \"#252e39\" }, \"button\": { \"background\": \"#428bca\" } }, \"theme\": \"classic\", \"content\": { \"href\": \"Toggle navigation Communities Communities People Mates Weekly Activity view Recent Activity list view All Mate Goals Manage Mates Plans Goals All Goals Manage My Goals New Goal Admin Admin Account Profile Email Settings Manage Mates My Interests Log out New Plan Template Plan name: Plan description: What additional information is required? (optional) What is the delivery medium & requirements of plan? (optional) What should you do once the plan is complete? (optional) How many hours per week does this goal take to complete? How many days in total does this take to complete? /7 for weeks. This will be used to calculate start times What level of user is this plan for? beginner novice intermediate experienced expert What category is this goal in? Feed My Goals My Mates All goals Home Sign Up Log in Browse Users Feedback © Browse Goals Privacy TOS Cookies window.setTimeout(function() { $(\".alert\").fadeTo(500, 0).slideUp(500, function() { $(this).remove(); }); }, 6000);"

我尝试约会的事情在下面被注释掉了(也尝试过其他的并删除了它们):

let(:category) { create :category, name: 'Planets', id: 99 }

  scenario 'admin can create new plan' do
    login_as(create(:admin))
    visit("/plans/new")
    find('#plan-name').set('A test plan name')
    find('#plan-desc').set('A test plan description, I think these need to be longish')
    find('#plan-weekly-hours').set(1)
    # page.check('Planets')
    # find("label[for='Planets']").click
    # find_field(['plan_template_category_ids_1']).check
    # find('.checkbox').check
    # check('Planets')
    # find(:label, 'Planets').click
    # check('.checkbox', allow_label_click: true)
    # find(:label, 'plan_template_category_ids_1').click
    # find('#plan_template_category_ids_1', visible: false).trigger('click')
    find('#plan-days-to-complete').set(35)
    find('#submit-new-plan').click
    expect(page).to have_content('Plan template was successfully created.')
    expect(page).to have_content('A test plan description, I think these need to be longish')
  end

我也添加到rails_helper:

  Capybara.automatic_label_click = true
  Capybara.ignore_hidden_elements = false

有问题的表格部分代码:

<%= form.label "What category is this goal in?" %><br>
  <%= form.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
    <% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
  <% end %>

来自 Capybara 屏幕截图的 html 快照 - 必须将其粘贴为图像,因为代码中不喜欢它。

它在 DEV 浏览器中的显示方式 *未测试:

<div class="col-md-12 goalform">
  <label>What category is this goal in?</label><br>
    <input type="hidden" name="plan_template[category_ids][]" value=""><label class="checkbox-inline input_checkbox" for="plan_template_category_ids_2">
    <input class="checkbox" type="checkbox" value="2" name="plan_template[category_ids][]" id="plan_template_category_ids_2">Dream Chasing</label>
    <label class="checkbox-inline input_checkbox" for="plan_template_category_ids_1"><input class="checkbox" type="checkbox" value="1" name="plan_template[category_ids][]" id="plan_template_category_ids_1">Weightloss</label>
    <label class="checkbox-inline input_checkbox" for="plan_template_category_ids_4"><input class="checkbox" type="checkbox" value="4" name="plan_template[category_ids][]" id="plan_template_category_ids_4">Productivity</label>
    <label class="checkbox-inline input_checkbox" for="plan_template_category_ids_11"><input class="checkbox" type="checkbox" value="11" name="plan_template[category_ids][]" id="plan_template_category_ids_11">Popular</label>
    <label class="checkbox-inline input_checkbox" for="plan_template_category_ids_3"><input class="checkbox" type="checkbox" value="3" name="plan_template[category_ids][]" id="plan_template_category_ids_3">Fitness</label>
    <label class="checkbox-inline input_checkbox" for="plan_template_category_ids_12"><input class="checkbox" type="checkbox" value="12" name="plan_template[category_ids][]" id="plan_template_category_ids_12">Health</label>
</div>  

这可以通过使用调试工具来诊断(例如,使用 pry gem 并在测试中添加 'binding.pry' (在失败的期望行之前),这会告诉我由于 'let'

的延迟加载,类别未正确生成

首先,在您显示的 HTML 中没有任何地方提到 'Planets',因此 Capybara 找不到复选框也就不足为奇了。如果您尝试 select 值为 1 的复选框,则根据 HTML.

其标签内容为 'Weightloss'

其次,假设您在 set 上使用的字段是 input 元素,停止使用 find(...).set(...) 并仅使用 fill_in.

这些更改将使您的测试代码

scenario 'admin can create new plan' do
  login_as(create(:admin))
  visit("/plans/new")
  fill_in('plan-name', with: 'A test plan name')
  fill_in('plan-desc', with: 'A test plan description, I think these need to be longish')
  find('#plan-weekly-hours').set(1) # I don't know what type of element this is? If a select use `choose`

  # check accepts the id, unique name, or associated label text as a locator
  check('Weightloss')
  
  find('#plan-days-to-complete').set(35) # same as above
  click('submit-new-plan')
  expect(page).to have_content('Plan template was successfully created.')
  expect(page).to have_content('A test plan description, I think these need to be longish')
end

如果这对您不起作用,请复制您收到的完整错误并将其添加到您的问题中。

此外,在编写测试时切勿设置 Capybara.ignore_hidden_elements = false。从用户的角度来看,这样做会使您的测试无效,并且基本上毫无意义。唯一有意义的设置是在网络抓取时。

我遇到的错误是因为我使用 let(:category) { create :category, name: 'Planets', id: 99 } & let(:category) { create :category } 作为创建类别的方法,但它们在测试中不可用。

我创建它们时没有在测试中使用工厂,它工作正常。

Category.create(名称:"类别 1")

我还认为我的 PlanTemplate 工厂会创建一个类别,可以像我在工厂类别 { [create(:category)] } 中使用的那样使用,但这并不像我预期的那样可用。