Class: SitePrism::Page

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

Overview

[SitePrism::Page]

SitePrism Pages are the top level construct of the POM framework

Instances of this class represent a full web page that can either be dynamically navigated to through clicking buttons or filling in fields, or verbosely loaded by using the #load method

All method calls made whilst on a page are scoped using #to_capybara_node which defaults to the current Capybara session

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?, #run_load_validations, #when_loaded

Methods included from ElementChecker

#all_there?, #elements_missing, #elements_present

Class Attribute Details

.urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.set_url(page_url) ⇒ String

Sets and returns the specific url that will be loaded for a page object

Returns:

  • (String)


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

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

.set_url_matcher(page_url_matcher) ⇒ Regexp

Sets and returns the specific url matcher that will be used to validate the page is loaded

Returns:

  • (Regexp)


31
32
33
# File 'lib/site_prism/page.rb', line 31

def set_url_matcher(page_url_matcher)
  @url_matcher = page_url_matcher
end

.url_matcherRegexp || String

The specific url matcher that is used to validate the page is loaded. When one hasn't been previously set, use the url that was set as a direct Regexp exact matcher

Returns:

  • (Regexp || String)


39
40
41
# File 'lib/site_prism/page.rb', line 39

def url_matcher
  @url_matcher ||= url
end

Instance Method Details

#displayed?(*args) ⇒ Boolean

Returns true if the page is displayed within the requisite time Returns false if the page is not displayed within the requisite time

Returns:

  • (Boolean)


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

def displayed?(*args)
  wait_until_displayed(*args)
rescue SitePrism::Error::TimeoutError
  false
end

#load(params = {}) ⇒ Object

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

Executes the block, if given

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

Parameters:

  • params (defaults to: {})
    • An optional set of parameters
  • &
    • An optional block to run once the page is loaded


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/site_prism/page.rb', line 59

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

  return_yield = load_html_website(params, &)

  # 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

#secure?Boolean

Returns true if the page is secure, otherwise returns false

Returns:

  • (Boolean)


130
131
132
# File 'lib/site_prism/page.rb', line 130

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

#to_capybara_nodeObject

This scopes our calls inside Page correctly to the Capybara::Session

Returns:

  • Capybara::Session



47
48
49
# File 'lib/site_prism/page.rb', line 47

def to_capybara_node
  Capybara.current_session
end

#url(expansion = {}) ⇒ NilClass || String

Returns the templated url from the set_url property defined during the page definition Returns nil if there was not a property set (i.e. the page should not be directly loaded)

Returns:

  • (NilClass || String)


116
117
118
# File 'lib/site_prism/page.rb', line 116

def url(expansion = {})
  self.class.url && Addressable::Template.new(self.class.url).expand(expansion).to_s
end

#url_matcherRegexp

Returns the url_matcher property defined during the page definition

Returns:

  • (Regexp)


123
124
125
# File 'lib/site_prism/page.rb', line 123

def url_matcher
  self.class.url_matcher
end

#url_matches(seconds = Capybara.default_max_wait_time) ⇒ Nil || MatchData || Hash

Return the matching information of a page

Return nil if the page is not displayed correctly Return the regex matches if we have provided a regexp style url_matcher Otherwise fall back to an addressable-style template of matches

Returns:

  • (Nil || MatchData || Hash)


105
106
107
108
109
110
# File 'lib/site_prism/page.rb', line 105

def url_matches(seconds = Capybara.default_max_wait_time)
  return unless displayed?(seconds)
  return regexp_backed_matches if url_matcher.is_a?(Regexp)

  template_backed_matches
end

#wait_until_displayed(*args) ⇒ Boolean

Wait until the page is displayed according to input arguments If no url_matcher is provided we don't know how to determine if the page is displayed. So we return an error Then we wait until the url matches the expected mappings

Returns:

  • (Boolean)

Raises:



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

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

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