Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add custom Capybara selectors for theme testing

Kabiru Mwenja8eed437032ce
diff --git a/spec/features/my/interface_spec.rb b/spec/features/my/interface_spec.rb
index 8562e8744b4..04483dd5536 100644
--- a/spec/features/my/interface_spec.rb
+++ b/spec/features/my/interface_spec.rb
@@ -29,12 +29,9 @@
#++
require "spec_helper"
-require_relative "support/theme_matchers"
RSpec.describe "My account Interface settings",
:js, :selenium do
- include ThemeTestHelpers
-
let(:user) { create(:user) }
before do
@@ -52,7 +49,7 @@ RSpec.describe "My account Interface settings",
expect(page).to have_theme("dark")
select_theme "Automatic (match OS color mode)"
- expect(page).to have_auto_theme_config({})
+ expect(page).to have_auto_theme_config
end
it "allows user to increase contrast for single theme modes" do
@@ -65,11 +62,11 @@ RSpec.describe "My account Interface settings",
expect(page).to have_no_field("Force high-contrast when in Dark mode")
enable_contrast_for_single_theme
- expect(page).to have_theme("light", high_contrast: true)
+ expect(page).to have_theme("light", contrast: true)
select "Dark", from: "Color mode"
enable_contrast_for_single_theme
- expect(page).to have_theme("dark", high_contrast: true)
+ expect(page).to have_theme("dark", contrast: true)
end
it "shows appropriate contrast options based on theme selection" do
@@ -116,4 +113,24 @@ RSpec.describe "My account Interface settings",
end
end
end
+
+ def select_theme(theme)
+ select theme, from: "Color mode"
+ click_on "Update look and feel"
+ end
+
+ def enable_contrast_for_single_theme
+ check "Increase contrast"
+ click_on "Update look and feel"
+ end
+
+ def configure_auto_contrast(light: false, dark: false)
+ check "Force high-contrast when in Light mode" if light
+ check "Force high-contrast when in Dark mode" if dark
+ click_on "Update look and feel"
+ end
+
+ def navigate_to_interface_settings
+ click_on "Interface"
+ end
end
diff --git a/spec/features/my/support/theme_matchers.rb b/spec/features/my/support/theme_matchers.rb
deleted file mode 100644
index 8fe219d67d2..00000000000
--- a/spec/features/my/support/theme_matchers.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-# frozen_string_literal: true
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 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.
-#++
-
-RSpec::Matchers.define :have_theme do |theme, options = {}|
- match do |page|
- contrast_suffix = options[:high_contrast] ? "_high_contrast" : ""
- page.has_css?("body[data-color-mode='#{theme}'][data-#{theme}-theme='#{theme}#{contrast_suffix}']")
- end
-
- failure_message do |_page|
- contrast_text = options[:high_contrast] ? " with high contrast" : ""
- "expected page to have #{theme} theme#{contrast_text}"
- end
-
- failure_message_when_negated do |_page|
- contrast_text = options[:high_contrast] ? " with high contrast" : ""
- "expected page not to have #{theme} theme#{contrast_text}"
- end
-end
-
-RSpec::Matchers.define :have_auto_theme_config do |expected_config|
- match do |page|
- base_selector = "body[data-auto-theme-switcher-theme-value='sync_with_os']"
- return false unless page.has_css?(base_selector)
-
- if expected_config[:enable_auto_light_contrast]
- light_selector = "body[data-auto-theme-switcher-enable-auto-light-theme-contrast-value='true']"
- return false unless page.has_css?(light_selector)
- end
-
- if expected_config[:enable_auto_dark_contrast]
- dark_selector = "body[data-auto-theme-switcher-enable-auto-dark-theme-contrast-value='true']"
- return false unless page.has_css?(dark_selector)
- end
-
- true
- end
-
- failure_message do |_page|
- "expected page to have auto theme configuration: #{expected_config}"
- end
-end
-
-module ThemeTestHelpers
- def select_theme(theme)
- select theme, from: "Color mode"
- click_on "Update look and feel"
- end
-
- def enable_contrast_for_single_theme
- check "Increase contrast"
- click_on "Update look and feel"
- end
-
- def configure_auto_contrast(light: false, dark: false)
- check "Force high-contrast when in Light mode" if light
- check "Force high-contrast when in Dark mode" if dark
- click_on "Update look and feel"
- end
-
- def navigate_to_interface_settings
- click_on "Interface"
- end
-end
diff --git a/spec/support/capybara/theme_selectors.rb b/spec/support/capybara/theme_selectors.rb
new file mode 100644
index 00000000000..015b5ecc46e
--- /dev/null
+++ b/spec/support/capybara/theme_selectors.rb
@@ -0,0 +1,111 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+Capybara.add_selector :theme do
+ label "Theme"
+
+ xpath do |locator|
+ xpath = XPath.descendant(:body)
+ xpath = builder(xpath).add_attribute_conditions("data-color-mode": locator) if locator
+ xpath
+ end
+
+ expression_filter(:contrast) do |expr, contrast|
+ if contrast
+ # Extract the theme from the existing expression
+ theme_match = expr.to_s.match(/data-color-mode='([^']+)'/)
+ if theme_match
+ theme = theme_match[1]
+ contrast_suffix = "_high_contrast"
+ builder(expr).add_attribute_conditions("data-#{theme}-theme": "#{theme}#{contrast_suffix}")
+ else
+ expr
+ end
+ else
+ expr
+ end
+ end
+
+ describe_expression_filters do |contrast: nil, **|
+ contrast ? " with high contrast" : ""
+ end
+end
+
+Capybara.add_selector :auto_theme_config do
+ label "Auto Theme Config"
+
+ xpath do |_locator|
+ xpath = XPath.descendant(:body)
+ xpath = builder(xpath).add_attribute_conditions("data-auto-theme-switcher-theme-value": "sync_with_os")
+ xpath
+ end
+
+ expression_filter(:enable_auto_light_contrast) do |expr, enabled|
+ if enabled
+ builder(expr).add_attribute_conditions("data-auto-theme-switcher-enable-auto-light-theme-contrast-value": "true")
+ else
+ expr
+ end
+ end
+
+ expression_filter(:enable_auto_dark_contrast) do |expr, enabled|
+ if enabled
+ builder(expr).add_attribute_conditions("data-auto-theme-switcher-enable-auto-dark-theme-contrast-value": "true")
+ else
+ expr
+ end
+ end
+
+ describe_expression_filters do |enable_auto_light_contrast: nil, enable_auto_dark_contrast: nil, **|
+ desc = +""
+ desc << " with auto light contrast" if enable_auto_light_contrast
+ desc << " with auto dark contrast" if enable_auto_dark_contrast
+ desc
+ end
+end
+
+module Capybara
+ module RSpecMatchers
+ def have_theme(locator = nil, contrast: false, **, &)
+ Matchers::HaveSelector.new(:theme, locator, contrast:, **, &)
+ end
+
+ def have_no_theme(locator = nil, **, &)
+ Matchers::NegatedMatcher.new(have_theme(locator, **, &))
+ end
+
+ def have_auto_theme_config(enable_auto_light_contrast: false, enable_auto_dark_contrast: false, **, &)
+ Matchers::HaveSelector.new(:auto_theme_config, nil,
+ enable_auto_light_contrast:,
+ enable_auto_dark_contrast:,
+ **, &)
+ end
+ end
+end