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

Adjust has_select filters to allow invisible options in some cases

- selected filter always allows invisible options

- options filter allows invisible options if the select
  itself is invisible

- with_options filter allows invisible options if hte select
  itself is invisible
This commit is contained in:
Nathan Wenneker 2015-10-06 14:42:45 -06:00
parent 91e09a793d
commit 33e233a029
3 changed files with 47 additions and 3 deletions

View file

@ -226,12 +226,22 @@ Capybara.add_selector(:select) do
label "select box"
xpath { |locator| XPath::HTML.select(locator) }
filter(:options) do |node, options|
if node.visible?
actual = node.all(:xpath, './/option').map { |option| option.text }
else
actual = node.all(:xpath, './/option', visible: false).map { |option| option.text(:all) }
end
options.sort == actual.sort
end
filter(:with_options) { |node, options| options.all? { |option| node.first(:option, option, minimum: 0) } }
filter(:with_options) do |node, options|
finder_settings = { minimum: 0 }
if !node.visible?
finder_settings[:visible] = false
end
options.all? { |option| node.first(:option, option, finder_settings) }
end
filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
actual = node.all(:xpath, './/option', visible: false).select { |option| option.selected? }.map { |option| option.text(:all) }
[selected].flatten.sort == actual.sort
end
filter(:disabled, default: false, boolean: true, skip_if: :all) { |node, value| not(value ^ node.disabled?) }

View file

@ -59,6 +59,11 @@ Capybara::SpecHelper.spec '#has_select?' do
'Boxerbriefs', 'Briefs', 'Commando', "Frenchman's Pantalons", 'Long Johns'
])
end
it "should be true even when the selected option invisible, regardless of the select's visibility" do
expect(@session).to have_select('Icecream', :visible => false, :selected => 'Chocolate')
expect(@session).to have_select('Sorbet', :selected => 'Vanilla')
end
end
context 'with exact options' do
@ -74,6 +79,11 @@ Capybara::SpecHelper.spec '#has_select?' do
expect(@session).not_to have_select('Region', :options => ['Norway', 'Sweden'])
expect(@session).not_to have_select('Region', :options => ['Norway', 'Norway', 'Norway'])
end
it" should be true even when the options are invisible, if the select itself is invisible" do
expect(@session).to have_select("Icecream", :visible => false, :options => ['Chocolate', 'Vanilla', 'Strawberry'])
end
end
context 'with partial options' do
@ -87,6 +97,10 @@ Capybara::SpecHelper.spec '#has_select?' do
expect(@session).not_to have_select('Does not exist', :with_options => ['John'])
expect(@session).not_to have_select('Region', :with_options => ['Norway', 'Sweden', 'Finland', 'Latvia'])
end
it" should be true even when the options are invisible, if the select itself is invisible" do
expect(@session).to have_select("Icecream", :visible => false, :with_options => ['Vanilla', 'Strawberry'])
end
end
end

View file

@ -191,6 +191,26 @@ New line after and before textarea tag
</select>
</p>
<!-- invisible select and options -->
<p style="display: none">
<label for="form_icecream">Icecream</label>
<select name="form[icecream]" id="form_icecream">
<option selected="selected">Chocolate</option>
<option>Vanilla</option>
<option>Strawberry</option>
</select>
</p>
<!-- visible select with invisible selected option (which some browsers may treat as visible) -->
<p>
<label for="form_sorbet">Sorbet</label>
<select name="form[sorbet]" id="form_sorbet">
<option>Chocolate</option>
<option selected="selected" style="display: none">Vanilla</option>
<option>Strawberry</option>
</select>
</p>
<p>
<span>First address<span>
<label for='address1_street'>Street</label>