Sha256: 9d2851345913a80d38d96c201504e351e1e5f4f202b9a1402b274f5a36a3b7df

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module PageMagic
  # class ElementContext - resolves which element definition to use when accessing the browser.
  class ElementContext
    # Message template for execptions raised as a result of calling method_missing
    ELEMENT_NOT_FOUND_MSG = 'Unable to find %s'.freeze

    attr_reader :page_element

    def initialize(page_element)
      @page_element = page_element
    end

    # acts as proxy to element defintions defined on @page_element
    # @return [Object] result of callng method on page_element
    # @return [Element] animated page element containing located browser element
    # @return [Array<Element>] array of elements if more that one result was found the browser
    def method_missing(method, *args, &block)
      return page_element.send(method, *args, &block) if page_element.methods.include?(method)

      builder = page_element.element_by_name(method, *args)

      super unless builder

      prefecteched_element = builder.element
      return builder.build(prefecteched_element) if prefecteched_element

      elements = find(builder)
      elements.size == 1 ? elements.first : elements
    end

    def respond_to?(*args)
      page_element.respond_to?(*args) || super
    end

    private

    def find(builder)
      query_args = builder.build_query
      result = page_element.browser_element.all(*query_args)

      if result.empty?
        query = Capybara::Query.new(*query_args)
        raise ElementMissingException, ELEMENT_NOT_FOUND_MSG % query.description
      end

      result.to_a.collect { |e| builder.build(e) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
page_magic-1.2.6 lib/page_magic/element_context.rb
page_magic-1.2.5 lib/page_magic/element_context.rb
page_magic-1.2.5.alpha1 lib/page_magic/element_context.rb