Allow a selector to specify its default visibility setting

This commit is contained in:
Thomas Walpole 2016-10-02 18:28:52 -07:00
parent 6000e0d332
commit a0e99a8da9
3 changed files with 39 additions and 12 deletions

View File

@ -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

View File

@ -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={})

View File

@ -25,6 +25,7 @@ RSpec.describe Capybara do
<label for="my_text_input">My Text Input</label>
<input type="text" name="form[my_text_input]" placeholder="my text" id="my_text_input"/>
<input type="file" id="file" class=".special file"/>
<input type="hidden" id="hidden_field" value="this is hidden"/>
<a href="#">link</a>
<fieldset></fieldset>
<select>
@ -51,6 +52,18 @@ RSpec.describe Capybara do
end
end
describe "adding a selector" do
it "can set default visiblity" do
Capybara.add_selector :hidden_field do
visible :hidden
css { |sel| 'input[type="hidden"]' }
end
expect(string).to have_no_css('input[type="hidden"]')
expect(string).to have_selector(:hidden_field)
end
end
describe "modify_selector" do
it "allows modifying a selector" do
el = string.find(:custom_selector, 'a')