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

make locator optional in selector based have_xxx matchers and has_xxx? booleans

This commit is contained in:
Thomas Walpole 2016-09-28 13:54:20 -07:00
parent 1427e9b734
commit 843d7c57ea
4 changed files with 61 additions and 21 deletions

View file

@ -332,7 +332,8 @@ module Capybara
# @option options [String, Regexp] :href The value the href attribute must be # @option options [String, Regexp] :href The value the href attribute must be
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_link?(locator, options={}) def has_link?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:link, locator, options) has_selector?(:link, locator, options)
end end
@ -344,7 +345,8 @@ module Capybara
# @param (see Capybara::Node::Finders#has_link?) # @param (see Capybara::Node::Finders#has_link?)
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_link?(locator, options={}) def has_no_link?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:link, locator, options) has_no_selector?(:link, locator, options)
end end
@ -356,7 +358,8 @@ module Capybara
# @param [String] locator The text, value or id of a button to check for # @param [String] locator The text, value or id of a button to check for
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_button?(locator, options={}) def has_button?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:button, locator, options) has_selector?(:button, locator, options)
end end
@ -368,7 +371,8 @@ module Capybara
# @param [String] locator The text, value or id of a button to check for # @param [String] locator The text, value or id of a button to check for
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_button?(locator, options={}) def has_no_button?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:button, locator, options) has_no_selector?(:button, locator, options)
end end
@ -394,7 +398,8 @@ module Capybara
# @option options [String] :type The type attribute of the field # @option options [String] :type The type attribute of the field
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_field?(locator, options={}) def has_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:field, locator, options) has_selector?(:field, locator, options)
end end
@ -408,7 +413,8 @@ module Capybara
# @option options [String] :type The type attribute of the field # @option options [String] :type The type attribute of the field
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_field?(locator, options={}) def has_no_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:field, locator, options) has_no_selector?(:field, locator, options)
end end
@ -421,7 +427,8 @@ module Capybara
# @param [String] locator The label, name or id of a checked field # @param [String] locator The label, name or id of a checked field
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_checked_field?(locator, options={}) def has_checked_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:field, locator, options.merge(:checked => true)) has_selector?(:field, locator, options.merge(:checked => true))
end end
@ -434,7 +441,8 @@ module Capybara
# @param [String] locator The label, name or id of a checked field # @param [String] locator The label, name or id of a checked field
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_checked_field?(locator, options={}) def has_no_checked_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:field, locator, options.merge(:checked => true)) has_no_selector?(:field, locator, options.merge(:checked => true))
end end
@ -447,7 +455,8 @@ module Capybara
# @param [String] locator The label, name or id of an unchecked field # @param [String] locator The label, name or id of an unchecked field
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_unchecked_field?(locator, options={}) def has_unchecked_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:field, locator, options.merge(:unchecked => true)) has_selector?(:field, locator, options.merge(:unchecked => true))
end end
@ -460,7 +469,8 @@ module Capybara
# @param [String] locator The label, name or id of an unchecked field # @param [String] locator The label, name or id of an unchecked field
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_unchecked_field?(locator, options={}) def has_no_unchecked_field?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:field, locator, options.merge(:unchecked => true)) has_no_selector?(:field, locator, options.merge(:unchecked => true))
end end
@ -492,7 +502,8 @@ module Capybara
# @option options [String, Array] :selected Options which should be selected # @option options [String, Array] :selected Options which should be selected
# @return [Boolean] Whether it exists # @return [Boolean] Whether it exists
# #
def has_select?(locator, options={}) def has_select?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:select, locator, options) has_selector?(:select, locator, options)
end end
@ -504,7 +515,8 @@ module Capybara
# @param (see Capybara::Node::Matchers#has_select?) # @param (see Capybara::Node::Matchers#has_select?)
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_select?(locator, options={}) def has_no_select?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:select, locator, options) has_no_selector?(:select, locator, options)
end end
@ -518,7 +530,8 @@ module Capybara
# @param [String] locator The id or caption of a table # @param [String] locator The id or caption of a table
# @return [Boolean] Whether it exist # @return [Boolean] Whether it exist
# #
def has_table?(locator, options={}) def has_table?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_selector?(:table, locator, options) has_selector?(:table, locator, options)
end end
@ -530,7 +543,8 @@ module Capybara
# @param (see Capybara::Node::Matchers#has_table?) # @param (see Capybara::Node::Matchers#has_table?)
# @return [Boolean] Whether it doesn't exist # @return [Boolean] Whether it doesn't exist
# #
def has_no_table?(locator, options={}) def has_no_table?(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
has_no_selector?(:table, locator, options) has_no_selector?(:table, locator, options)
end end

View file

@ -262,31 +262,38 @@ module Capybara
HaveCurrentPath.new(path, options) HaveCurrentPath.new(path, options)
end end
def have_link(locator, options={}) def have_link(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:link, locator, options) HaveSelector.new(:link, locator, options)
end end
def have_button(locator, options={}) def have_button(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:button, locator, options) HaveSelector.new(:button, locator, options)
end end
def have_field(locator, options={}) def have_field(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options) HaveSelector.new(:field, locator, options)
end end
def have_checked_field(locator, options={}) def have_checked_field(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options.merge(:checked => true)) HaveSelector.new(:field, locator, options.merge(:checked => true))
end end
def have_unchecked_field(locator, options={}) def have_unchecked_field(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options.merge(:unchecked => true)) HaveSelector.new(:field, locator, options.merge(:unchecked => true))
end end
def have_select(locator, options={}) def have_select(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:select, locator, options) HaveSelector.new(:select, locator, options)
end end
def have_table(locator, options={}) def have_table(locator=nil, options={})
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:table, locator, options) HaveSelector.new(:table, locator, options)
end end

