DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

have_selector is good for waiting test in Capybara

🔗 Parent Note

🤔 Situation

Let's suppose that you have a panel and test it's changed from open to closed.

😅 Bad code

expect(find('div.target > div.message', visible: false).visible?).to eq true
find('div.target').click # the panel will be closed.
expect(find('div.target > div.message', visible: false).visible?).to eq false

# Error because Capybara is too fast 😭
# ---
# Failure/Error
#   expected: false
#        got: true

👍Good code

Let's use Capybara::RSpecMatchers#have_selector.

Then, Capybara waits the animation until the test is green.

expect(find('div.target').to have_selector('div.message')
find('div.target').click
expect(find('div.target').not_to have_selector('div.message')

Top comments (0)