Zack
Zack

Reputation: 2497

Ruby selenium/capybara wait for element to go away

I know this isn't a unique problem but I have yet to find an actual solution... I have a page that has a spinner on it while it is loading. What I would like my ruby rspec to do is wait for the spinner to go away before it moves on...

I've already implemented a wait_for_ajax which helps but doesn't completely solve the issue:

def wait_for_ajax
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until page.evaluate_script('jQuery.active').zero?
  end
end

Given the spinner has a certain class (generated by Kendo), how can I wait for that element to go away? This is what I'm currently trying...

def wait_until_load
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until !find(".k-loading-color")
  end
end

Upvotes: 3

Views: 3881

Answers (3)

Thomas Walpole
Thomas Walpole

Reputation: 49900

99% of the time if you need wait_for_ajax you're doing something wrong. In your current case you should just be doing

expect(page).to have_no_css('.k-loading-color')

which will wait up to Capybara.default_max_wait_time seconds for matching elements to disappear. If you need to allow longer than you have Capybara.default_max_wait_time set to you can override the maximum wait time like

expect(page).to have_no_css('.k-loading-color', wait: 10)

which will wait up to 10 seconds for any matching items to disappear

Upvotes: 3

Zack
Zack

Reputation: 2497

I ended up using:

def wait_until_load
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until page.has_no_css?(".k-loading-color")
  end
end

Upvotes: 1

Sam Jeston
Sam Jeston

Reputation: 71

If your current loop isn't working, I expect the spinner with class k-loading-color is remaining in the DOM but with the hidden property.

I'd try using the visible? method

def wait_until_load
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop until !find(".k-loading-color").visible?
  end
end

Of course this will only work if

find(".k-loading-color").visible? == true

when the page first loads

Upvotes: 0

Related Questions