Added has_checked_field? and has_unchecked_field?

This commit is contained in:
Jonas Nicklas 2010-01-18 22:31:22 +01:00
parent fe558b2f01
commit 2cb0548bfe
3 changed files with 61 additions and 19 deletions

View File

@ -193,6 +193,14 @@ module Capybara
has_no_xpath?(XPath.field(locator, options))
end
def has_checked_field?(locator)
has_xpath?(XPath.field(locator, :checked => true))
end
def has_unchecked_field?(locator)
has_xpath?(XPath.field(locator, :unchecked => true))
end
def save_and_open_page
require 'capybara/save_and_open_page'
Capybara::SaveAndOpenPage.save_and_open_page(body)

View File

@ -37,12 +37,16 @@ module Capybara
if options[:with]
fillable_field(locator, options)
else
fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator)
xpath = fillable_field(locator)
xpath = xpath.input_field(:file, locator, options)
xpath = xpath.checkbox(locator, options)
xpath = xpath.radio_button(locator, options)
xpath.select(locator, options)
end
end
def fillable_field(locator, options={})
[:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator)) do |all, type|
[:text, :password, :email, :url, :search, :tel, :color].inject(text_area(locator, options)) do |all, type|
all.input_field(type, locator, options)
end
end
@ -71,12 +75,12 @@ module Capybara
xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end
def text_area(locator)
add_field(locator, "//textarea")
def text_area(locator, options)
add_field(locator, "//textarea", options)
end
def select(locator)
add_field(locator, "//select")
def select(locator, options={})
add_field(locator, "//select", options)
end
def input_field(type, locator, options={})
@ -99,12 +103,12 @@ module Capybara
XPath.new(*[XPath.wrap(path).paths, @paths].flatten)
end
def checkbox(locator)
input_field(:checkbox, locator)
def checkbox(locator, options={})
input_field(:checkbox, locator, options)
end
def radio_button(locator)
input_field(:radio, locator)
def radio_button(locator, options={})
input_field(:radio, locator, options)
end
[:text, :password, :email, :url, :search, :tel, :color, :file].each do |type|
@ -129,7 +133,9 @@ module Capybara
def extract_postfix(options)
options.inject("") do |postfix, (key, value)|
case key
when :with then postfix += "[@value=#{s(value)}]"
when :with then postfix += "[@value=#{s(value)}]"
when :checked then postfix += "[@checked]"
when :unchecked then postfix += "[not(@checked)]"
end
postfix
end

View File

@ -1,9 +1,9 @@
shared_examples_for "has_field" do
describe '#has_field' do
before do
@session.visit('/form')
end
before do
@session.visit('/form')
end
describe '#has_field' do
it "should be true if the field is on the page" do
@session.should have_field('Dog')
@session.should have_field('form_description')
@ -29,10 +29,6 @@ shared_examples_for "has_field" do
end
describe '#has_no_field' do
before do
@session.visit('/form')
end
it "should be false if the field is on the page" do
@session.should_not have_no_field('Dog')
@session.should_not have_no_field('form_description')
@ -56,5 +52,37 @@ shared_examples_for "has_field" do
end
end
end
describe '#has_checked_field?' do
it "should be true if a checked field is on the page" do
@session.should have_checked_field('gender_female')
@session.should have_checked_field('Hamster')
end
it "should be false if an unchecked field is on the page" do
@session.should_not have_checked_field('form_pets_cat')
@session.should_not have_checked_field('Male')
end
it "should be false if no field is on the page" do
@session.should_not have_checked_field('Does Not Exist')
end
end
describe '#has_unchecked_field?' do
it "should be false if a checked field is on the page" do
@session.should_not have_unchecked_field('gender_female')
@session.should_not have_unchecked_field('Hamster')
end
it "should be true if an unchecked field is on the page" do
@session.should have_unchecked_field('form_pets_cat')
@session.should have_unchecked_field('Male')
end
it "should be false if no field is on the page" do
@session.should_not have_unchecked_field('Does Not Exist')
end
end
end