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

发布于 2025-01-23 13:03:54 字数 5168 浏览 2 评论 0原文

我已经检查了所有的问题,可以找到与此问题有关的问题。

如果我删除了植物板必须具有类别的验证,则其余表格可以通过。

我在其他地方的测试脚本中创建了一个具有“行星”名称的类别。

我刚刚有Capybara屏幕截图工作,现在已经对其进行了更新以包含文本 - 它似乎没有出现在Capybara中,但是正在Local中出现 - 我无法从我的代码中告诉我为什么会隐藏它?

完全错误:

  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

capybara Screenshot的html快照

<%= 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 %>

- 必须将其粘贴为图像如此不喜欢代码。

它在开发浏览器中如何显示 *未测试:

<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.pry.pry)诊断出来。 '在测试的内部(以及失败的期望线之前),这表明我由于“ Let”

I've checked all the SO questions I can find related to this issue.

The rest of the form works and the test passes if I remove the validation that a PlanTemplate must have a Category.

I have created a Category with a name of 'Planets' in the Test script elsewhere.

I've just got Capybara screenshot working and have now updated it to include the text - it doesn't seem to be appearing in Capybara, but is appearing in local - I can't tell from my code why it would be hidden?

Full error:

  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);"

Things I have tried to date are commented out below (have also tried others and removed them):

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

I have also added to rails_helper:

  Capybara.automatic_label_click = true
  Capybara.ignore_hidden_elements = false

The form partial code in question:

<%= 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 %>

The html snapshot from Capybara screenshot - have to paste it as an image as SO doesn't like it in code.

The Snapshot from Capybara - the HTML isn't generated?

How it displays in the DEV browser *not test:

<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>  

This could've been diagnosed by using a debugging tool (e.g. using the pry gem and adding 'binding.pry' inside of the test (and before the failing expect line) which would have shown me that the Categories were not being generated correctly due to the lazy loading of the 'let'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2025-01-30 13:03:54

首先,您所显示的HTML中没有提到“行星”,因此Capybara找不到复选框也就不足为奇了。如果您尝试选择具有1个值的复选框,则其标签内容为“举重片”,根据HTML。

其次,假设您使用的字段set on是input元素,请停止执行查找(...)。设置(...)只需使用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编写测试时。从用户的角度来看,这样做会使您的测试无效,并且基本上毫无意义。设置可能唯一有意义的是网络刮擦时。

First, nowhere in the HTML you've shown is 'Planets' mentioned, so it's not surprising that Capybara can't find the checkbox. If you're trying to select the checkbox with a value of 1 then its label contents is 'Weightloss' according to the HTML.

Second, assuming the fields you're using set on are input elements, stop doing find(...).set(...) and just use fill_in.

Those changes would make your test code

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

If that doesn't work for you, please copy the full error you get and add it to your question.

Additionally, never set Capybara.ignore_hidden_elements = false when writing tests. Doing so makes your tests invalid, from a users perspective, and basically pointless. The only time that setting may make sense is when web scraping.

氛圍 2025-01-30 13:03:54

我遇到的错误是因为我在使用LET(:类别){创建:类别,名称:'Planets',id:99}&amp;令(:类别){创建:类别}作为创建类别的一种方式,但它们在测试中不可用。

我创建了它们,而无需在测试中使用工厂,而且工作正常。

category.create(名称:“类别1”)

我还认为我的工厂板厂将创建一个可以像我在工厂类别内部一样使用的类别我期望的方式。

The error I was having was because I was using let(:category) { create :category, name: 'Planets', id: 99 } & let(:category) { create :category } as a way to create Categories, but they weren't usable in the test.

I created them without using a factory inside the test and it's working fine.

Category.create(name:" Category 1")

I also thought my PlanTemplate factory would create a category that could be used as I have inside of the factory categories { [create(:category)] }, but that isn't usable the way I expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文