Sha256: 65c6acb7ce684a1c509224654f00dd85309d6c51a9481228960ce0a9ba8e32cc

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

module PageMagic
  # Builder for creating ElementDefinitions
  class ElementDefinitionBuilder
    INVALID_SELECTOR_MSG = 'Pass a locator/define one on the class'.freeze
    attr_reader :definition_class, :options, :selector, :type, :element

    def initialize(definition_class:, selector:, type:, options: {}, element: nil)
      unless element
        selector ||= definition_class.selector
        raise UndefinedSelectorException, INVALID_SELECTOR_MSG if selector.nil? || selector.empty?
      end

      @definition_class = definition_class
      @selector = selector
      @type = type
      @options = options
      @element = element
    end

    # @return [Capybara::Query] query to find this element in the browser
    def build_query
      Element::Query.find(type).build(selector, options)
    end

    # Create new instance of the ElementDefinition modeled by this builder
    # @param [Object] browser_element capybara browser element corresponding to the element modelled by this builder
    # @return [Element] element definition
    def build(browser_element)
      definition_class.new(browser_element)
    end

    def ==(other)
      return false unless other.is_a?(ElementDefinitionBuilder)
      this = [options, selector, type, element, definition_class]
      this == [other.options, other.selector, other.type, other.element, other.definition_class]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
page_magic-1.2.6 lib/page_magic/element_definition_builder.rb
page_magic-1.2.5 lib/page_magic/element_definition_builder.rb
page_magic-1.2.5.alpha1 lib/page_magic/element_definition_builder.rb
page_magic-1.2.4 lib/page_magic/element_definition_builder.rb
page_magic-1.2.3 lib/page_magic/element_definition_builder.rb