brouillon_spec: fix the slow use of have_select(locator, selected: …)

Capybara's `have_select` can be very slow for elemtns with many options
(see https://github.com/teamcapybara/capybara/issues/1527)

This is because Capybara asserts that no other elements than the
required ones are selected.

This faster version is not as complete, but helps when checking the
countries list or the years in a date picker.
This commit is contained in:
Pierre de La Morinerie 2019-06-03 15:47:56 +02:00
parent 5419c130f4
commit b829d105d9
2 changed files with 28 additions and 10 deletions

View file

@ -75,11 +75,11 @@ feature 'The user' do
expect(page).to have_field('email', with: 'loulou@yopmail.com') expect(page).to have_field('email', with: 'loulou@yopmail.com')
expect(page).to have_field('phone', with: '1234567890') expect(page).to have_field('phone', with: '1234567890')
expect(page).to have_checked_field('Non') expect(page).to have_checked_field('Non')
expect(page).to have_select('simple_drop_down_list', selected: 'val2') expect(page).to have_selected_value('simple_drop_down_list', selected: 'val2')
expect(page).to have_select('multiple_drop_down_list', selected: ['val1', 'val3']) expect(page).to have_selected_value('multiple_drop_down_list', selected: ['val1', 'val3'])
expect(page).to have_select('pays', selected: 'AUSTRALIE') expect(page).to have_selected_value('pays', selected: 'AUSTRALIE')
expect(page).to have_select('regions', selected: 'region2') expect(page).to have_selected_value('regions', selected: 'region2')
expect(page).to have_select('departement', selected: 'dep2') expect(page).to have_selected_value('departement', selected: 'dep2')
expect(page).to have_checked_field('engagement') expect(page).to have_checked_field('engagement')
expect(page).to have_field('dossier_link', with: '123') expect(page).to have_field('dossier_link', with: '123')
expect(page).to have_text('file.pdf') expect(page).to have_text('file.pdf')
@ -229,10 +229,10 @@ feature 'The user' do
end end
def check_date_and_time(date, field) def check_date_and_time(date, field)
expect(page).to have_select("#{field}_1i", selected: date.strftime('%Y')) expect(page).to have_selected_value("#{field}_1i", selected: date.strftime('%Y'))
expect(page).to have_select("#{field}_2i", selected: I18n.l(date, format: '%B')) expect(page).to have_selected_value("#{field}_2i", selected: I18n.l(date, format: '%B'))
expect(page).to have_select("#{field}_3i", selected: date.strftime('%-d')) expect(page).to have_selected_value("#{field}_3i", selected: date.strftime('%-d'))
expect(page).to have_select("#{field}_4i", selected: date.strftime('%H')) expect(page).to have_selected_value("#{field}_4i", selected: date.strftime('%H'))
expect(page).to have_select("#{field}_5i", selected: date.strftime('%M')) expect(page).to have_selected_value("#{field}_5i", selected: date.strftime('%M'))
end end
end end

View file

@ -167,4 +167,22 @@ RSpec.configure do |config|
actual.attributes.with_indifferent_access.except(*ignored) == expected.attributes.with_indifferent_access.except(*ignored) actual.attributes.with_indifferent_access.except(*ignored) == expected.attributes.with_indifferent_access.except(*ignored)
end end
end end
# Asserts that a given select element exists in the page,
# and that the option(s) with the given value(s) are selected.
#
# Usage: expect(page).to have_selected_value('Country', selected: 'Australia')
#
# For large lists, this is much faster than `have_select(location, selected: value)`,
# as it doesnt check that every other options are not selected.
RSpec::Matchers.define(:have_selected_value) do |select_locator, options|
match do |page|
values = options[:selected].is_a?(String) ? [options[:selected]] : options[:selected]
select_element = page.first(:select, select_locator)
select_element && values.all? do |value|
select_element.first(:option, value).selected?
end
end
end
end end