Class: SitePrism::Page

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL, DSL, ElementChecker, 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 DSL

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


14
15
16
# File 'lib/site_prism/page.rb', line 14

def url
  @url
end

Class Method Details

.set_url(page_url) ⇒ Object


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

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

.set_url_matcher(page_url_matcher) ⇒ Object


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

def set_url_matcher(page_url_matcher)
  @url_matcher = page_url_matcher
end

.url_matcherObject


24
25
26
# File 'lib/site_prism/page.rb', line 24

def url_matcher
  @url_matcher ||= url
end

Instance Method Details

#displayed?(*args) ⇒ Boolean

Returns:

  • (Boolean)

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

def displayed?(*args)
  wait_until_displayed(*args)
rescue SitePrism::TimeoutError
  false
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

When calling #load, all the validations that are set will be ran in order

Parameters:

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

    An optional block to run once the page is loaded.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/site_prism/page.rb', line 46

def load(expansion_or_html = {}, &block)
  self.loaded = false
  SitePrism.logger.debug("Reset loaded state on #{self.class}.")

  return_yield = if expansion_or_html.is_a?(String)
                   load_html_string(expansion_or_html, &block)
                 else
                   load_html_website(expansion_or_html, &block)
                 end

  # Ensure that we represent that the page we loaded is now indeed loaded!
  # This ensures that future calls to #loaded? do not perform the
  # instance evaluations against all load validations procs another time.
  self.loaded = true

  SitePrism.logger.info("#{self.class} loaded.")
  # Return the yield from the block if there was one, otherwise return true
  return_yield || true
end

#pageObject


29
30
31
32
33
34
35
# File 'lib/site_prism/page.rb', line 29

def page
  if defined?(@page)
    @page
  else
    Capybara.current_session
  end
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

#url(expansion = {}) ⇒ Object


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

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 = wait_time) ⇒ Object


80
81
82
83
84
85
86
87
88
# File 'lib/site_prism/page.rb', line 80

def url_matches(seconds = wait_time)
  return unless displayed?(seconds)

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

#wait_until_displayed(*args) ⇒ Object


72
73
74
75
76
77
78
# File 'lib/site_prism/page.rb', line 72

def wait_until_displayed(*args)
  raise SitePrism::NoUrlMatcherForPageError unless url_matcher

  expected_mappings = args.last.is_a?(::Hash) ? args.pop : {}
  seconds = args&.first || wait_time
  Waiter.wait_until_true(seconds) { url_matches?(expected_mappings) }
end