1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Added has_link? and has_field?

This commit is contained in:
Jonas Nicklas 2010-01-18 21:28:06 +01:00
parent 2e432b82e7
commit 5150ad429f
5 changed files with 137 additions and 12 deletions

View file

@ -169,6 +169,22 @@ module Capybara
has_no_xpath?(XPath.content(content)) has_no_xpath?(XPath.content(content))
end end
def has_link?(locator)
has_xpath?(XPath.link(locator))
end
def has_no_link?(locator)
has_no_xpath?(XPath.link(locator))
end
def has_field?(locator, options={})
has_xpath?(XPath.field(locator, options))
end
def has_no_field?(locator, options={})
has_no_xpath?(XPath.field(locator, options))
end
def save_and_open_page def save_and_open_page
require 'capybara/save_and_open_page' require 'capybara/save_and_open_page'
Capybara::SaveAndOpenPage.save_and_open_page(body) Capybara::SaveAndOpenPage.save_and_open_page(body)

View file

@ -33,13 +33,17 @@ module Capybara
@paths = paths @paths = paths
end end
def field(locator) def field(locator, options={})
fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator) if options[:with]
fillable_field(locator, options)
else
fillable_field(locator).input_field(:file, locator).checkbox(locator).radio_button(locator).select(locator)
end
end end
def fillable_field(locator) 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)) do |all, type|
all.input_field(type, locator) all.input_field(type, locator, options)
end end
end end
@ -75,8 +79,8 @@ module Capybara
add_field(locator, "//select") add_field(locator, "//select")
end end
def input_field(type, locator) def input_field(type, locator, options={})
add_field(locator, "//input[@type='#{type}']") add_field(locator, "//input[@type='#{type}']", options)
end end
def scope(scope) def scope(scope)
@ -113,12 +117,22 @@ module Capybara
protected protected
def add_field(locator, field) def add_field(locator, field, options={})
xpath = append("#{field}[@id=#{s(locator)}]") postfix = extract_postfix(options)
xpath = xpath.append("#{field}[@name=#{s(locator)}]") xpath = append("#{field}[@id=#{s(locator)}]#{postfix}")
xpath = xpath.append("#{field}[@id=//label[contains(.,#{s(locator)})]/@for]") xpath = xpath.append("#{field}[@name=#{s(locator)}]#{postfix}")
xpath = xpath.append("//label[contains(.,#{s(locator)})]#{field}") xpath = xpath.append("#{field}[@id=//label[contains(.,#{s(locator)})]/@for]#{postfix}")
xpath.prepend("#{field}[@id=//label[text()=#{s(locator)}]/@for]") xpath = xpath.append("//label[contains(.,#{s(locator)})]#{field}#{postfix}")
xpath.prepend("#{field}[@id=//label[text()=#{s(locator)}]/@for]#{postfix}")
end
def extract_postfix(options)
options.inject("") do |postfix, (key, value)|
case key
when :with then postfix += "[@value=#{s(value)}]"
end
postfix
end
end end
# Sanitize a String for putting it into an xpath query # Sanitize a String for putting it into an xpath query

View file

@ -0,0 +1,60 @@
shared_examples_for "has_field" do
describe '#has_field' do
before do
@session.visit('/form')
end
it "should be true if the field is on the page" do
@session.should have_field('Dog')
@session.should have_field('form_description')
@session.should have_field('Region')
end
it "should be false if the field is not on the page" do
@session.should_not have_field('Monkey')
end
context 'with value' do
it "should be true if a field with the given value is on the page" do
@session.should have_field('First Name', :with => 'John')
@session.should have_field('Phone', :with => '+1 555 7021')
@session.should have_field('Street', :with => 'Sesame street 66')
end
it "should be false if the given field is not on the page" do
@session.should_not have_field('First Name', :with => 'Peter')
@session.should_not have_field('Wrong Name', :with => 'John')
end
end
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')
@session.should_not have_no_field('Region')
end
it "should be true if the field is not on the page" do
@session.should have_no_field('Monkey')
end
context 'with value' do
it "should be flase if a field with the given value is on the page" do
@session.should_not have_no_field('First Name', :with => 'John')
@session.should_not have_no_field('Phone', :with => '+1 555 7021')
@session.should_not have_no_field('Street', :with => 'Sesame street 66')
end
it "should be true if the given field is not on the page" do
@session.should have_no_field('First Name', :with => 'Peter')
@session.should have_no_field('Wrong Name', :with => 'John')
end
end
end
end

33
spec/dsl/has_link_spec.rb Normal file
View file

@ -0,0 +1,33 @@
shared_examples_for "has_link" do
describe '#has_link?' do
before do
@session.visit('/with_html')
end
it "should be true if the given link is on the page" do
@session.should have_link('foo')
@session.should have_link('awesome title')
end
it "should be false if the given link is not on the page" do
@session.should_not have_link('monkey')
end
end
describe '#has_no_link?' do
before do
@session.visit('/with_html')
end
it "should be false if the given link is on the page" do
@session.should_not have_no_link('foo')
@session.should_not have_no_link('awesome title')
end
it "should be true if the given link is not on the page" do
@session.should have_no_link('monkey')
end
end
end

View file

@ -52,6 +52,8 @@ shared_examples_for "session" do
it_should_behave_like "has_css" it_should_behave_like "has_css"
it_should_behave_like "has_css" it_should_behave_like "has_css"
it_should_behave_like "has_xpath" it_should_behave_like "has_xpath"
it_should_behave_like "has_link"
it_should_behave_like "has_field"
it_should_behave_like "select" it_should_behave_like "select"
it_should_behave_like "uncheck" it_should_behave_like "uncheck"
it_should_behave_like "locate" it_should_behave_like "locate"