From 84d500f18bf83930d11c9627b33a13e63756ee74 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Fri, 26 Sep 2025 13:39:19 +0100 Subject: [PATCH] Adapt Selenium to work remotely as well as local. - Parallelel tests are not working with remote Selenium. For now let's detect the environment and disable parallel tests when remote. - When using remote Selenium, the browser doesn't reset properly when changing preferences. As a result these are not applied. As a workaround, on remote we'll run a Selenium instance per different set of settings we need. This is not scalable, but for now it should be ok as there are only 3 sets. - Tell WebMock not to interfere with requests between containers. --- .devcontainer/compose.yaml | 14 ++++++++-- .devcontainer/devcontainer.json | 1 - test/application_system_test_case.rb | 38 +++++++++++++++++++--------- test/system/embed_test.rb | 18 ++++++++----- test/test_helper.rb | 22 ++++++++++------ 5 files changed, 64 insertions(+), 29 deletions(-) diff --git a/.devcontainer/compose.yaml b/.devcontainer/compose.yaml index b9265f6fd..7cb518323 100644 --- a/.devcontainer/compose.yaml +++ b/.devcontainer/compose.yaml @@ -19,10 +19,20 @@ services: # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. # (Adding the "ports" property to this file will not forward from a Codespace.) depends_on: - - selenium + - selenium-default + - selenium-de + - selenium-nolang - postgres - selenium: + selenium-default: + image: selenium/standalone-firefox + restart: unless-stopped + + selenium-de: + image: selenium/standalone-firefox + restart: unless-stopped + + selenium-nolang: image: selenium/standalone-firefox restart: unless-stopped diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e2c08c716..88a00f971 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -16,7 +16,6 @@ "containerEnv": { "CAPYBARA_SERVER_PORT": "45678", - "SELENIUM_HOST": "selenium", "DB_HOST": "postgres" }, diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index d803e4368..be7b35f0c 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -15,27 +15,41 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase config.enable_aria_label = true end - if ENV["CAPYBARA_SERVER_PORT"] - served_by :host => "rails-app", :port => ENV["CAPYBARA_SERVER_PORT"] + cattr_accessor(:capybara_server_port) { ENV.fetch("CAPYBARA_SERVER_PORT", nil) } + + served_by :host => "rails-app", :port => capybara_server_port if capybara_server_port + + def self.driven_by_selenium(config_name = "default", opts = {}) + preferences = opts.fetch(:preferences, {}).reverse_merge( + "intl.accept_languages" => "en" + ) + + options = { + :name => config_name + } + + if capybara_server_port + selenium_host = "http://selenium-#{config_name}:4444" + options = options.merge( + :url => selenium_host, + :browser => :remote + ) + end driven_by( :selenium, :using => Settings.system_test_headless ? :headless_firefox : :firefox, - :options => { - :url => "http://#{ENV.fetch('SELENIUM_HOST', nil)}:4444", - :browser => :remote - } + :options => options ) do |options| - options.add_preference("intl.accept_languages", "en") - options.binary = Settings.system_test_firefox_binary if Settings.system_test_firefox_binary - end - else - driven_by :selenium, :using => Settings.system_test_headless ? :headless_firefox : :firefox do |options| - options.add_preference("intl.accept_languages", "en") + preferences.each do |name, value| + options.add_preference(name, value) + end options.binary = Settings.system_test_firefox_binary if Settings.system_test_firefox_binary end end + driven_by_selenium + def before_setup super osm_website_app = create(:oauth_application, :name => "OpenStreetMap Web Site", :scopes => "write_api write_notes") diff --git a/test/system/embed_test.rb b/test/system/embed_test.rb index 2c798832b..e906866ea 100644 --- a/test/system/embed_test.rb +++ b/test/system/embed_test.rb @@ -10,9 +10,12 @@ class EmbedTest < ApplicationSystemTestCase end class GermanEmbedTest < ApplicationSystemTestCase - driven_by :selenium, :using => :headless_firefox, :options => { :name => :selenium_de } do |options| - options.add_preference("intl.accept_languages", "de") - end + driven_by_selenium( + "de", + :preferences => { + "intl.accept_languages" => "de" + } + ) test "shows localized report link" do visit export_embed_path @@ -21,9 +24,12 @@ class GermanEmbedTest < ApplicationSystemTestCase end class UnknownLanguageEmbedTest < ApplicationSystemTestCase - driven_by :selenium, :using => :headless_firefox, :options => { :name => :selenium_unknown_language } do |options| - options.add_preference("intl.accept_languages", "unknown-language") - end + driven_by_selenium( + "nolang", + :preferences => { + "intl.accept_languages" => "unknown-language" + } + ) test "shows report link in fallback language" do visit export_embed_path diff --git a/test/test_helper.rb b/test/test_helper.rb index ef159b0d4..1dc91f162 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -36,7 +36,7 @@ require "rails/test_help" require "webmock/minitest" require "minitest/focus" unless ENV["CI"] -WebMock.disable_net_connect!(:allow_localhost => true) +WebMock.disable_net_connect!(:allow_localhost => true, :allow => %w[selenium-default selenium-de selenium-nolang rails-app]) module ActiveSupport class TestCase @@ -44,15 +44,21 @@ module ActiveSupport include ActiveJob::TestHelper include LibXML - # Run tests in parallel with specified workers - parallelize(:workers => :number_of_processors) + if ENV.key?("CAPYBARA_SERVER_PORT") + # Running in the devcontainer. Can't figure out how + # to run things in parallel at the moment, so for now + # we are not doing it. + else + # Run tests in parallel with specified workers + parallelize(:workers => :number_of_processors) - parallelize_setup do |worker| - SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}" - end + parallelize_setup do |worker| + SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}" + end - parallelize_teardown do - SimpleCov.result + parallelize_teardown do + SimpleCov.result + end end ## -- 2.39.5