Class: Gamera::Page
- Inherits:
-
Object
- Object
- Gamera::Page
- Includes:
- Capybara::DSL
- Defined in:
- lib/gamera/page.rb
Overview
This is a base class which implements common methods for page object classes.
You can use this to create a Ruby class which wraps a web page, providing an API for automating elements or processes on the page
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#url_matcher ⇒ Object
readonly
Returns the value of attribute url_matcher.
Instance Method Summary collapse
-
#displayed?(wait_time_seconds = Capybara.default_wait_time) ⇒ Boolean
Check to see if we eventually land on the right page.
-
#initialize(url, url_matcher = nil) ⇒ Page
constructor
A new instance of Page.
-
#path_join(*elements) ⇒ String
This is a utility method to clean up URLs formed by concatenation since we sometimes ended up with “//” in the middle of URLs which broke the url_matcher checks.
-
#sparse? ⇒ Boolean
This is a flag for tracking which page object classes don’t cover all of the elements and/or controls on the target web page.
-
#visit(fail_on_redirect = true) ⇒ Object
Open the page url in the browser specified in your Capybara configuration.
-
#with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError], &block) ⇒ Object
A method to wait for slow loading data on a page.
Constructor Details
#initialize(url, url_matcher = nil) ⇒ Page
Returns a new instance of Page.
137 138 139 140 |
# File 'lib/gamera/page.rb', line 137 def initialize(url, url_matcher = nil) @url = url @url_matcher = url_matcher end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
135 136 137 |
# File 'lib/gamera/page.rb', line 135 def url @url end |
#url_matcher ⇒ Object (readonly)
Returns the value of attribute url_matcher.
135 136 137 |
# File 'lib/gamera/page.rb', line 135 def url_matcher @url_matcher end |
Instance Method Details
#displayed?(wait_time_seconds = Capybara.default_wait_time) ⇒ Boolean
Check to see if we eventually land on the right page
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gamera/page.rb', line 158 def displayed?(wait_time_seconds = Capybara.default_wait_time) fail Gamera::NoUrlMatcherForPage if url_matcher.nil? start_time = Time.now loop do return true if page.current_url.chomp('/') =~ url_matcher break unless Time.now - start_time <= wait_time_seconds sleep(0.05) end false end |
#path_join(*elements) ⇒ String
This is a utility method to clean up URLs formed by concatenation since we sometimes ended up with “//” in the middle of URLs which broke the url_matcher checks.
201 202 203 |
# File 'lib/gamera/page.rb', line 201 def path_join(*elements) "/#{elements.join('/')}".gsub(%r(//+), '/') end |
#sparse? ⇒ Boolean
This is a flag for tracking which page object classes don’t cover all of the elements and/or controls on the target web page.
191 192 193 |
# File 'lib/gamera/page.rb', line 191 def sparse? false end |
#visit(fail_on_redirect = true) ⇒ Object
Open the page url in the browser specified in your Capybara configuration
146 147 148 149 150 151 |
# File 'lib/gamera/page.rb', line 146 def visit(fail_on_redirect = true) super(url) if fail_on_redirect fail WrongPageVisited, "Expected URL '#{url}', got '#{page.current_url}'" unless displayed? end end |
#with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError], &block) ⇒ Object
A method to wait for slow loading data on a page. Useful, for example, when waiting on a page that shows the count of records loaded via a slow web or import.
176 177 178 179 180 181 182 183 184 |
# File 'lib/gamera/page.rb', line 176 def with_refreshes(retries, allowed_errors = [RSpec::Expectations::ExpectationNotMetError], &block) retries_left ||= retries visit block.call(retries_left) rescue *allowed_errors => e retries_left -= 1 retry if retries_left >= 0 raise e end |