Sha256: 0dc7d59b7c3c84522796fd061a6b88741d563726286c162f4a2a66da65186a88

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 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'

    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)

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

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

    def respond_to?(*args)
      page_element.element_definitions.keys.include?(args.first)
    end

    private

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

      if result.size == 0
        query = Capybara::Query.new(*query_args)
        fail ElementMissingException, ELEMENT_NOT_FOUND_MSG % query.description
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_magic-1.0.1 lib/page_magic/element_context.rb
page_magic-1.0.0 lib/page_magic/element_context.rb