module Browsery::Utils::Castable

Public Class Methods

included(base) click to toggle source

Extend the base class in which this module is included in order to inject class methods.

@param base [Class] @return [void]

# File lib/browsery/utils/castable.rb, line 53
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

cast(name) click to toggle source

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.

@param name [String] see {Base.cast} @return [Base] The casted page object. @raise InvalidPageState if the page cannot be casted to @raise NameError if the page object doesn't exist

# 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) click to toggle source

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.

@param names [Enumerable<String>] see {Base.cast} @return [Base, nil] the casted page object, if successful; nil otherwise. @raise NameError if the page object doesn't exist

# 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