Module: MiniAutobot::Utils::Castable::ClassMethods

Defined in:
lib/mini_autobot/utils/castable.rb

Instance Method Summary collapse

Instance Method Details

#cast(driver, name) ⇒ Base

Attempts to create a new page object from a driver state. Use the instance method for convenience. Raises ‘NameError` if the page could not be found.

Parameters:

  • driver (Selenium::WebDriver)

    The instance of the current WebDriver.

  • name (#to_s)

    The name of the page object to instantiate.

Returns:

  • (Base)

    A subclass of ‘Base` representing the page object.

Raises:

  • InvalidPageState if the page cannot be casted to

  • NameError if the page object doesn’t exist



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mini_autobot/utils/castable.rb', line 19

def cast(driver, name)
  # Transform the name string into a file path and then into a module name
  klass_name = "mini_autobot/page_objects/#{name}".camelize

  # Attempt to load the class
  klass = begin
    klass_name.constantize
  rescue => exc
    msg = ""
    msg << "Cannot find page object '#{name}', "
    msg << "because could not load class '#{klass_name}' "
    msg << "with underlying error:\n  #{exc.class}: #{exc.message}\n"
    msg << exc.backtrace.map { |str| "    #{str}" }.join("\n")
    raise NameError, msg
  end

  # Instantiate the class, passing the driver automatically, and
  # validates to ensure the driver is in the correct state
  instance = klass.new(driver)
  begin
    instance.validate!
  rescue Minitest::Assertion => exc
    raise MiniAutobot::PageObjects::InvalidePageState, "#{klass}: #{exc.message}"
  end
  instance
end