Module: MiniAutobot::Utils::Castable

Included in:
PageObjects::Base, PageObjects::Overlay::Base, PageObjects::Widgets::Base
Defined in:
lib/mini_autobot/utils/castable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • base (Class)


53
54
55
# File 'lib/mini_autobot/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.

Parameters:

  • name (String)

    see Base.cast

Returns:

  • (Base)

    The casted page object.

Raises:

  • InvalidPageState if the page cannot be casted to

  • NameError if the page object doesn’t exist



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mini_autobot/utils/castable.rb', line 66

def cast(name)
  tries ||= 3
  self.class.cast(@driver, name).tap do |new_page|
    self.freeze
    MiniAutobot.logger.debug("Casting #{self.class}(##{self.object_id}) into #{new_page.class}(##{new_page.object_id})")
  end
rescue Selenium::WebDriver::Error::StaleElementReferenceError => sere
  MiniAutobot.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.

Parameters:

  • names (Enumerable<String>)

    see Base.cast

Returns:

  • (Base, nil)

    the casted page object, if successful; nil otherwise.

Raises:

  • NameError if the page object doesn’t exist



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mini_autobot/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