Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hatsu38 opened this issue Oct 19, 2024 · 0 comments · May be fixed by #137
Open

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

hatsu38 opened this issue Oct 19, 2024 · 0 comments · May be fixed by #137
Labels
bug Something isn't working

Comments

@hatsu38
Copy link

hatsu38 commented Oct 19, 2024

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.

@ydah ydah added the bug Something isn't working label Oct 20, 2024
ydah added a commit that referenced this issue Oct 31, 2024
…escape required css selector

Fix: #136

Previously, the following automatic corrections were made.

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

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

This is `.' in find_by_id. ` has the same meaning as the escaped id.
In other words, when replacing within, the `. ` must be escaped.

This PR has been modified so that escaping is correctly done in selectors that require escaping.
This will prevent the behavior from changing before and after the automatic correction.
ydah added a commit that referenced this issue Oct 31, 2024
…escape required css selector

Fix: #136

Previously, the following automatic corrections were made.

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

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

This is `.' in find_by_id. ` has the same meaning as the escaped id.
In other words, when replacing within, the `. ` must be escaped.

This PR has been modified so that escaping is correctly done in selectors that require escaping.
This will prevent the behavior from changing before and after the automatic correction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants