waqas waqas - 2 months ago 16x
Ruby Question

how to reload page until a button appears using capybara and ruby

I want to click a button after an action and button does not appear until and unless I reload the page. And some times it takes some time to appear the button and I have to reload page for more than once. I don't want to put static delays. So is there a way to achieve following using capybara and ruby:

until a button appears


While Mesut's code should work fine, I would re-write it as:

Timeout.timeout(Capybara.default_max_wait_time) do
  loop do
    break if page.has_selector(...)

This will make sure to fail if it will have to wait more than timeout defined in Capybara settings. It can be useful when for example specs are running on the CI server.

Be aware that it can still lead to unexpected behaviors in some drivers, because it can interrupt while some scripts are evaluating.