Sha256: fad89e94741e260e75206de81bfda222fbe55b5ac89c84d95b3dcafeab484750

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

module Screengem
  #
  # Singleton that gives the ability to get references to screen elements.
  #
  # On initialization generate a factory method for each screen element. If the screen element
  # defines a visit_path method then we wrap the screen element to support auto visit.
  #
  # Examples:
  #
  #   def login_page
  #     ScreenElements::LoginPage.new
  #   end
  #
  #   def login_page
  #     Screengem::AutomaticVisit.new(ScreenElements::LoginPage.new)
  #   end
  #
  class ScreenElements
    include ::Singleton

    def initialize
      generated_method_names = []

      Screengem::ScreenElement.descendants.each do |screen_element_class|
        class_name = screen_element_class.name

        # Skip any anonymous classes. We can't do anything with them. Probably they're from testing.
        next if class_name.blank?

        method_name = class_name.demodulize.underscore

        # Create some safety by detecting and failing fast for duplicate creation method names
        if generated_method_names.include?(method_name)
          raise "Name collision: two screen elements resolve to '#{method_name}'."
        end

        generated_method_names << method_name

        if auto_visit?(screen_element_class)
          class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
            def #{method_name}(*args)
              Screengem::AutomaticVisit.new(#{class_name}.new(*args))
            end
          RUBY
        else
          class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
            def #{method_name}(*args)
              #{class_name}.new(*args)
            end
          RUBY
        end
      end
    end

    private

    def auto_visit?(screen_element)
      screen_element.instance_methods(false).include?(:visit_path)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
screengem-0.8.1 lib/screengem/screen_elements.rb
screengem-0.8.0 lib/screengem/screen_elements.rb
screengem-0.7.3 lib/screengem/screen_elements.rb
screengem-0.7.2 lib/screengem/screen_elements.rb
screengem-0.7.1 lib/screengem/screen_elements.rb