Module: MiniAutobot::Utils::Castable::ClassMethods
- Defined in:
- lib/mini_autobot/utils/castable.rb
Instance Method Summary collapse
-
#cast(driver, name) ⇒ Base
Attempts to create a new page object from a driver state.
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.
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.}\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.}" end instance end |