lib/prickle/capybara/element.rb in prickle-0.0.3 vs lib/prickle/capybara/element.rb in prickle-0.0.4

- old
+ new

@@ -1,8 +1,7 @@ require 'capybara/dsl' require_relative 'actions' -require_relative 'exceptions' module Prickle module Capybara class Element @@ -19,15 +18,39 @@ @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 } + handle_exception { find_element_by_xpath } end - def find_element_by xpath + 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) @@ -35,27 +58,16 @@ def handle_exception &block begin block.call rescue Exception => e - error_message = Error.new(@type, @identifier, @text, e).message - raise ElementNotFound, error_message if e.class.to_s == "Capybara::ElementNotFound" + raise element_not_found(e) if e.class == ::Capybara::ElementNotFound raise end end - def identifier - return @identifier.each_pair.to_a.map { |k, v| "@#{k}='#{v}'" }.join " and " - end - - def type - CONVERTED_TYPES[@type.to_sym] || @type - end - - def xpath - xpath = "//#{type}[#{identifier}" - xpath << " and contains(text(), '#{@text}')" if @text - xpath << "]" + def element_not_found caught_exception + Exceptions::ElementNotFound.new(@type, identifier, @text, caught_exception) end public include Prickle::Capybara::Actions