From 091a522cbfd77c7af454a9771d2b1998c8bba3b2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 27 Oct 2022 22:23:25 +0200 Subject: [PATCH 1/4] conf(capybara): add MAKE_IT_SLOW option to reveal bugs --- README.md | 4 ++++ spec/support/capybara.rb | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4ccc423d5..a78769236 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,10 @@ Pour exécuter les tests de l'application, plusieurs possibilités : JS_LOG=error bin/rspec spec/system +- Augmenter la latence lors de tests end2end pour déceler des bugs récalcitrants + + MAKE_IT_SLOW=1 bin/rspec spec/system + ### Ajout de taches à exécuter au déploiement rails generate after_party:task task_name diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 66ef268f7..3fcaf9368 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -3,6 +3,22 @@ require 'capybara-screenshot/rspec' require 'capybara/email/rspec' require 'selenium/webdriver' +def setup_driver(app, download_path, options) + Capybara::Selenium::Driver.new(app, browser: :chrome, options:).tap do |driver| + # Set download dir for Chrome < 77 + driver.browser.download_path = download_path + + if ENV['MAKE_IT_SLOW'].present? + driver.browser.network_conditions = { + offline: false, + latency: 800, + download_throughput: 1024000, + upload_throughput: 1024000 + } + end + end +end + Capybara.register_driver :chrome do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--no-sandbox') unless ENV['SANDBOX'] @@ -15,10 +31,7 @@ Capybara.register_driver :chrome do |app| options.add_preference('download.default_directory', download_path) options.add_preference(:download, default_directory: download_path) - Capybara::Selenium::Driver.new(app, browser: :chrome, options: options).tap do |driver| - # Set download dir for Chrome < 77 - driver.browser.download_path = download_path - end + setup_driver(app, download_path, options) end Capybara.register_driver :headless_chrome do |app| @@ -36,10 +49,7 @@ Capybara.register_driver :headless_chrome do |app| options.add_preference('download.default_directory', download_path) options.add_preference(:download, default_directory: download_path) - Capybara::Selenium::Driver.new(app, browser: :chrome, options: options).tap do |driver| - # Set download dir for Chrome < 77 - driver.browser.download_path = download_path - end + setup_driver(app, download_path, options) end Capybara.default_max_wait_time = 2 From db0fcec019d86070d11b0baf0804c85131e48f50 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 28 Oct 2022 11:13:09 +0200 Subject: [PATCH 2/4] conf(capybara): make JS_LOG work for head and headless --- spec/support/capybara.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 3fcaf9368..693b7419e 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -16,6 +16,12 @@ def setup_driver(app, download_path, options) upload_throughput: 1024000 } end + + if ENV['JS_LOG'].present? + driver.browser.on_log_event(:console) do |event| + puts event.args if event.type == ENV['JS_LOG'].downcase.to_sym + end + end end end @@ -76,12 +82,6 @@ RSpec.configure do |config| config.before(:each, type: :system, js: true) do driven_by ENV['NO_HEADLESS'] ? :chrome : :headless_chrome - - if ENV['JS_LOG'].present? - page.driver.browser.on_log_event(:console) do |event| - puts event.args if event.type == ENV['JS_LOG'].downcase.to_sym - end - end end # Set the user preferred language before Javascript system specs. From d859f1ed0ae38868d6137aa6e88146e3bcbf4651 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 28 Oct 2022 10:45:52 +0200 Subject: [PATCH 3/4] fix(spec/system): add delay when dealing with pj --- spec/system/users/brouillon_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index 36e5fc1c3..7e61325ba 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -184,7 +184,7 @@ describe 'The user' do find_field('Pièce justificative 2').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf') # Expect the files to be uploaded immediately - expect(page).to have_text('analyse antivirus en cours', count: 2) + expect(page).to have_text('analyse antivirus en cours', count: 2, wait: 5) expect(page).to have_text('file.pdf') expect(page).to have_text('RIB.pdf') @@ -206,7 +206,7 @@ describe 'The user' do # Test invalid file type attach_file('Pièce justificative 1', Rails.root + 'spec/fixtures/files/invalid_file_format.json') expect(page).to have_no_text('La pièce justificative n’est pas d’un type accepté') - expect(page).to have_text('analyse antivirus en cours', count: 1) + expect(page).to have_text('analyse antivirus en cours', count: 1, wait: 5) end scenario 'retry on transcient upload error', js: true do @@ -219,15 +219,15 @@ describe 'The user' do instance.render json: { errors: ['Error'] }, status: :bad_request end attach_file('Pièce justificative 1', Rails.root + 'spec/fixtures/files/file.pdf') - expect(page).to have_text('Une erreur s’est produite pendant l’envoi du fichier') + expect(page).to have_css('p', text: 'Le fichier n’a pas pu être envoyé', visible: :visible, wait: 5) expect(page).to have_button('Ré-essayer', visible: true) expect(page).to have_button('Déposer le dossier', disabled: false) allow_any_instance_of(Champs::PieceJustificativeController).to receive(:update).and_call_original # Test that retrying after a failure works - click_on('Ré-essayer', visible: true) - expect(page).to have_text('analyse antivirus en cours') + click_on('Ré-essayer', visible: true, wait: 5) + expect(page).to have_text('analyse antivirus en cours', wait: 5) expect(page).to have_text('file.pdf') expect(page).to have_button('Déposer le dossier', disabled: false) From e7666e274fcc4fd28aa2ee684acb3483d10fdbc6 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 28 Oct 2022 11:12:39 +0200 Subject: [PATCH 4/4] fix(spec/system): wait for merci page --- spec/system/api_particulier/api_particulier_spec.rb | 4 ++++ spec/system/users/brouillon_spec.rb | 1 + 2 files changed, 5 insertions(+) diff --git a/spec/system/api_particulier/api_particulier_spec.rb b/spec/system/api_particulier/api_particulier_spec.rb index 42ac7a00a..3314df8a0 100644 --- a/spec/system/api_particulier/api_particulier_spec.rb +++ b/spec/system/api_particulier/api_particulier_spec.rb @@ -290,6 +290,7 @@ describe 'fetch API Particulier Data', js: true do click_on 'Déposer le dossier' perform_enqueued_jobs end + expect(page).to have_current_path(merci_dossier_path(Dossier.last)) visit demande_dossier_path(dossier) expect(page).to have_content(/Des données.*ont été reçues depuis la CAF/) @@ -344,6 +345,7 @@ describe 'fetch API Particulier Data', js: true do click_on 'Déposer le dossier' perform_enqueued_jobs end + expect(page).to have_current_path(merci_dossier_path(Dossier.last)) visit demande_dossier_path(dossier) expect(page).to have_content(/Des données.*ont été reçues depuis Pôle emploi/) @@ -413,6 +415,7 @@ describe 'fetch API Particulier Data', js: true do click_on 'Déposer le dossier' perform_enqueued_jobs end + expect(page).to have_current_path(merci_dossier_path(Dossier.last)) visit demande_dossier_path(dossier) expect(page).to have_content(/Des données.*ont été reçues depuis le MESRI/) @@ -475,6 +478,7 @@ describe 'fetch API Particulier Data', js: true do click_on 'Déposer le dossier' perform_enqueued_jobs end + expect(page).to have_current_path(merci_dossier_path(Dossier.last)) visit demande_dossier_path(dossier) expect(page).to have_content(/Des données.*ont été reçues depuis la DGFiP/) diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index 7e61325ba..257c92674 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -257,6 +257,7 @@ describe 'The user' do click_on 'Déposer le dossier' + expect(page).to have_current_path(merci_dossier_path(user_dossier)) expect(page).to have_text('Merci') end end