Class: SitePrism::Page

Inherits:
Object
  • Object
show all
Extended by:
ElementContainer
Includes:
Capybara::DSL, ElementChecker, Loadable
Defined in:
lib/site_prism/page.rb

Instance Attribute Summary

Attributes included from ElementContainer

#expected_items, #mapped_items

Attributes included from Loadable

#load_error, #loaded

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ElementContainer

add_to_mapped_items, element, elements, expected_elements, iframe, raise_if_block, section, sections

Methods included from Loadable

included, #loaded?, #when_loaded

Methods included from ElementChecker

#all_there?

Class Method Details

.set_url(page_url) ⇒ Object



70
71
72
# File 'lib/site_prism/page.rb', line 70

def self.set_url(page_url)
  @url = page_url.to_s
end

.set_url_matcher(page_url_matcher) ⇒ Object



74
75
76
# File 'lib/site_prism/page.rb', line 74

def self.set_url_matcher(page_url_matcher)
  @url_matcher = page_url_matcher
end

.urlObject



78
79
80
# File 'lib/site_prism/page.rb', line 78

def self.url
  @url
end

.url_matcherObject



82
83
84
# File 'lib/site_prism/page.rb', line 82

def self.url_matcher
  @url_matcher || url
end

Instance Method Details

#displayed?(*args) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



41
42
43
44
45
46
47
48
49
50
# File 'lib/site_prism/page.rb', line 41

def displayed?(*args)
  expected_mappings = args.last.is_a?(::Hash) ? args.pop : {}
  seconds = !args.empty? ? args.first : Waiter.default_wait_time
  raise SitePrism::NoUrlMatcherForPage if url_matcher.nil?
  begin
    Waiter.wait_until_true(seconds) { url_matches?(expected_mappings) }
  rescue SitePrism::TimeoutException
    false
  end
end

#load(expansion_or_html = {}, &block) ⇒ Object

Loads the page. The page will yield the block if defined.

Executes the block, if given. Runs load validations on the page, unless input is a string

Parameters:

  • expansion_or_html (defaults to: {})
  • block (&block)

    An optional block to run once the page is loaded.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/site_prism/page.rb', line 27

def load(expansion_or_html = {}, &block)
  self.loaded = false

  if expansion_or_html.is_a?(String)
    @page = Capybara.string(expansion_or_html)
    yield self if block_given?
  else
    expanded_url = url(expansion_or_html)
    raise SitePrism::NoUrlForPage if expanded_url.nil?
    visit expanded_url
    when_loaded(&block) if block_given?
  end
end

#pageObject



16
17
18
# File 'lib/site_prism/page.rb', line 16

def page
  @page || Capybara.current_session
end

#regexp_backed_matchesObject



62
63
64
# File 'lib/site_prism/page.rb', line 62

def regexp_backed_matches
  url_matcher.match(page.current_url)
end

#secure?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/site_prism/page.rb', line 95

def secure?
  page.current_url.start_with? 'https'
end

#template_backed_matchesObject



66
67
68
# File 'lib/site_prism/page.rb', line 66

def template_backed_matches
  matcher_template.mappings(page.current_url)
end

#url(expansion = {}) ⇒ Object



86
87
88
89
# File 'lib/site_prism/page.rb', line 86

def url(expansion = {})
  return nil if self.class.url.nil?
  Addressable::Template.new(self.class.url).expand(expansion).to_s
end

#url_matcherObject



91
92
93
# File 'lib/site_prism/page.rb', line 91

def url_matcher
  self.class.url_matcher
end

#url_matches(seconds = Waiter.default_wait_time) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/site_prism/page.rb', line 52

def url_matches(seconds = Waiter.default_wait_time)
  return unless displayed?(seconds)

  if url_matcher.is_a?(Regexp)
    regexp_backed_matches
  else
    template_backed_matches
  end
end