Skip to content

Unsafe Auto-correction by Capybara/RedundantWithinFind Can Lead to Incorrect Behavior #136

@hatsu38

Description

@hatsu38

Summary

I encountered an issue with the auto-correction provided by Capybara/RedundantWithinFind. The automatic correction replaces within find_by_id with a within string selector, leading to unexpected behavior and causing the test to fail.

Before Auto-correction:

within find_by_id("array-form-session.dates") do
  expect(page).to have_text(:visible, "YYYY/MM/DD")
end

→ Match: <div id="array-form-session.dates">YYYY/MM/DD</div>
→ Not Match: : <div id="array-form-session" class="dates">YYYY/MM/DD</div>
This is the expected behavior.

After Auto-correction:

within "#array-form-session.dates" do
  expect(page).to have_text(:visible, "YYYY/MM/DD")
end

→ Not Match: <div id="array-form-session.dates">YYYY/MM/DD</div>
→ Match: : <div id="array-form-session" class="dates">YYYY/MM/DD</div>

Solution?

While it's rare for . to be used in HTML IDs, I believe a potential solution could be to avoid replacing find_by_id with within when the argument of find_by_id contains a . or to mark it as unsafe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions