Starting on field_with_xpath implementation

This commit is contained in:
Bryan Helmkamp 2008-11-24 14:15:28 -05:00
parent fe8a8cf435
commit 5502948417
7 changed files with 37 additions and 2 deletions

View File

@ -41,6 +41,10 @@ module Webrat
@element["id"] @element["id"]
end end
def path
@element.path
end
def matches_id?(id) def matches_id?(id)
if id.is_a?(Regexp) if id.is_a?(Regexp)
@element["id"] =~ id @element["id"] =~ id

View File

@ -18,6 +18,10 @@ module Webrat
nil nil
end end
def field_by_element(element)
fields.detect { |possible_field| possible_field.path == element.path }
end
def find_select_option(option_text) def find_select_option(option_text)
select_fields = fields_by_type([SelectField]) select_fields = fields_by_type([SelectField])

View File

@ -3,6 +3,14 @@ require "webrat/core_extensions/detect_mapped"
module Webrat module Webrat
module Locators module Locators
def field_by_xpath(xpath)
element = dom.at(xpath)
forms.detect_mapped do |form|
form.field_by_element(element)
end
end
def field(*args) def field(*args)
# This is the default locator strategy # This is the default locator strategy
find_field_with_id(*args) || find_field_with_id(*args) ||

View File

@ -46,7 +46,8 @@ module Webrat
:request_page, :current_dom, :request_page, :current_dom,
:selects_date, :selects_time, :selects_datetime, :selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime, :select_date, :select_time, :select_datetime,
:wait_for_page_to_load :wait_for_page_to_load,
:field_by_xpath
end end

View File

@ -216,6 +216,7 @@ module Webrat
def_delegators :current_scope, :should_see def_delegators :current_scope, :should_see
def_delegators :current_scope, :should_not_see def_delegators :current_scope, :should_not_see
def_delegators :current_scope, :field_labeled def_delegators :current_scope, :field_labeled
def_delegators :current_scope, :field_by_xpath
private private
# accessor for testing # accessor for testing

View File

@ -0,0 +1,17 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe "field_by_xpath" do
it "should work" do
with_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<input type="text" id="element_42">
</form>
</html>
HTML
field_by_xpath(".//input").id.should == "element_42"
end
end

View File

@ -1,4 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe "field_labeled" do describe "field_labeled" do