0

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

Guillaume
  • 1,437
  • 2
  • 15
  • 17
  • 1
    Try out this code without a space at the end of the label. Also, make sure the label itself does not have a space at end in the content. Instead, use CSS padding for moving star further from the label text. – Gabor Garami Aug 11 '22 at 15:48
  • 2
    Are you sure the option (Ryan, Reynolds and Effertz) you're trying to select is in the `select` tag you're specifying, it's not in the picture. Show the actual html of the options in the select from your test. – Thomas Walpole Aug 11 '22 at 20:02
  • You are both right. Thanks! – Guillaume Aug 12 '22 at 14:24

1 Answers1

1

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
Chiperific
  • 4,428
  • 3
  • 21
  • 41
  • 1
    Capybara has no issues with standard `optgroup` grouping of options in a select. It's more likely the option being looked for doesn't actually exist in the select - which would make sense if the @project instance being built to use for filling in the data wasn't built with an existing stage... – Thomas Walpole Aug 11 '22 at 20:25
  • 1
    Yes `"Ryan, Reynolds and Effertz" ` was not on the list. Thanks! – Guillaume Aug 12 '22 at 14:25