DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

4 2

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')

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay