mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Added have_link, have_checked_field and have_unchecked_field matchers
This commit is contained in:
parent
414b2d983f
commit
6920eae19d
6 changed files with 208 additions and 22 deletions
|
@ -72,11 +72,11 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
|
|||
end
|
||||
|
||||
def checked?
|
||||
self[:checked]
|
||||
string_node.checked?
|
||||
end
|
||||
|
||||
def selected?
|
||||
self[:selected]
|
||||
string_node.selected?
|
||||
end
|
||||
|
||||
def path
|
||||
|
|
|
@ -293,6 +293,19 @@ module Capybara
|
|||
has_xpath?(XPath::HTML.field(locator), :checked => true)
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Checks if the page or current node has no radio button or
|
||||
# checkbox with the given label, value or id, that is currently
|
||||
# checked.
|
||||
#
|
||||
# @param [String] locator The label, name or id of a checked field
|
||||
# @return [Boolean] Whether it doesn't exists
|
||||
#
|
||||
def has_no_checked_field?(locator)
|
||||
has_no_xpath?(XPath::HTML.field(locator), :checked => true)
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Checks if the page or current node has a radio button or
|
||||
|
@ -306,6 +319,19 @@ module Capybara
|
|||
has_xpath?(XPath::HTML.field(locator), :unchecked => true)
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Checks if the page or current node has no radio button or
|
||||
# checkbox with the given label, value or id, that is currently
|
||||
# unchecked.
|
||||
#
|
||||
# @param [String] locator The label, name or id of an unchecked field
|
||||
# @return [Boolean] Whether it doesn't exists
|
||||
#
|
||||
def has_no_unchecked_field?(locator)
|
||||
has_no_xpath?(XPath::HTML.field(locator), :unchecked => true)
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Checks if the page or current node has a select field with the
|
||||
|
|
|
@ -98,6 +98,26 @@ module Capybara
|
|||
native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none')]").size == 0
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Whether or not the element is checked.
|
||||
#
|
||||
# @return [Boolean] Whether the element is checked
|
||||
#
|
||||
def checked?
|
||||
native[:checked]
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Whether or not the element is selected.
|
||||
#
|
||||
# @return [Boolean] Whether the element is selected
|
||||
#
|
||||
def selected?
|
||||
native[:selected]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def find_in_base(xpath)
|
||||
|
|
|
@ -101,8 +101,8 @@ module Capybara
|
|||
HaveSelector.new(*args)
|
||||
end
|
||||
|
||||
def have_xpath(path, options={})
|
||||
HaveMatcher.new(:xpath, path, options)
|
||||
def have_xpath(xpath, options={})
|
||||
HaveMatcher.new(:xpath, xpath, options)
|
||||
end
|
||||
|
||||
def have_css(css, options={})
|
||||
|
@ -115,12 +115,24 @@ module Capybara
|
|||
end
|
||||
end
|
||||
|
||||
def have_button(button, options={})
|
||||
HaveMatcher.new(:button, button, options)
|
||||
def have_link(locator, options={})
|
||||
HaveMatcher.new(:link, locator, options)
|
||||
end
|
||||
|
||||
def have_field(button, options={})
|
||||
HaveMatcher.new(:field, button, options)
|
||||
def have_button(locator)
|
||||
HaveMatcher.new(:button, locator)
|
||||
end
|
||||
|
||||
def have_field(locator, options={})
|
||||
HaveMatcher.new(:field, locator, options)
|
||||
end
|
||||
|
||||
def have_checked_field(locator)
|
||||
HaveMatcher.new(:checked_field, locator)
|
||||
end
|
||||
|
||||
def have_unchecked_field(locator)
|
||||
HaveMatcher.new(:unchecked_field, locator)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
shared_examples_for "has_field" do
|
||||
shared_examples_for "has_field" do
|
||||
describe '#has_field' do
|
||||
before { @session.visit('/form') }
|
||||
|
||||
|
@ -14,15 +14,15 @@ shared_examples_for "has_field" do
|
|||
|
||||
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')
|
||||
@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')
|
||||
@session.should have_field('Description', :with => 'Descriptive text goes here')
|
||||
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')
|
||||
@session.should_not have_field('First Name', :with => 'Peter')
|
||||
@session.should_not have_field('Wrong Name', :with => 'John')
|
||||
@session.should_not have_field('Description', :with => 'Monkey')
|
||||
end
|
||||
|
||||
|
@ -53,15 +53,15 @@ shared_examples_for "has_field" do
|
|||
|
||||
context 'with value' do
|
||||
it "should be false 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')
|
||||
@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')
|
||||
@session.should_not have_no_field('Description', :with => 'Descriptive text goes here')
|
||||
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')
|
||||
@session.should have_no_field('First Name', :with => 'Peter')
|
||||
@session.should have_no_field('Wrong Name', :with => 'John')
|
||||
@session.should have_no_field('Description', :with => 'Monkey')
|
||||
end
|
||||
|
||||
|
@ -115,6 +115,24 @@ shared_examples_for "has_field" do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#has_no_checked_field?', :focus => true do
|
||||
before { @session.visit('/form') }
|
||||
|
||||
it "should be false if a checked field is on the page" do
|
||||
@session.should_not have_no_checked_field('gender_female')
|
||||
@session.should_not have_no_checked_field('Hamster')
|
||||
end
|
||||
|
||||
it "should be true if an unchecked field is on the page" do
|
||||
@session.should have_no_checked_field('form_pets_cat')
|
||||
@session.should have_no_checked_field('Male')
|
||||
end
|
||||
|
||||
it "should be true if no field is on the page" do
|
||||
@session.should have_no_checked_field('Does Not Exist')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#has_unchecked_field?' do
|
||||
before { @session.visit('/form') }
|
||||
|
||||
|
@ -152,5 +170,23 @@ shared_examples_for "has_field" do
|
|||
@session.should have_unchecked_field('gender_female')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#has_no_unchecked_field?', :focus => true do
|
||||
before { @session.visit('/form') }
|
||||
|
||||
it "should be true if a checked field is on the page" do
|
||||
@session.should have_no_unchecked_field('gender_female')
|
||||
@session.should have_no_unchecked_field('Hamster')
|
||||
end
|
||||
|
||||
it "should be false if an unchecked field is on the page" do
|
||||
@session.should_not have_no_unchecked_field('form_pets_cat')
|
||||
@session.should_not have_no_unchecked_field('Male')
|
||||
end
|
||||
|
||||
it "should be true if no field is on the page" do
|
||||
@session.should have_no_unchecked_field('Does Not Exist')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -296,7 +296,19 @@ describe Capybara::RSpecMatchers do
|
|||
end
|
||||
end
|
||||
|
||||
describe "have_link matcher"
|
||||
describe "have_link matcher" do
|
||||
let(:html) { '<a href="#">Just a link</a>' }
|
||||
|
||||
it "passes if there is such a button" do
|
||||
html.should have_link('Just a link')
|
||||
end
|
||||
|
||||
it "fails if there is no such button" do
|
||||
expect do
|
||||
html.should have_link('No such Link')
|
||||
end.to raise_error(/expected link "No such Link"/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "have_button matcher" do
|
||||
let(:html) { '<button>A button</button><input type="submit" value="Another button"/>' }
|
||||
|
@ -326,8 +338,88 @@ describe Capybara::RSpecMatchers do
|
|||
end
|
||||
end
|
||||
|
||||
describe "have_checked_field matcher"
|
||||
describe "have_unchecked_field matcher"
|
||||
describe "have_checked_field matcher" do
|
||||
let(:html) do
|
||||
'<label>it is checked<input type="checkbox" checked="checked"/></label>
|
||||
<label>unchecked field<input type="checkbox"/></label>'
|
||||
end
|
||||
|
||||
context "with should" do
|
||||
it "passes if there is such a field and it is checked" do
|
||||
html.should have_checked_field('it is checked')
|
||||
end
|
||||
|
||||
it "fails if there is such a field but it is not checked" do
|
||||
expect do
|
||||
html.should have_checked_field('unchecked field')
|
||||
end.to raise_error(/expected checked_field "unchecked field"/)
|
||||
end
|
||||
|
||||
it "fails if there is no such field" do
|
||||
expect do
|
||||
html.should have_checked_field('no such field')
|
||||
end.to raise_error(/expected checked_field "no such field"/)
|
||||
end
|
||||
end
|
||||
|
||||
context "with should not" do
|
||||
it "fails if there is such a field and it is checked" do
|
||||
expect do
|
||||
html.should_not have_checked_field('it is checked')
|
||||
end.to raise_error(/expected checked_field "it is checked" not to return anything/)
|
||||
end
|
||||
|
||||
it "passes if there is such a field but it is not checked" do
|
||||
html.should_not have_checked_field('unchecked field')
|
||||
end
|
||||
|
||||
it "passes if there is no such field" do
|
||||
html.should_not have_checked_field('no such field')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "have_unchecked_field matcher" do
|
||||
let(:html) do
|
||||
'<label>it is checked<input type="checkbox" checked="checked"/></label>
|
||||
<label>unchecked field<input type="checkbox"/></label>'
|
||||
end
|
||||
|
||||
context "with should" do
|
||||
it "passes if there is such a field and it is not checked" do
|
||||
html.should have_unchecked_field('unchecked field')
|
||||
end
|
||||
|
||||
it "fails if there is such a field but it is checked" do
|
||||
expect do
|
||||
html.should have_unchecked_field('it is checked')
|
||||
end.to raise_error(/expected unchecked_field "it is checked"/)
|
||||
end
|
||||
|
||||
it "fails if there is no such field" do
|
||||
expect do
|
||||
html.should have_unchecked_field('no such field')
|
||||
end.to raise_error(/expected unchecked_field "no such field"/)
|
||||
end
|
||||
end
|
||||
|
||||
context "with should not" do
|
||||
it "fails if there is such a field and it is not checked" do
|
||||
expect do
|
||||
html.should_not have_unchecked_field('unchecked field')
|
||||
end.to raise_error(/expected unchecked_field "unchecked field" not to return anything/)
|
||||
end
|
||||
|
||||
it "passes if there is such a field but it is checked" do
|
||||
html.should_not have_unchecked_field('it is checked')
|
||||
end
|
||||
|
||||
it "passes if there is no such field" do
|
||||
html.should_not have_unchecked_field('no such field')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "have_select matcher"
|
||||
describe "have_table matcher"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue