waqas waqas - 3 months ago 26
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:

do
page.evaluate_script("window.location.reload()")
until a button appears

Answer

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

Timeout.timeout(Capybara.default_max_wait_time) do
  loop do
    page.evaluate_script("window.location.reload()")
    break if page.has_selector(...)
  end
end

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.