Sha256: 9cd0946c232ba4d7362488e70b383b1eb30cbd96174470c21ce51766dc364876

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

module PageMagic
  class Element
    # class Selector - models the selection criteria understood by Capybara
    class Selector
      class << self
        # Find a Selecor using it's name
        # @param [Symbol] name the name of the required Selector in snakecase format. See class constants for available
        #  selectors
        # @return [Selector] returns the predefined selector with the given name
        def find(name)
          selector = constants.find { |constant| constant.to_s.downcase == name.to_s.downcase }
          fail UnsupportedCriteriaException unless selector
          const_get(selector)
        end
      end

      # Build selector query parameters for Capybara's find method
      # @param [Symbol] element_type the type of browser element being found. e.g :link
      # @param [Hash] locator the selection method and its parameter. E.g. text: 'click me'
      def build(element_type, locator)
        [].tap do |array|
          array << element_type if supports_type
          array << name if name
          array << formatter.call(locator)
        end
      end

      attr_reader :name, :formatter, :supports_type

      def initialize(selector = nil, supports_type: false, &formatter)
        @name = selector
        @formatter = formatter || proc { |arg| arg }
        @supports_type = supports_type
      end

      XPATH = Selector.new(:xpath, supports_type: false)
      ID = Selector.new(:id, supports_type: false)
      LABEL = Selector.new(:field, supports_type: false)

      CSS = Selector.new(supports_type: false)
      TEXT = Selector.new(supports_type: true)
      NAME = Selector.new(supports_type: false) do |arg|
        "*[name='#{arg}']"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
page_magic-1.0.0.alpha21 lib/page_magic/element/selector.rb
page_magic-1.0.0.alpha20 lib/page_magic/element/selector.rb
page_magic-1.0.0.alpha19 lib/page_magic/element/selector.rb
page_magic-1.0.0.alpha18 lib/page_magic/element/selector.rb