From 9aa81de5f38963e015e2831a0e1b1c5607479511 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 27 Aug 2024 15:13:30 +0200 Subject: [PATCH] use playwright driver --- .github/workflows/ci.yml | 13 ++-- Gemfile | 1 + Gemfile.lock | 8 +++ bin/setup | 1 + spec/support/capybara.rb | 61 ++++++++++--------- spec/system/accessibilite/wcag_usager_spec.rb | 2 +- spec/system/administrateurs/condition_spec.rb | 10 +-- .../administrateurs/types_de_champ_spec.rb | 11 ++-- .../instructeurs/batch_operation_spec.rb | 2 +- spec/system/instructeurs/instruction_spec.rb | 2 +- 10 files changed, 66 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 89d52a61dfe..08dc4a812ba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: POSTGRES_USER: tps_test POSTGRES_DB: tps_test POSTGRES_PASSWORD: tps_test - ports: [ "5432:5432" ] + ports: ['5432:5432'] steps: - uses: actions/checkout@v4 @@ -61,7 +61,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 20 env: - RUBY_YJIT_ENABLE: "1" + RUBY_YJIT_ENABLE: '1' services: postgres: image: postgis/postgis:14-3.3 @@ -69,7 +69,7 @@ jobs: POSTGRES_USER: tps_test POSTGRES_DB: tps_test POSTGRES_PASSWORD: tps_test - ports: ["5432:5432"] + ports: ['5432:5432'] strategy: matrix: @@ -117,7 +117,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 20 env: - RUBY_YJIT_ENABLE: "1" + RUBY_YJIT_ENABLE: '1' services: postgres: image: postgis/postgis:14-3.3 @@ -125,7 +125,7 @@ jobs: POSTGRES_USER: tps_test POSTGRES_DB: tps_test POSTGRES_PASSWORD: tps_test - ports: ["5432:5432"] + ports: ['5432:5432'] strategy: matrix: @@ -137,6 +137,9 @@ jobs: - name: Setup the app runtime and dependencies uses: ./.github/actions/ci-setup-rails + - name: Setup playwright + run: bunx playwright install + - name: Pre-compile assets uses: ./.github/actions/ci-setup-assets diff --git a/Gemfile b/Gemfile index 0a44993e604..33db0435ee9 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,7 @@ gem 'anchored' gem 'bcrypt' gem 'bootsnap', '>= 1.4.4', require: false # Reduces boot times through caching; required in config/boot.rb gem 'browser' +gem 'capybara-playwright-driver' gem 'charlock_holmes' gem 'chartkick' gem 'chunky_png' diff --git a/Gemfile.lock b/Gemfile.lock index 950a1f2b2e5..bdc89529711 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -157,6 +157,10 @@ GEM capybara-email (3.0.2) capybara (>= 2.4, < 4.0) mail + capybara-playwright-driver (0.5.2) + addressable + capybara + playwright-ruby-client (>= 1.16.0) capybara-screenshot (1.0.26) capybara (>= 1.0, < 4) launchy @@ -483,6 +487,9 @@ GEM pdf-core (0.9.0) pg (1.5.6) phonelib (0.8.8) + playwright-ruby-client (1.46.0) + concurrent-ruby (>= 1.1.6) + mime-types (>= 3.0) prawn (2.4.0) pdf-core (~> 0.9.0) ttfunk (~> 1.7) @@ -899,6 +906,7 @@ DEPENDENCIES browser capybara capybara-email + capybara-playwright-driver capybara-screenshot charlock_holmes chartkick diff --git a/bin/setup b/bin/setup index 9b8efb38dbf..f4007d9321e 100755 --- a/bin/setup +++ b/bin/setup @@ -20,6 +20,7 @@ FileUtils.chdir APP_ROOT do # Install JavaScript dependencies system! 'bun --version' system! 'bun install' + system! 'bunx playwright install' if ENV["UPDATE_WEBDRIVER"] puts "\n== Updating webdrivers ==" diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 14efc7214b7..6d978518324 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -24,29 +24,24 @@ def setup_driver(app, download_path, options) end end -Capybara.register_driver :chrome do |app| - options = Selenium::WebDriver::Chrome::Options.new - options.add_argument('--no-sandbox') unless ENV['SANDBOX'] - options.add_argument('--mute-audio') - options.add_argument('--window-size=1440,900') - - download_path = Capybara.save_path - # Chromedriver 77 requires setting this for headless mode on linux - # Different versions of Chrome/selenium-webdriver require setting differently - just set them all - options.add_preference('download.default_directory', download_path) - options.add_preference(:download, default_directory: download_path) - - setup_driver(app, download_path, options) +Capybara.register_driver :playwright do |app| + Capybara::Playwright::Driver.new(app, + browser_type: (ENV['PLAYWRIGHT_BROWSER'] || 'chromium').to_sym, # :chromium (default) or :firefox, :webkit + headless: ENV['NO_HEADLESS'].blank?, + locale: Rails.application.config.i18n.default_locale + ) end -Capybara.register_driver :headless_chrome do |app| +Capybara.register_driver :chrome do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--no-sandbox') unless ENV['SANDBOX'] - options.add_argument('--headless') - options.add_argument('--window-size=1440,900') - options.add_argument('--disable-dev-shm-usage') - options.add_argument('--disable-software-rasterizer') options.add_argument('--mute-audio') + options.add_argument('--window-size=1440,900') + if ENV['NO_HEADLESS'].blank? + options.add_argument('--headless') + options.add_argument('--disable-dev-shm-usage') + options.add_argument('--disable-software-rasterizer') + end download_path = Capybara.save_path # Chromedriver 77 requires setting this for headless mode on linux @@ -69,9 +64,13 @@ def setup_driver(app, download_path, options) Capybara::Screenshot.autosave_on_failure = true # Keep only the screenshots generated from the last failing test suite Capybara::Screenshot.prune_strategy = :keep_last_run -# Tell Capybara::Screenshot how to take screenshots when using the headless_chrome driver -Capybara::Screenshot.register_driver :headless_chrome do |driver, path| - driver.browser.save_screenshot(path) +# Tell Capybara::Screenshot how to take screenshots when using the chrome driver +Capybara::Screenshot.register_driver :chrome do |driver, path| + driver.save_screenshot(path) +end +# Tell Capybara::Screenshot how to take screenshots when using the playwright driver +Capybara::Screenshot.register_driver :playwright do |driver, path| + driver.save_screenshot(path) end RSpec.configure do |config| @@ -80,7 +79,11 @@ def setup_driver(app, download_path, options) end config.before(:each, type: :system, js: true) do - driven_by ENV['NO_HEADLESS'] ? :chrome : :headless_chrome + driven_by :playwright + end + + config.before(:each, type: :system, chrome: true) do + driven_by :chrome end # Set the user preferred language before Javascript system specs. @@ -89,13 +92,13 @@ def setup_driver(app, download_path, options) # However specs using Javascript are run into a Headless Chrome, which doesn't support setting # the default Accept-Language value reliably. # So instead we set the locale cookie explicitly before each Javascript test. - config.before(:each, type: :system, js: true) do - visit '/' # Webdriver needs visiting a page before setting the cookie - Capybara.current_session.driver.browser.manage.add_cookie( - name: :locale, - value: Rails.application.config.i18n.default_locale - ) - end + # config.before(:each, type: :system, js: true) do + # visit '/' # Webdriver needs visiting a page before setting the cookie + # Capybara.current_session.driver.browser.manage.add_cookie( + # name: :locale, + # value: Rails.application.config.i18n.default_locale + # ) + # end # Examples tagged with :capybara_ignore_server_errors will allow Capybara # to continue when an exception in raised by Rails. diff --git a/spec/system/accessibilite/wcag_usager_spec.rb b/spec/system/accessibilite/wcag_usager_spec.rb index ebacdd80ea6..6d7a7849580 100644 --- a/spec/system/accessibilite/wcag_usager_spec.rb +++ b/spec/system/accessibilite/wcag_usager_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe 'wcag rules for usager', js: true do +describe 'wcag rules for usager', chrome: true do let(:procedure) { create(:procedure, :published, :with_service, :for_individual) } let(:password) { 'a very complicated password' } let(:litteraire_user) { create(:user, password: password) } diff --git a/spec/system/administrateurs/condition_spec.rb b/spec/system/administrateurs/condition_spec.rb index a6fd7ef7b33..5bb7d71e1b9 100644 --- a/spec/system/administrateurs/condition_spec.rb +++ b/spec/system/administrateurs/condition_spec.rb @@ -39,8 +39,9 @@ scenario "the first type de champ is removed" do within '.type-de-champ:nth-child(1)' do - click_on 'Supprimer' - page.accept_alert + page.accept_alert do + click_on 'Supprimer' + end end # the condition table is deleted @@ -56,8 +57,9 @@ scenario "removing all conditions" do within '.type-de-champ:nth-child(2)' do - click_on 'cliquer pour désactiver' - page.accept_alert + page.accept_alert do + click_on 'cliquer pour désactiver' + end # the condition table is deleted expect(page).to have_no_table diff --git a/spec/system/administrateurs/types_de_champ_spec.rb b/spec/system/administrateurs/types_de_champ_spec.rb index e8ffd19ee44..ef81cebc011 100644 --- a/spec/system/administrateurs/types_de_champ_spec.rb +++ b/spec/system/administrateurs/types_de_champ_spec.rb @@ -393,13 +393,16 @@ expect(page).to have_button('Publier les modifications', disabled: true) # Supprime dernier champ - all('.fr-btn--tertiary-no-outline[title="Supprimer le champ"]').last.click - page.driver.browser.switch_to.alert.accept rescue nil + page.driver.accept_alert do + all('.fr-btn--tertiary-no-outline[title="Supprimer le champ"]').last.click + end expect(page).to have_selector('.type-de-champ', count: 1, wait: 5) - click_on "Publier les modifications" - page.driver.browser.switch_to.alert.accept + page.driver.accept_alert do + click_on "Publier les modifications" + end + expect(page).to have_content("démarche publiée") unpublished_procedure = create(:procedure) diff --git a/spec/system/instructeurs/batch_operation_spec.rb b/spec/system/instructeurs/batch_operation_spec.rb index 8428f3118a2..5b265cb1cb0 100644 --- a/spec/system/instructeurs/batch_operation_spec.rb +++ b/spec/system/instructeurs/batch_operation_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe 'BatchOperation a dossier:', js: true do +describe 'BatchOperation a dossier:', chrome: true do include ActionView::RecordIdentifier include ActiveJob::TestHelper diff --git a/spec/system/instructeurs/instruction_spec.rb b/spec/system/instructeurs/instruction_spec.rb index 88f79b033f5..507013c850f 100644 --- a/spec/system/instructeurs/instruction_spec.rb +++ b/spec/system/instructeurs/instruction_spec.rb @@ -145,7 +145,7 @@ perform_enqueued_jobs(only: ExportJob) end - page.driver.browser.navigate.refresh + page.refresh expect(page).to have_text('Télécharger l’export') end