Class: SitePrism::Page

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

Overview

rubocop:disable Metrics/ClassLength

Class Attribute Summary collapse

Attributes included from Loadable

#load_error, #loaded

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ElementContainer

included

Methods included from Loadable

included, #loaded?, #when_loaded

Methods included from ElementChecker

#all_there?, #elements_present

Class Attribute Details

.urlObject (readonly)

Returns the value of attribute url.



84
85
86
# File 'lib/site_prism/page.rb', line 84

def url
  @url
end

Class Method Details

.set_url(page_url) ⇒ Object



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

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

.set_url_matcher(page_url_matcher) ⇒ Object



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

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

.url_matcherObject



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

def self.url_matcher
  @url_matcher || url
end

Instance Method Details

#displayed?(*args) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/site_prism/page.rb', line 45

def displayed?(*args)
  expected_mappings = args.last.is_a?(::Hash) ? args.pop : {}
  seconds = !args.empty? ? args.first : Capybara.default_max_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.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/site_prism/page.rb', line 31

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



20
21
22
# File 'lib/site_prism/page.rb', line 20

def page
  @page || Capybara.current_session
end

#regexp_backed_matchesObject



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

def regexp_backed_matches
  url_matcher.match(page.current_url)
end

#secure?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/site_prism/page.rb', line 100

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

#template_backed_matchesObject



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

def template_backed_matches
  matcher_template.mappings(page.current_url)
end

#url(expansion = {}) ⇒ Object



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

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

#url_matcherObject



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

def url_matcher
  self.class.url_matcher
end

#url_matches(seconds = Capybara.default_max_wait_time) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/site_prism/page.rb', line 57

def url_matches(seconds = Capybara.default_max_wait_time)
  return unless displayed?(seconds)

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