lib/page-object/elements/element.rb in page-object-0.2 vs lib/page-object/elements/element.rb in page-object-0.2.1

- old
+ new

@@ -1,22 +1,21 @@ - module PageObject module Elements # # Contains functionality that is common across all elements. # # @see PageObject::Platforms::WatirElement for the Watir version of all common methods # @see PageObject::Platforms::SeleniumElement for the Selenium version of all common methods # class Element attr_reader :element - + def initialize(element, platform) @element = element include_platform_for platform end - + # @private def self.watir_identifier_for identifier if should_build_watir_xpath(identifier) how = :xpath what = build_xpath_for(identifier) @@ -28,11 +27,11 @@ ident = identifier_for each, watir_finders, watir_mapping all_identities[ident.keys.first] = ident.values.first end all_identities end - + # @private def self.selenium_identifier_for identifier if identifier.length == 1 identifier = identifier_for identifier, selenium_finders, selenium_mapping return identifier.keys.first, identifier.values.first @@ -42,11 +41,11 @@ return how, what end end protected - + def self.should_build_watir_xpath identifier ['table', 'span', 'div', 'td', 'li', 'ul', 'ol'].include? identifier[:tag_name] and identifier[:name] end def self.build_xpath_for identifier @@ -56,37 +55,37 @@ xpath = ".//#{tag_locator}" xpath << "[#{attribute_expression(identifier)}]" unless identifier.empty? xpath << "[#{idx+1}]" if idx xpath end - + def self.attribute_expression(identifier) identifier.map do |key, value| if value.kind_of?(Array) "(" + value.map { |v| equal_pair(key, v) }.join(" or ") + ")" else equal_pair(key, value) end end.join(" and ") end - + def self.equal_pair(key, value) if key == :label "@id=//label[normalize-space()=#{xpath_string(value)}]/@for" else "#{lhs_for(key)}=#{xpath_string(value)}" end end - + def self.lhs_for(key) case key - when :text, 'text' - 'normalize-space()' - when :href - 'normalize-space(@href)' - else - "@#{key.to_s.gsub("_", "-")}" + when :text, 'text' + 'normalize-space()' + when :href + 'normalize-space(@href)' + else + "@#{key.to_s.gsub("_", "-")}" end end def self.xpath_string(value) if value.include? "'" @@ -102,35 +101,35 @@ how, what = identifier.keys.first, identifier.values.first return how => what if find_by.include? how return find_by_mapping[how] => what if find_by_mapping[how] return nil => what end - + def self.watir_finders [:class, :id, :index, :name, :xpath] end - - def self.watir_mapping - {} + + def self.watir_mapping + {} end - + def self.selenium_finders [:class, :id, :name, :xpath, :index] end - - def self.selenium_mapping - {} + + def self.selenium_mapping + {} end - + def include_platform_for platform if platform[:platform] == :watir - require 'page-object/platforms/watir_element' - self.class.send :include, PageObject::Platforms::WatirElement + require 'page-object/platforms/watir/element' + self.class.send :include, PageObject::Platforms::Watir::Element elsif platform[:platform] == :selenium - require 'page-object/platforms/selenium_element' - self.class.send :include, PageObject::Platforms::SeleniumElement + require 'page-object/platforms/selenium/element' + self.class.send :include, PageObject::Platforms::Selenium::Element else - raise ArgumentError, "expect platform to be :watir or :selenium" + raise ArgumentError, "expect platform to be :watir or :selenium" end end end end end