From 86cde8f118bba6718fed5bbc659f3bbba37a057b Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Wed, 21 Jun 2023 12:47:46 +0200 Subject: [PATCH] Register and toggle Cuprite driver Sets up a barebones Cuprite initialization and toggles the javascript driver to use depending on metadata attached to the example/group. --- docker-compose.yml | 17 +++++++ spec/support/cuprite_setup.rb | 88 +++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 spec/support/cuprite_setup.rb diff --git a/docker-compose.yml b/docker-compose.yml index 6cab7e57249..1c1fd6e2ba6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -154,12 +154,14 @@ services: - db-test - selenium-hub - frontend-test + - cuprite-chrome environment: RAILS_ENV: test OPENPROJECT_RAILS__RELATIVE__URL__ROOT: "${OPENPROJECT_RAILS__RELATIVE__URL__ROOT:-}" DATABASE_URL: postgresql://openproject:openproject@db-test/openproject DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true" SELENIUM_GRID_URL: http://selenium-hub:4444/wd/hub + CHROME_URL: http://cuprite-chrome:3333 CAPYBARA_SERVER_PORT: 3000 CAPYBARA_DYNAMIC_BIND_IP: 1 CAPYBARA_APP_HOSTNAME: backend-test @@ -241,3 +243,18 @@ services: # in case we want multiple sessions per container NODE_MAX_INSTANCES: "${CI_JOBS:-4}" NODE_MAX_SESSION: "${CI_JOBS:-4}" + + cuprite-chrome: + # Currently, Apple M1 is only supported in unnumbered "latest" versions. + # See https://github.com/browserless/chrome/issues/1393 + image: browserless/chrome:latest + networks: + - testing + ports: + - "3333:3333" + environment: + # By default, it uses 3000, which is typically used by Rails. + PORT: 3333 + # Set connection timeout to avoid timeout exception during debugging + # https://docs.browserless.io/docs/docker.html#connection-timeout + CONNECTION_TIMEOUT: 600000 diff --git a/spec/support/cuprite_setup.rb b/spec/support/cuprite_setup.rb new file mode 100644 index 00000000000..fe614b6781f --- /dev/null +++ b/spec/support/cuprite_setup.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +# -- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2023 the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +# ++ +# + +require 'capybara/cuprite' + +def headful_mode? + ActiveRecord::Type::Boolean.new.cast(ENV.fetch('OPENPROJECT_TESTING_NO_HEADLESS', nil)) +end + +def headless_mode? + !headful_mode? +end + +def register_better_cuprite(language, name: :"better_cuprite_#{language}") + Capybara.register_driver(name) do |app| + options = { + process_timeout: 10, + inspector: true, + headless: headless_mode? + } + options = options.merge(window_size: [1920, 1080]) if headless_mode? + + if ENV['CHROME_URL'].present? + options = options.merge(url: ENV['CHROME_URL']) + end + + browser_options = { + 'no-sandbox': nil, + 'disable-gpu': nil, + 'disable-popup-blocking': nil, + 'disable-dev-shm-usage': nil, + lang: language + } + + if ENV['OPENPROJECT_TESTING_AUTO_DEVTOOLS'].present? + browser_options = browser_options.merge('auto-open-devtools-for-tabs': nil) + end + + driver_options = options.merge(browser_options:) + + Capybara::Cuprite::Driver.new(app, **driver_options) + end + + Capybara::Screenshot.register_driver(name) do |driver, path| + driver.save_screenshot(path) + end +end + +register_better_cuprite 'en' + +RSpec.configure do |config| + config.around(:each, type: :feature, with_cuprite: true) do |example| + original_driver = Capybara.javascript_driver + Capybara.javascript_driver = :better_cuprite_en + + example.run + + Capybara.javascript_driver = original_driver + end +end