Home > Back-end >  Capybara::ElementNotFound: Unable to find checkbox that is not disabled
Capybara::ElementNotFound: Unable to find checkbox that is not disabled

Time:04-24

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

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related