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)