Sha256: 3279767dc132fdedbce81811f7e2ff1dbf30a8870b6f0ff3812d90129cb2c509

Contents?: true

Size: 1023 Bytes

Versions: 4

Compression:

Stored size: 1023 Bytes

Contents

module PageMagic
  class Element
    # class Query - executes query on capybara driver
    class Query
      # Message template for execptions raised as a result of calling method_missing
      ELEMENT_NOT_FOUND_MSG = 'Unable to find %s'.freeze

      attr_reader :args, :multiple_results

      alias multiple_results? multiple_results

      def initialize(args, multiple_results: false)
        @args = args
        @multiple_results = multiple_results
      end

      def execute(capybara_element)
        if multiple_results
          capybara_element.all(*args).to_a.tap do |result|
            raise Capybara::ElementNotFound if result.empty?
          end
        else
          capybara_element.find(*args)
        end
      rescue Capybara::Ambiguous => e
        raise AmbiguousQueryException, e.message
      rescue Capybara::ElementNotFound => e
        raise ElementMissingException, e.message
      end

      def ==(other)
        other.respond_to?(:args) && args == other.args
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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