Sha256: 79451b77e034fd03cfe16ff31dc92572177b6de829149c053e59166e2232af61

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 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, :query_builder

    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
      @query_builder = Element::QueryBuilder.find(type)

      @options = { multiple_results: false }.merge(options)
      @element = element
    end

    # @return [Capybara::Query] query to find this element in the browser
    def build_query
      multiple_results = options.delete(:multiple_results)
      query_builder.build(selector, options, multiple_results: multiple_results)
    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

4 entries across 4 versions & 1 rubygems

Version Path
page_magic-1.2.9 lib/page_magic/element_definition_builder.rb
page_magic-1.2.8 lib/page_magic/element_definition_builder.rb
page_magic-1.2.8.alpha lib/page_magic/element_definition_builder.rb
page_magic-1.2.7 lib/page_magic/element_definition_builder.rb