refactored XPath
This commit is contained in:
parent
bb887c5d1d
commit
6e885bec4d
|
@ -34,12 +34,13 @@ module Capybara
|
||||||
end
|
end
|
||||||
|
|
||||||
def field(locator)
|
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
|
end
|
||||||
|
|
||||||
def fillable_field(locator)
|
def fillable_field(locator)
|
||||||
text_field(locator).password_field(locator).text_area(locator).email(locator)\
|
[:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator)) do |all, type|
|
||||||
.url(locator).search(locator).tel(locator).color(locator)
|
all.input_field(type, locator)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def content(locator)
|
def content(locator)
|
||||||
|
@ -71,13 +72,8 @@ module Capybara
|
||||||
add_field(locator, "//select")
|
add_field(locator, "//select")
|
||||||
end
|
end
|
||||||
|
|
||||||
[ :text_field, :password_field, :radio_button, :checkbox, :file_field,
|
def input_field(type, locator)
|
||||||
:email, :url, :search, :tel, :color
|
add_field(locator, "//input[@type='#{type}']")
|
||||||
].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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def scope(scope)
|
def scope(scope)
|
||||||
|
@ -96,6 +92,22 @@ module Capybara
|
||||||
XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
|
XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
def add_field(locator, field)
|
def add_field(locator, field)
|
||||||
|
|
|
@ -83,9 +83,9 @@ describe Capybara::XPath do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be chainable" do
|
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'
|
@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'
|
@driver.find(@query).first.value.should == 'seeekrit'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -220,11 +220,11 @@ describe Capybara::XPath do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
[ [:email, 'html5_email', 'Html5 Email', 'person@email.com'],
|
[ [:email_field, 'html5_email', 'Html5 Email', 'person@email.com'],
|
||||||
[:url, 'html5_url', 'Html5 Url', 'http://www.example.com'],
|
[:url_field, 'html5_url', 'Html5 Url', 'http://www.example.com'],
|
||||||
[:search, 'html5_search', 'Html5 Search', 'what are you looking for'],
|
[:search_field, 'html5_search', 'Html5 Search', 'what are you looking for'],
|
||||||
[:tel, 'html5_tel', 'Html5 Tel', '911'],
|
[:tel_field, 'html5_tel', 'Html5 Tel', '911'],
|
||||||
[:color, 'html5_color', 'Html5 Color', '#FFF']].each do |method, id, label, output|
|
[:color_field, 'html5_color', 'Html5 Color', '#FFF']].each do |method, id, label, output|
|
||||||
describe "##{method}" do
|
describe "##{method}" do
|
||||||
it "should find a file field by label" do
|
it "should find a file field by label" do
|
||||||
@query = @xpath.send(method, label).to_s
|
@query = @xpath.send(method, label).to_s
|
||||||
|
|
Loading…
Reference in New Issue