Sha256: d87d2f678e7999f27013bf5734b3c9ba1677ea230f1072ba116aa95e41a7f090

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'capybara/dsl'
require_relative 'actions'

module Prickle
  module Capybara
    class Element

      include ::Capybara::DSL

      OF_ANY_TYPE = "*"
      CONVERTED_TYPES = { :link => 'a',
                          :paragraph => 'p'
                        }

      private

      def initialize type=OF_ANY_TYPE, identifier
        @identifier = identifier
        @type = type
        self
      end

      def identifier
        @identifier.each_pair.inject([]) do |xpath_str, (key, value)|
          xpath_str << convert_to_xpath(key, value)
        end.join " and "
      end

      def convert_to_xpath key, value
        if key.to_s.include? "<value>"
          key.sub "<value>", value
        else
          "@#{key}='#{value}'"
        end
      end

      def type
        CONVERTED_TYPES[@type.to_sym] || @type
      end

      def find_element
        handle_exception { find_element_by_xpath }
      end

      def xpath
        xpath = "//#{type}[#{identifier}"
        xpath << " and contains(text(), '#{@text}')" if @text
        xpath << "]"
      end

      def find_element_by_xpath
        wait_until(Prickle::Capybara.wait_time) do
          find(:xpath, xpath).visible?
        end unless Prickle::Capybara.wait_time.nil?

        find(:xpath, xpath)
      end

      def handle_exception &block
        begin
          block.call
        rescue Exception => e
          raise element_not_found(e) if e.class == ::Capybara::ElementNotFound
          raise
        end
      end

      def element_not_found caught_exception
        Exceptions::ElementNotFound.new(@type, identifier, @text, caught_exception)
      end

      public

      include Prickle::Capybara::Actions

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prickle-0.0.4 lib/prickle/capybara/element.rb