Home > Back-end >  Capybara::ElementNotFound for grouped_select's simple_form
Capybara::ElementNotFound for grouped_select's simple_form

Time:08-12

I used grouped_select input to group nested associations. (see images below) It looks like Capybara can't find this kind of input.

Capybara::ElementNotFound: Unable to find option "Ryan, Reynolds and Effertz" within #<Capybara::Node::Element tag="select"

These are my specs

FactoryBot.define do
  factory :project_type do
    organization

    name { FFaker::Company.unique.name }
  end
end
FactoryBot.define do
  factory :project_stage do
    project_type

    name { FFaker::Company.unique.name }
  end
end
FactoryBot.define do
  factory :project do
    user
    stage factory: :project_stage

    name { FFaker::Company.unique.name }
    note { FFaker::Lorem.phrase }
    start_date { Date.new(2022, 01, 01) }
    end_date { Date.new(2022, 12, 19) }
    budget { 10000 }
  end
end
  test "should create project" do
    login_as @user
    visit projects_url
    find(:css, ".project-application .new-item").click

    fill_in "project_name", with: @project.name
    fill_in "project_budget", with: @project.budget
    fill_in "project_start_date", with: @project.start_date
    fill_in "project_end_date", with: @project.end_date
    fill_in "project_note", with: @project.note
    select(@project.stage.name, from: "Stade ")
    # fill_in "project_project_stage_id", with: @project.project_stage_id
    find(:css, ".hubflo-modal .save-item").click

    assert_text(:all, "Projet créé")
    find(:css, ".project-application .back-to-index").click
  end

form inspect element

CodePudding user response:

Capybara::ElementNotFound: Unable to find option "Ryan, Reynolds and Effertz" within #<Capybara::Node::Element tag="select"

Capybara is finding the select tag correctly, it's just not finding that option in the list. I think you're right that it's struggling with the grouping.

Try selecting an option by value instead of the text.

But, @Thomas Walpole makes a good point.

I tend to believe Capybara is telling the truth. Which means:

@project.stage.name is not an option in the list.

This could be because the controller or view is not creating the select correctly.

It could also be that your test is not saving @project.stage before the controller or view calls for the collection of stages.

Try this:

test "should create project" do
  login_as @user
  visit projects_url
  find(:css, ".project-application .new-item").click

  # debugger to manually verify that @project.stage.name is in the select list
  save_and_open_page

  # fill_in "project_name", with: @project.name
  # fill_in "project_budget", with: @project.budget
  # fill_in "project_start_date", with: @project.start_date
  # fill_in "project_end_date", with: @project.end_date
  # fill_in "project_note", with: @project.note
  # select(@project.stage.name, from: "Stade ")
  # fill_in "project_project_stage_id", with: @project.project_stage_id
  # find(:css, ".hubflo-modal .save-item").click

  # assert_text(:all, "Projet créé")
  # find(:css, ".project-application .back-to-index").click
end
  • Related