Module: Browsery::Utils::Castable
- Defined in:
- lib/browsery/utils/castable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ void
Extend the base class in which this module is included in order to inject class methods.
Instance Method Summary collapse
-
#cast(name) ⇒ Base
The preferred way to create a new page object from the current page’s driver state.
-
#cast_any(*names) ⇒ Base?
Cast the page to any of the listed ‘names`, in order of specification.
Class Method Details
.included(base) ⇒ void
This method returns an undefined value.
Extend the base class in which this module is included in order to inject class methods.
53 54 55 |
# File 'lib/browsery/utils/castable.rb', line 53 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#cast(name) ⇒ Base
The preferred way to create a new page object from the current page’s driver state. Raises a NameError if the page could not be found. If casting causes a StaleElementReferenceError, the method will retry up to 2 more times.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/browsery/utils/castable.rb', line 66 def cast(name) tries ||= 3 self.class.cast(@driver, name).tap do |new_page| self.freeze Browsery.logger.debug("Casting #{self.class}(##{self.object_id}) into #{new_page.class}(##{new_page.object_id})") end rescue Selenium::WebDriver::Error::StaleElementReferenceError => sere Browsery.logger.debug("#{self.class}(##{@driver.object_id})->cast(#{name}) raised a potentially-swallowed StaleElementReferenceError") sleep 1 retry unless (tries -= 1).zero? end |
#cast_any(*names) ⇒ Base?
Cast the page to any of the listed ‘names`, in order of specification. Returns the first page that accepts the casting, or returns nil, rather than raising InvalidPageState.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/browsery/utils/castable.rb', line 85 def cast_any(*names) # Try one at a time, swallowing InvalidPageState exceptions names.each do |name| begin return self.cast(name) rescue InvalidPageState # noop end end # Return nil otherwise return nil end |