Reputation: 592
Task: I am writing a script using Ruby and Capybara that checks if a day in a calendar has become available for booking.
Problem: Whenever Capybara can't find this element it throughs an error an everything stops. Below is the line of code that I am using to check if the element is available or not. It return true if is found but throughs and error if it can't find.
page.should have_selector("input[type=submit][value='22']")
Question: How can I keep on refreshing the page until this element is available?
Btw: I am using page.driver.browser.navigate.refresh to refresh the page
Many thanks
Upvotes: 0
Views: 1776
Reputation: 49870
Rather than using have_selector which raises an error on failure, you can use
page.has_selector?("input...")
which will return true if the element exists on the page within Capybara.default_max_wait_time seconds (pass :wait option to change that time), false otherwise.
Upvotes: 2
Reputation: 4215
The default Capybara behaviour is to wait some time (it's customizable) if it does not find the element.
I suggest you to check your code and your selector.
From https://github.com/jnicklas/capybara:
When working with asynchronous JavaScript, you might come across situations where you are attempting to interact with an element which is not yet present on the page. Capybara automatically deals with this by waiting for elements to appear on the page.
Upvotes: 0
Reputation: 7401
Write a wait_until
function use it to wait until the element presents on the page. See below:
def wait_until
require "timeout"
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = yield
value
end
end
to call it:
wait_until { find(:css, "input[type=submit][value='22']").visible? }
# write your case
Upvotes: 0