Module: Gamera::Visitable

Includes:
Capybara::DSL
Included in:
Page
Defined in:
lib/gamera/modules/visitable.rb

Constant Summary collapse

CAPYBARA_DEFAULT_MAX_WAIT_TIME =

Define CAPYBARA_DEFAULT_MAX_WAIT_TIME differently, depending on the version of Capybara.

if Gem::Version.new(Capybara::VERSION) >= Gem::Version.new('2.5.0')
                                   Capybara.default_max_wait_time
                                 else
                                   Capybara.default_wait_time
end

Instance Method Summary collapse

Instance Method Details

#displayed?(wait_time_seconds = CAPYBARA_DEFAULT_MAX_WAIT_TIME) ⇒ Boolean

Check to see if we eventually land on the right page

Parameters:

  • wait_time_seconds (Integer) (defaults to: CAPYBARA_DEFAULT_MAX_WAIT_TIME)

    How long to wait for the correct page to load

Returns:

  • (Boolean)

    true if the url loaded in the browser matches the url_matcher pattern

Raises:



30
31
32
33
34
35
36
37
38
39
# 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

#urlObject

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

Raises:

  • (NotImplementedError)


59
60
61
# 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) ⇒ Object

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

Parameters:

  • fail_on_redirect (Boolean) (defaults to: true)

    Whether to fail if the site redirects to a page that does not match the url_matcher regex

Raises:

  • (WrongPageVisited)

    if the site redirects to URL that doesn’t match the url_matcher regex and fail_on_redirect is true



18
19
20
21
22
23
# 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]) ⇒ Object

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.

Parameters:

  • retries (Integer)

    Number of times to reload the page before giving up

  • allowed_errors (Array) (defaults to: [RSpec::Expectations::ExpectationNotMetError])

    Array of errors that trigger a refresh, e.g., if an ExpectationNotMetError was raised during an acceptance test

  • block (Block)

    The block to execute after each refresh



48
49
50
51
52
53
54
55
56
# 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