refactored XPath

This commit is contained in:
Jonas Nicklas 2010-01-11 20:36:05 +01:00
parent bb887c5d1d
commit 6e885bec4d
2 changed files with 29 additions and 17 deletions

View File

@ -34,12 +34,13 @@ module Capybara
end
def field(locator)
fillable_field(locator).file_field(locator).checkbox(locator).radio_button(locator).select(locator)
fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator)
end
def fillable_field(locator)
text_field(locator).password_field(locator).text_area(locator).email(locator)\
.url(locator).search(locator).tel(locator).color(locator)
[:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator)) do |all, type|
all.input_field(type, locator)
end
end
def content(locator)
@ -71,13 +72,8 @@ module Capybara
add_field(locator, "//select")
end
[ :text_field, :password_field, :radio_button, :checkbox, :file_field,
:email, :url, :search, :tel, :color
].each do |input_type|
define_method(input_type) do |locator|
input_type = input_type.to_s.gsub("_field", "").gsub("_button", "")
add_field(locator, "//input[@type='#{input_type}']")
end
def input_field(type, locator)
add_field(locator, "//input[@type='#{type}']")
end
def scope(scope)
@ -96,6 +92,22 @@ module Capybara
XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
end
def checkbox(locator)
input_field(:checkbox, locator)
end
def radio_button(locator)
input_field(:radio, locator)
end
[:text, :password, :email, :url, :search, :tel, :color, :file].each do |type|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{type}_field(locator)
input_field(:#{type}, locator)
end
RUBY
end
protected
def add_field(locator, field)

View File

@ -83,9 +83,9 @@ describe Capybara::XPath do
end
it "should be chainable" do
@query = @xpath.field('First Name').password_field('First Name').to_s
@query = @xpath.field('First Name').input_field(:password, 'First Name').to_s
@driver.find(@query).first.value.should == 'John'
@query = @xpath.field('Password').password_field('Password').to_s
@query = @xpath.field('Password').input_field(:password, 'Password').to_s
@driver.find(@query).first.value.should == 'seeekrit'
end
end
@ -220,11 +220,11 @@ describe Capybara::XPath do
end
end
[ [:email, 'html5_email', 'Html5 Email', 'person@email.com'],
[:url, 'html5_url', 'Html5 Url', 'http://www.example.com'],
[:search, 'html5_search', 'Html5 Search', 'what are you looking for'],
[:tel, 'html5_tel', 'Html5 Tel', '911'],
[:color, 'html5_color', 'Html5 Color', '#FFF']].each do |method, id, label, output|
[ [:email_field, 'html5_email', 'Html5 Email', 'person@email.com'],
[:url_field, 'html5_url', 'Html5 Url', 'http://www.example.com'],
[:search_field, 'html5_search', 'Html5 Search', 'what are you looking for'],
[:tel_field, 'html5_tel', 'Html5 Tel', '911'],
[:color_field, 'html5_color', 'Html5 Color', '#FFF']].each do |method, id, label, output|
describe "##{method}" do
it "should find a file field by label" do
@query = @xpath.send(method, label).to_s