Sha256: 9aa294d76ef28beaa0308d8ce3c227ef01b4dd1855fe6fb60ea31ab78854bbeb

Contents?: true

Size: 1.46 KB

Versions: 38

Compression:

Stored size: 1.46 KB

Contents

require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/locator"

module Webrat
  module Locators
    
    class FieldLabeledLocator < Locator # :nodoc:
  
      def locate
        matching_labels.any? && matching_labels.detect_mapped { |label| label.field }
      end
      
      def matching_labels
        matching_label_elements.sort_by do |label_element|
          text(label_element).length
        end.map do |label_element|
          Label.load(@session, label_element)
        end
      end
      
      def matching_label_elements
        label_elements.select do |label_element|
          text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
        end
      end
      
      def label_elements
        Webrat::XML.xpath_search(@dom, Label.xpath_search)
      end
  
      def error_message
        "Could not find field labeled #{@value.inspect}"
      end
      
      def text(element)
        str = Webrat::XML.all_inner_text(element)
        str.gsub!("\n","")
        str.strip!
        str.squeeze!(" ")
        str
      end
      
    end
    
    # Locates a form field based on a <tt>label</tt> element in the HTML source.
    # This can be useful in order to verify that a field is pre-filled with the
    # correct value.
    #
    # Example:
    #   field_labeled("First name").value.should == "Bryan"
    def field_labeled(label, *field_types)
      FieldLabeledLocator.new(@session, dom, label, *field_types).locate!
    end
    
  end
end

Version data entries

38 entries across 38 versions & 11 rubygems

Version Path
adva-0.3.2 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.3.1 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.3.0 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.2.4 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.2.3 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.2.2 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.2.1 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.2.0 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.1.4 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.1.3 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.1.2 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.1.1 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.1.0 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
adva-0.0.1 test/webrat/lib/webrat/core/locators/field_labeled_locator.rb
auxesis-webrat-0.4.1 lib/webrat/core/locators/field_labeled_locator.rb
brynary-webrat-0.4.0 lib/webrat/core/locators/field_labeled_locator.rb
diabolo-webrat-0.4.2 lib/webrat/core/locators/field_labeled_locator.rb
hectoregm-webrat-0.4.2 lib/webrat/core/locators/field_labeled_locator.rb
jferris-webrat-0.4.3.0.1238640835 lib/webrat/core/locators/field_labeled_locator.rb
jferris-webrat-0.4.3 lib/webrat/core/locators/field_labeled_locator.rb