add :with_selected filter to :select selector + tests

add :with_selected filter to :select selector + tests

use shortened block notation

use quicker alternative to [].flatten
This commit is contained in:
Bartek Nowak 2017-06-04 15:51:29 +02:00
parent 8f5f55af89
commit 4d1cac75d7
3 changed files with 93 additions and 7 deletions

View File

@ -352,6 +352,7 @@ end
# @filter [Array<String>] :options Exact match options # @filter [Array<String>] :options Exact match options
# @filter [Array<String>] :with_options Partial match options # @filter [Array<String>] :with_options Partial match options
# @filter [String, Array<String>] :selected Match the selection(s) # @filter [String, Array<String>] :selected Match the selection(s)
# @filter [String, Array<String>] :with_selected Partial match the selection(s)
# #
Capybara.add_selector(:select) do Capybara.add_selector(:select) do
label "select box" label "select box"
@ -380,8 +381,13 @@ Capybara.add_selector(:select) do
end end
filter(:selected) do |node, selected| filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option', visible: false).select { |option| option.selected? }.map { |option| option.text(:all) } actual = node.all(:xpath, './/option', visible: false).select(&:selected?).map { |option| option.text(:all) }
[selected].flatten.sort == actual.sort Array(selected).sort == actual.sort
end
filter(:with_selected) do |node, selected|
actual = node.all(:xpath, './/option', visible: false).select(&:selected?).map { |option| option.text(:all) }
(Array(selected) - actual).empty?
end end
describe do |options| describe do |options|
@ -389,6 +395,7 @@ Capybara.add_selector(:select) do
desc << " with options #{options[:options].inspect}" if options[:options] desc << " with options #{options[:options].inspect}" if options[:options]
desc << " with at least options #{options[:with_options].inspect}" if options[:with_options] desc << " with at least options #{options[:with_options].inspect}" if options[:with_options]
desc << " with #{options[:selected].inspect} selected" if options[:selected] desc << " with #{options[:selected].inspect} selected" if options[:selected]
desc << " with at least #{options[:with_selected].inspect} selected" if options[:with_selected]
desc << describe_all_expression_filters(options) desc << describe_all_expression_filters(options)
desc desc
end end

View File

