Sha256: f3f174ba416a9c42e9279f94aed17d4638fa19cbc438825aae52af074eaaba31

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 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) 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) }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
page_magic-1.2.1 lib/page_magic/element_context.rb
page_magic-1.2.0 lib/page_magic/element_context.rb
page_magic-1.1.0 lib/page_magic/element_context.rb
page_magic-1.0.4 lib/page_magic/element_context.rb
page_magic-1.0.3 lib/page_magic/element_context.rb
page_magic-1.0.2 lib/page_magic/element_context.rb