From 843d7c57ea07db0e6cf6b200aaece5a63c30ceae Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 28 Sep 2016 13:54:20 -0700 Subject: [PATCH] make locator optional in selector based have_xxx matchers and has_xxx? booleans --- lib/capybara/node/matchers.rb | 42 +++++++++++++------- lib/capybara/rspec/matchers.rb | 21 ++++++---- lib/capybara/spec/session/has_field_spec.rb | 10 +++++ lib/capybara/spec/session/has_select_spec.rb | 9 +++++ 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/lib/capybara/node/matchers.rb b/lib/capybara/node/matchers.rb index ce573393..a5443b5c 100644 --- a/lib/capybara/node/matchers.rb +++ b/lib/capybara/node/matchers.rb @@ -332,7 +332,8 @@ module Capybara # @option options [String, Regexp] :href The value the href attribute must be # @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) end @@ -344,7 +345,8 @@ module Capybara # @param (see Capybara::Node::Finders#has_link?) # @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) end @@ -356,7 +358,8 @@ module Capybara # @param [String] locator The text, value or id of a button to check for # @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) end @@ -368,7 +371,8 @@ module Capybara # @param [String] locator The text, value or id of a button to check for # @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) end @@ -394,7 +398,8 @@ module Capybara # @option options [String] :type The type attribute of the field # @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) end @@ -408,7 +413,8 @@ module Capybara # @option options [String] :type The type attribute of the field # @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) end @@ -421,7 +427,8 @@ module Capybara # @param [String] locator The label, name or id of a checked field # @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)) end @@ -434,7 +441,8 @@ module Capybara # @param [String] locator The label, name or id of a checked field # @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)) end @@ -447,7 +455,8 @@ module Capybara # @param [String] locator The label, name or id of an unchecked field # @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)) end @@ -460,7 +469,8 @@ module Capybara # @param [String] locator The label, name or id of an unchecked field # @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)) end @@ -492,7 +502,8 @@ module Capybara # @option options [String, Array] :selected Options which should be selected # @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) end @@ -504,7 +515,8 @@ module Capybara # @param (see Capybara::Node::Matchers#has_select?) # @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) end @@ -518,7 +530,8 @@ module Capybara # @param [String] locator The id or caption of a table # @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) end @@ -530,7 +543,8 @@ module Capybara # @param (see Capybara::Node::Matchers#has_table?) # @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) end diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index 50981442..5071850a 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -262,31 +262,38 @@ module Capybara HaveCurrentPath.new(path, options) 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) 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) 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) 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)) 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)) 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) 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) end diff --git a/lib/capybara/spec/session/has_field_spec.rb b/lib/capybara/spec/session/has_field_spec.rb index 1593e7c5..f89e2dec 100644 --- a/lib/capybara/spec/session/has_field_spec.rb +++ b/lib/capybara/spec/session/has_field_spec.rb @@ -266,6 +266,11 @@ Capybara::SpecHelper.spec '#has_unchecked_field?' do @session.choose('gender_male') expect(@session).to have_unchecked_field('gender_female') 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 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 expect(@session).to have_no_unchecked_field('Disabled Unchecked Checkbox', :disabled => false) 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 diff --git a/lib/capybara/spec/session/has_select_spec.rb b/lib/capybara/spec/session/has_select_spec.rb index 4bbfb2bd..42390496 100644 --- a/lib/capybara/spec/session/has_select_spec.rb +++ b/lib/capybara/spec/session/has_select_spec.rb @@ -121,6 +121,10 @@ Capybara::SpecHelper.spec '#has_select?' do 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 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']) 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