waqas
waqas

Reputation: 1125

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

Upvotes: 1

Views: 3465

Answers (2)

Maciej Małecki
Maciej Małecki

Reputation: 2745

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.

Upvotes: 4

Mesut GUNES
Mesut GUNES

Reputation: 7401

reoload until page.has_selector? returns true, check this:

while true 
  page.evaluate_script 'window.location.reload()'
  if page.has_selector?("css_selector")
    break
  end
end

Upvotes: 0

Related Questions