Skip to content

Commit

Permalink
Extract FieldByIdLocator object
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent 9e030a1 commit 04959ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 2 additions & 10 deletions lib/webrat/core/locators.rb
Expand Up @@ -68,17 +68,9 @@ def link_by_element(element)
end

def find_field_with_id(id, *field_types) #:nodoc:
field_elements = Webrat::XML.xpath_search(dom, *Field.xpath_search)
require "webrat/core/locators/field_by_id_locator"

field_element = field_elements.detect do |field_element|
if id.is_a?(Regexp)
Webrat::XML.attribute(field_element, "id") =~ id
else
Webrat::XML.attribute(field_element, "id") == id.to_s
end
end

field_by_element(field_element)
FieldByIdLocator.new(self, id).locate
end

def find_select_option(option_text, id_or_name_or_label) #:nodoc:
Expand Down
26 changes: 26 additions & 0 deletions lib/webrat/core/locators/field_by_id_locator.rb
@@ -0,0 +1,26 @@
class FieldByIdLocator

def initialize(scope, value)
@scope = scope
@value = value
end

def locate
@scope.field_by_element(field_element)
end

def field_element
field_elements.detect do |field_element|
if @value.is_a?(Regexp)
Webrat::XML.attribute(field_element, "id") =~ @value
else
Webrat::XML.attribute(field_element, "id") == @value.to_s
end
end
end

def field_elements
Webrat::XML.xpath_search(@scope.dom, *Webrat::Field.xpath_search)
end

end

0 comments on commit 04959ae

Please sign in to comment.