Class: Webrat::Locators::FieldLabeledLocator

Inherits:
Locator show all
Defined in:
lib/webrat/core/locators/field_labeled_locator.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Locator

#initialize, #locate!

Constructor Details

This class inherits a constructor from Webrat::Locators::Locator

Instance Method Details

#error_messageObject



31
32
33
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 31

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

#label_elementsObject



27
28
29
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 27

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

#locateObject



9
10
11
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 9

def locate
  matching_labels.any? && matching_labels.detect_mapped { |label| label.field }
end

#matching_label_elementsObject



21
22
23
24
25
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 21

def matching_label_elements
  label_elements.select do |label_element|
    text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}(\b|\Z)/i
  end
end

#matching_labelsObject



13
14
15
16
17
18
19
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 13

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

#text(element) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/webrat/core/locators/field_labeled_locator.rb', line 35

def text(element)
  str = element.inner_text
  str.gsub!("\n","")
  str.strip!
  str.squeeze!(" ")
  str
end