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

- old
+ new

@@ -1,53 +1,66 @@ -require_relative 'find' -require_relative 'click' -require_relative 'match' +require 'capybara/dsl' +require_relative 'actions' require_relative 'exceptions' module Prickle module Capybara class Element - require 'capybara/dsl' include ::Capybara::DSL - def initialize type="*", identifier + OF_ANY_TYPE = "*" + CONVERTED_TYPES = { :link => 'a', + :paragraph => 'p' + } + + private + + def initialize type=OF_ANY_TYPE, identifier @identifier = identifier @type = type self end + def find_element + handle_exception { find_element_by 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 ElementNotFound, Error.new(@type, @identifier, @text, e).message if e.class.to_s == "Capybara::ElementNotFound" + error_message = Error.new(@type, @identifier, @text, e).message + raise ElementNotFound, error_message if e.class.to_s == "Capybara::ElementNotFound" raise end end def identifier - return @identifier.each_pair.to_a.map do |key, value| - "@#{key}='#{value}'" - end.join ' and ' + return @identifier.each_pair.to_a.map { |k, v| "@#{k}='#{v}'" }.join " and " 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) + def type + CONVERTED_TYPES[@type.to_sym] || @type end - def type_of element - Prickle::TAGS[element.to_sym] || element + def xpath + xpath = "//#{type}[#{identifier}" + xpath << " and contains(text(), '#{@text}')" if @text + xpath << "]" end - include Prickle::Actions::Find - include Prickle::Actions::Match - include Prickle::Actions::Click + public - end + include Prickle::Capybara::Actions + end end end