diff --git a/lib/capybara/queries/selector_query.rb b/lib/capybara/queries/selector_query.rb index 3ce97b5d..85cc14d8 100644 --- a/lib/capybara/queries/selector_query.rb +++ b/lib/capybara/queries/selector_query.rb @@ -76,18 +76,10 @@ module Capybara end def visible - if options.has_key?(:visible) - case @options[:visible] - when true then :visible - when false then :all - else @options[:visible] - end - else - if Capybara.ignore_hidden_elements - :visible - else - :all - end + case (vis = options.fetch(:visible){ @selector.default_visibility }) + when true then :visible + when false then :all + else vis end end diff --git a/lib/capybara/selector/selector.rb b/lib/capybara/selector/selector.rb index 5e44c651..1f49a646 100644 --- a/lib/capybara/selector/selector.rb +++ b/lib/capybara/selector/selector.rb @@ -50,6 +50,7 @@ module Capybara @format = nil @expression = nil @expression_filters = [] + @default_visibility = nil instance_eval(&block) end @@ -186,6 +187,27 @@ module Capybara @filter_set.describe &block end + ## + # + # Set the default visibility mode that shouble be used if no visibile option is passed when using the selector. + # If not specified will default to the behavior indicated by Capybara.ignore_hidden_elements + # + # @param [Symbol] default_visibility Only find elements with the specified visibility: + # * :all - finds visible and invisible elements. + # * :hidden - only finds invisible elements. + # * :visible - only finds visible elements. + def visible(default_visibility) + @default_visibility = default_visibility + end + + def default_visibility + if @default_visibility.nil? + Capybara.ignore_hidden_elements + else + @default_visibility + end + end + private def locate_field(xpath, locator, options={}) diff --git a/spec/selector_spec.rb b/spec/selector_spec.rb index 7c3a3799..f7c24670 100644 --- a/spec/selector_spec.rb +++ b/spec/selector_spec.rb @@ -25,6 +25,7 @@ RSpec.describe Capybara do + link