Skip to content

Commit

Permalink
Adjust has_select filters to allow invisible options in some cases
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gladtocode committed Oct 6, 2015
1 parent 91e09a7 commit 33e233a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/capybara/selector.rb
Expand Up @@ -226,12 +226,22 @@ def describe &block
label "select box"
xpath { |locator| XPath::HTML.select(locator) }
filter(:options) do |node, options|
actual = node.all(:xpath, './/option').map { |option| option.text }
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?) }
Expand Down
14 changes: 14 additions & 0 deletions lib/capybara/spec/session/has_select_spec.rb
Expand Up @@ -59,6 +59,11 @@
'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
Expand All @@ -74,6 +79,11 @@
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
Expand All @@ -87,6 +97,10 @@
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

Expand Down
20 changes: 20 additions & 0 deletions lib/capybara/spec/views/form.erb
Expand Up @@ -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>
Expand Down

0 comments on commit 33e233a

Please sign in to comment.