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 or the `@page` that has been loaded in-line

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_missing, #elements_present

Class Attribute Details

.urlObject (readonly)

Returns the value of attribute url.



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

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)


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

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)


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

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)


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

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)


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

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/site_prism/page.rb', line 61

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

#secure?Boolean

Returns true if the page is secure, otherwise returns false

Returns:

  • (Boolean)


136
137
138
# File 'lib/site_prism/page.rb', line 136

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

#to_capybara_nodeCapybara::Node::Simple || Capybara::Session

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

Returns:

  • (Capybara::Node::Simple || Capybara::Session)


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

def to_capybara_node
  (defined?(@page) && @page) || 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)


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

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)


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

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)


111
112
113
114
115
116
# File 'lib/site_prism/page.rb', line 111

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:



96
97
98
99
100
101
102
# File 'lib/site_prism/page.rb', line 96

def wait_until_displayed(*args)
  raise SitePrism::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