View file

@ -266,6 +266,11 @@ Capybara::SpecHelper.spec '#has_unchecked_field?' do
@session.choose('gender_male') @session.choose('gender_male')
expect(@session).to have_unchecked_field('gender_female') expect(@session).to have_unchecked_field('gender_female')
end end
it "should support locator-less usage" do
expect(@session.has_unchecked_field?(disabled: true, id: "form_disabled_unchecked_checkbox"))
expect(@session).to have_unchecked_field(disabled: true, id: "form_disabled_unchecked_checkbox" )
end
end end
Capybara::SpecHelper.spec '#has_no_unchecked_field?' do Capybara::SpecHelper.spec '#has_no_unchecked_field?' do
@ -296,4 +301,9 @@ Capybara::SpecHelper.spec '#has_no_unchecked_field?' do
it "should be true for disabled unchecked fields if :disabled => false" do it "should be true for disabled unchecked fields if :disabled => false" do
expect(@session).to have_no_unchecked_field('Disabled Unchecked Checkbox', :disabled => false) expect(@session).to have_no_unchecked_field('Disabled Unchecked Checkbox', :disabled => false)
end end
it "should support locator-less usage" do
expect(@session.has_no_unchecked_field?(disabled: false, id: "form_disabled_unchecked_checkbox")).to eq true
expect(@session).to have_no_unchecked_field(disabled: false, id: "form_disabled_unchecked_checkbox" )
end
end end

View file

@ -121,6 +121,10 @@ Capybara::SpecHelper.spec '#has_select?' do
end end
end end
it "should support locator-less usage" do
expect(@session.has_select?(:with_options => ['Norway', 'Sweden'])).to eq true
expect(@session).to have_select(:with_options => ['London'] )
end
end end
Capybara::SpecHelper.spec '#has_no_select?' do Capybara::SpecHelper.spec '#has_no_select?' do
@ -211,4 +215,9 @@ Capybara::SpecHelper.spec '#has_no_select?' do
expect(@session).to have_no_select('Region', :with_options => ['Norway', 'Sweden', 'Finland', 'Latvia']) expect(@session).to have_no_select('Region', :with_options => ['Norway', 'Sweden', 'Finland', 'Latvia'])
end end
end end
it "should support locator-less usage" do
expect(@session.has_no_select?(:with_options => ['Norway', 'Sweden', 'Finland', 'Latvia'])).to eq true
expect(@session).to have_no_select(:with_options => ['New London'] )
end
end end