module Gamera::Visitable

Constants

CAPYBARA_DEFAULT_MAX_WAIT_TIME

Define CAPYBARA_DEFAULT_MAX_WAIT_TIME differently, depending on the version of Capybara.

Public Instance Methods

displayed?(wait_time_seconds = CAPYBARA_DEFAULT_MAX_WAIT_TIME) click to toggle source

Check to see if we eventually land on the right page

@param wait_time_seconds [Integer] How long to wait for the correct page to load @return [Boolean] true if the url loaded in the browser matches the url_matcher pattern @raise [NoUrlMatcherForPage] if there's no url_matcher for this page

# File lib/gamera/modules/visitable.rb, line 30
def displayed?(wait_time_seconds = CAPYBARA_DEFAULT_MAX_WAIT_TIME)
  raise Gamera::NoUrlMatcherForPage if url_matcher.nil?
  start_time = Time.now
  loop do
    return true if page.current_url.chomp('/') =~ url_matcher
    break unless Time.now - start_time <= wait_time_seconds
    sleep(0.05)
  end
  false
end
url() click to toggle source

A reminder to implement a url method when using this module.

# File lib/gamera/modules/visitable.rb, line 59
def url
  raise NotImplementedError, 'To use the Visitable module, you must implement a url method'
end
visit(fail_on_redirect: true) click to toggle source

Open the page url in the browser specified in your Capybara configuration

@param fail_on_redirect [Boolean] Whether to fail if the site redirects to a page that does not match the url_matcher regex @raise [WrongPageVisited] if the site redirects to URL that doesn't match the url_matcher regex and fail_on_redirect is true

Calls superclass method
# File lib/gamera/modules/visitable.rb, line 18
def visit(fail_on_redirect: true)
  super(url)
  if fail_on_redirect
    raise Gamera::WrongPageVisited, "Expected URL '#{url}', got '#{page.current_url}'" unless displayed?
  end
end
with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError]) { |retries_left| ... } click to toggle source

A method to wait for slow loading data on a page. Useful, for example, when waiting on a page that shows the count of records loaded via a slow web or import.

@param retries [Integer] Number of times to reload the page before giving up @param allowed_errors [Array] Array of errors that trigger a refresh, e.g., if an ExpectationNotMetError was raised during an acceptance test @param block [Block] The block to execute after each refresh

# File lib/gamera/modules/visitable.rb, line 48
def with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError])
  retries_left ||= retries
  visit
  yield(retries_left)
rescue *allowed_errors => e
  retries_left -= 1
  retry if retries_left >= 0
  raise e
end