Sha256: d3a04d6b8d25998a6b3b02e2b65f63ef73938eac6cc68e7596857ded1a20adf5

Contents?: true

Size: 1.39 KB

Versions: 30

Compression:

Stored size: 1.39 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|\Z)/i
        end
      end

      def label_elements
        @dom.xpath(*Label.xpath_search)
      end

      def error_message
        "Could not find field labeled #{@value.inspect}"
      end

      def text(element)
        str = element.inner_text
        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

30 entries across 30 versions & 15 rubygems

Version Path
diabolo-webrat-0.5.1 lib/webrat/core/locators/field_labeled_locator.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/webrat-0.7.3/lib/webrat/core/locators/field_labeled_locator.rb
indirect-webrat-0.7.5 lib/webrat/core/locators/field_labeled_locator.rb
indirect-webrat-0.7.4 lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.3 lib/webrat/core/locators/field_labeled_locator.rb
jbd-webrat-0.7.2.rails3 lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.2 lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.2.beta.2 lib/webrat/core/locators/field_labeled_locator.rb
mutle-webrat-0.7.2.beta.1 lib/webrat/core/locators/field_labeled_locator.rb
thoughtbot-webrat-0.7.2.pre lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.2.beta.1 lib/webrat/core/locators/field_labeled_locator.rb
honkster-webrat-0.7.1.2 lib/webrat/core/locators/field_labeled_locator.rb
honkster-webrat-0.7.1.1 lib/webrat/core/locators/field_labeled_locator.rb
revo-webrat-0.7.0.1 lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.1 lib/webrat/core/locators/field_labeled_locator.rb
davidtrogers-webrat-0.7.0 lib/webrat/core/locators/field_labeled_locator.rb
revo-webrat-0.7.0 lib/webrat/core/locators/field_labeled_locator.rb
revo-webrat-0.7.1.pre lib/webrat/core/locators/field_labeled_locator.rb
webrat-0.7.0 lib/webrat/core/locators/field_labeled_locator.rb
honkster-webrat-0.6.0.10 lib/webrat/core/locators/field_labeled_locator.rb