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)