From 6e885bec4da13c8afa9ec3553243bf53cbbc012f Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Mon, 11 Jan 2010 20:36:05 +0100 Subject: [PATCH] refactored XPath --- lib/capybara/xpath.rb | 32 ++++++++++++++++++++++---------- spec/xpath_spec.rb | 14 +++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/capybara/xpath.rb b/lib/capybara/xpath.rb index aa2f4cdc..7b45785a 100644 --- a/lib/capybara/xpath.rb +++ b/lib/capybara/xpath.rb @@ -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) diff --git a/spec/xpath_spec.rb b/spec/xpath_spec.rb index dfc6e821..937901b3 100644 --- a/spec/xpath_spec.rb +++ b/spec/xpath_spec.rb @@ -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