I have an html container with a link on my page which is hidden
<div id=”container” style=”display: none”>
<a href=”/somelink” id=”inner-link”>Redirect Here</a>
</div>
An external button, toggles the containers visibility via the jQuery .show()
method
<a id=”toggle-container”>Show panel</a>
$(“#toggle-container”).on(“click”, function(){
$(“#container”).show();
});
All is peachy and runs fine in the browser, but for some reason, when I try to test this behaviour with capybara + poltergeist like so:
# Click the link, this should show the container
click_link(“toggle-container”)
# Click the link inside the container, this should cause a redirect
click_link(“inner-link”)
The test doesn’t fail, but the redirect link doesn’t get clicked by capybara. This is strange because Capybara acts like it finds the #inner-link
element but the redirect never happens (I also captured a screenshot to verify this - the container is shown and the link isn’t clicked).
I’ve managed to solve this by adding sleep 1
function before clicking the redirect link.
# Click the link, this should show the container
click_link(“toggle-container”)
sleep 1
# Click the link inside the container, this should cause a redirect
click_link(“inner-link”)
This is very strange, I can't figure out why sleep would cause the test to go green..
Is it possible that Capybara runs faster than the JS code? if so, what is the proper way to tackle this behavior?