Module: Gamera::Visitable

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

Instance Method Summary collapse

Instance Method Details

#displayed?(wait_time_seconds = Capybara.default_wait_time) ⇒ Boolean

Check to see if we eventually land on the right page

Parameters:

  • wait_time_seconds (Integer) (defaults to: Capybara.default_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:



23
24
25
26
27
28
29
30
31
32
# File 'lib/gamera/modules/visitable.rb', line 23

def displayed?(wait_time_seconds = Capybara.default_wait_time)
  fail 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.



52
53
54
# File 'lib/gamera/modules/visitable.rb', line 52

def url
  fail 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



11
12
13
14
15
16
# File 'lib/gamera/modules/visitable.rb', line 11

def visit(fail_on_redirect = true)
  super(url)
  if fail_on_redirect
    fail Gamera::WrongPageVisited, "Expected URL '#{url}', got '#{page.current_url}'" unless displayed?
  end
end

#with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError], &block) ⇒ 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



41
42
43
44
45
46
47
48
49
# File 'lib/gamera/modules/visitable.rb', line 41

def with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError], &block)
  retries_left ||= retries
  visit
  block.call(retries_left)
rescue *allowed_errors => e
  retries_left -= 1
  retry if retries_left >= 0
  raise e
end