Sha256: 6dc1d42a1b02c1a140656167e3d59ab75f6eab638dcd7685468b2dfb6fd003ce

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 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

6 entries across 6 versions & 4 rubygems

Version Path
cavalle-webrat-0.4.4.1 lib/webrat/core/locators/field_labeled_locator.rb
cavalle-webrat-0.4.4.2 lib/webrat/core/locators/field_labeled_locator.rb
dbrady-webrat-0.4.4.1 lib/webrat/core/locators/field_labeled_locator.rb
dbrady-webrat-0.4.4 lib/webrat/core/locators/field_labeled_locator.rb
diabolo-diabolo-webrat-0.4.4 lib/webrat/core/locators/field_labeled_locator.rb
diabolo-webrat-0.4.3 lib/webrat/core/locators/field_labeled_locator.rb