@ -67,6 +67,36 @@ Capybara::SpecHelper.spec '#has_select?' do
end end
end end
context 'with partial select' do
it "should be true if a field with the given partial values is on the page" do
expect(@session).to have_select('Underwear', with_selected: ['Boxerbriefs', 'Briefs'])
end
it "should be false if a field with the given partial values is not on the page" do
expect(@session).not_to have_select('Underwear', with_selected: ['Boxerbriefs', 'Boxers'])
end
it "should be true after the given partial value is selected" do
@session.select('Boxers', from: 'Underwear')
expect(@session).to have_select('Underwear', with_selected: ['Boxerbriefs', 'Boxers'])
end
it "should be false after one of the given partial values is unselected" do
@session.unselect('Briefs', from: 'Underwear')
expect(@session).not_to have_select('Underwear', with_selected: ['Boxerbriefs', 'Briefs'])
end
it "should be true even when the selected values are invisible, regardless of the select's visibility" do
expect(@session).to have_select('Dessert', visible: false, with_options: ['Pudding', 'Tiramisu'])
expect(@session).to have_select('Cake', with_selected: ['Chocolate Cake', 'Sponge Cake'])
end
it "should support non array partial values" do
expect(@session).to have_select('Underwear', with_selected: 'Briefs')
expect(@session).not_to have_select('Underwear', with_selected: 'Boxers')
end
end
context 'with exact options' do context 'with exact options' do
it "should be true if a field with the given options is on the page" do it "should be true if a field with the given options is on the page" do
expect(@session).to have_select('Region', options: ['Norway', 'Sweden', 'Finland']) expect(@session).to have_select('Region', options: ['Norway', 'Sweden', 'Finland'])
@ -81,10 +111,9 @@ Capybara::SpecHelper.spec '#has_select?' do
expect(@session).not_to have_select('Region', options: ['Norway', 'Norway', 'Norway']) expect(@session).not_to have_select('Region', options: ['Norway', 'Norway', 'Norway'])
end end
it" should be true even when the options are invisible, if the select itself is invisible" do 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']) expect(@session).to have_select("Icecream", visible: false, options: ['Chocolate', 'Vanilla', 'Strawberry'])
end end
end end
context 'with partial options' do context 'with partial options' do
@ -99,7 +128,7 @@ Capybara::SpecHelper.spec '#has_select?' do
expect(@session).not_to have_select('Region', with_options: ['Norway', 'Sweden', 'Finland', 'Latvia']) expect(@session).not_to have_select('Region', with_options: ['Norway', 'Sweden', 'Finland', 'Latvia'])
end end
it" should be true even when the options are invisible, if the select itself is invisible" do 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']) expect(@session).to have_select("Icecream", visible: false, with_options: ['Vanilla', 'Strawberry'])
end end
end end
@ -123,7 +152,9 @@ Capybara::SpecHelper.spec '#has_select?' do
it "should support locator-less usage" do it "should support locator-less usage" do
expect(@session.has_select?(with_options: ['Norway', 'Sweden'])).to eq true expect(@session.has_select?(with_options: ['Norway', 'Sweden'])).to eq true
expect(@session).to have_select(with_options: ['London'] ) expect(@session).to have_select(with_options: ['London'])
expect(@session.has_select?(with_selected: ['Commando', 'Boxerbriefs'])).to eq true
expect(@session).to have_select(with_selected: ['Briefs'])
end end
end end
@ -189,6 +220,31 @@ Capybara::SpecHelper.spec '#has_no_select?' do
end end
end end
context 'with partial select' do
it "should be false if a field with the given partial values is on the page" do
expect(@session).not_to have_no_select('Underwear', with_selected: ['Boxerbriefs', 'Briefs'])
end
it "should be true if a field with the given partial values is not on the page" do
expect(@session).to have_no_select('Underwear', with_selected: ['Boxerbriefs', 'Boxers'])
end
it "should be false after the given partial value is selected" do
@session.select('Boxers', from: 'Underwear')
expect(@session).not_to have_no_select('Underwear', with_selected: ['Boxerbriefs', 'Boxers'])
end
it "should be true after one of the given partial values is unselected" do
@session.unselect('Briefs', from: 'Underwear')
expect(@session).to have_no_select('Underwear', with_selected: ['Boxerbriefs', 'Briefs'])
end
it "should support non array partial values" do
expect(@session).not_to have_no_select('Underwear', with_selected: 'Briefs')
expect(@session).to have_no_select('Underwear', with_selected: 'Boxers')
end
end
context 'with exact options' do context 'with exact options' do
it "should be false if a field with the given options is on the page" do it "should be false if a field with the given options is on the page" do
expect(@session).not_to have_no_select('Region', options: ['Norway', 'Sweden', 'Finland']) expect(@session).not_to have_no_select('Region', options: ['Norway', 'Sweden', 'Finland'])
@ -219,5 +275,7 @@ Capybara::SpecHelper.spec '#has_no_select?' do
it "should support locator-less usage" do it "should support locator-less usage" do
expect(@session.has_no_select?(with_options: ['Norway', 'Sweden', 'Finland', 'Latvia'])).to eq true expect(@session.has_no_select?(with_options: ['Norway', 'Sweden', 'Finland', 'Latvia'])).to eq true
expect(@session).to have_no_select(with_options: ['New London'] ) expect(@session).to have_no_select(with_options: ['New London'] )
expect(@session.has_no_select?(with_selected: ['Boxers'])).to eq true
expect(@session).to have_no_select(with_selected: ['Commando', 'Boxers'])
end end
end end

View File

@ -235,6 +235,17 @@ New line after and before textarea tag
</select> </select>
</p> </p>
<!-- invisible multiselect and options -->
<p style="display: none">
<label for="form_dessert">Dessert</label>
<select name="form[dessert]" id="form_dessert" multiple="multiple">
<option selected="selected">Pudding</option>
<option>Lava cake</option>
<option selected="selected">Tiramisu</option>
<option>Panna cotta</option>
</select>
</p>
<!-- visible select with invisible selected option (which some browsers may treat as visible) --> <!-- visible select with invisible selected option (which some browsers may treat as visible) -->
<p> <p>
<label for="form_sorbet">Sorbet</label> <label for="form_sorbet">Sorbet</label>
@ -245,6 +256,17 @@ New line after and before textarea tag
</select> </select>
</p> </p>
<!-- visible multiselect with invisible selected options (which some browsers may treat as visible) -->
<p>
<label for="form_cake">Cake</label>
<select name="form[cake]" id="form_cake" multiple="multiple">
<option>Butter Cake</option>
<option selected="selected" style="display: none">Chocolate Cake</option>
<option>Strawberry Cake</option>
<option selected="selected" style="display: none">Sponge Cake</option>
</select>
</p>
<p> <p>
<span>First address<span> <span>First address<span>
<label for='address1_street'>Street</label> <label for='address1_street'>Street</label>
@ -593,4 +615,3 @@ New line after and before textarea tag
<label for="asterisk_input">With Asterisk<abbr title="required">*</abbr></label> <label for="asterisk_input">With Asterisk<abbr title="required">*</abbr></label>
<input id="asterisk_input" type="number"value="2016"/> <input id="asterisk_input" type="number"value="2016"/>
</p> </p>