Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add a computed style matcher (#36381)

Jarek Radoszd30d4e764f92
diff --git a/plugins/chat/spec/system/drawer_spec.rb b/plugins/chat/spec/system/drawer_spec.rb
index c98e09d58b9..69c27fe6c2d 100644
--- a/plugins/chat/spec/system/drawer_spec.rb
+++ b/plugins/chat/spec/system/drawer_spec.rb
@@ -78,8 +78,8 @@ RSpec.describe "Drawer", type: :system do
chat_page.open_from_header
chat_drawer = page.find(".chat-drawer")
- expect(get_style(chat_drawer, "width")).to eq("500px")
- expect(get_style(chat_drawer, "height")).to eq("500px")
+ expect(chat_drawer).to have_computed_style(width: "500px")
+ expect(chat_drawer).to have_computed_style(height: "500px")
end
it "has a default size" do
@@ -88,8 +88,8 @@ RSpec.describe "Drawer", type: :system do
chat_page.open_from_header
chat_drawer = page.find(".chat-drawer")
- expect(get_style(chat_drawer, "width")).to eq("400px")
- expect(get_style(chat_drawer, "height")).to eq("530px")
+ expect(chat_drawer).to have_computed_style(width: "400px")
+ expect(chat_drawer).to have_computed_style(height: "530px")
end
end
diff --git a/spec/support/computed_style_matcher.rb b/spec/support/computed_style_matcher.rb
new file mode 100644
index 00000000000..a8abc3a0a73
--- /dev/null
+++ b/spec/support/computed_style_matcher.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+RSpec::Matchers.define :have_computed_style do |expected|
+ match { |element| computed_style(element, expected.keys.first) == expected.values.first }
+
+ failure_message do |element|
+ actual = computed_style(element, expected.keys.first)
+ "expected the element to have #{expected.keys.first} with the value '#{expected.values.first}', but the value is '#{actual}'"
+ end
+
+ failure_message_when_negated do |element|
+ "expected the element not to have #{expected.keys.first} with the value #{expected.values.first}, but it does"
+ end
+
+ def computed_style(element, property)
+ element.evaluate_script("getComputedStyle(this)['#{property}']")
+ end
+end
diff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rb
index 18dfda521c6..1abe661e60e 100644
--- a/spec/support/system_helpers.rb
+++ b/spec/support/system_helpers.rb
@@ -332,11 +332,6 @@ module SystemHelpers
end
end
- def get_style(element, key)
- script = "window.getComputedStyle(arguments[0]).getPropertyValue(arguments[1])"
- page.evaluate_script(script, element, key)
- end
-
def expect_no_alert
opened_dialog = false
diff --git a/spec/system/lightbox_spec.rb b/spec/system/lightbox_spec.rb
index d1109e60eaf..3e772edcc28 100644
--- a/spec/system/lightbox_spec.rb
+++ b/spec/system/lightbox_spec.rb
@@ -139,7 +139,7 @@ describe "Lightbox | Photoswipe", type: :system do
expect(lightbox).to be_visible
# text is reversed by CSS, so check for correct property
- expect(get_style(lightbox.counter, "direction")).to eq("rtl")
+ expect(lightbox.counter).to have_computed_style(direction: "rtl")
expect(lightbox).to have_counter("1 / 3")
expect(lightbox).to have_image_source(upload_1)
diff --git a/themes/horizon/spec/system/page_objects/components/user_color_palette_selector.rb b/themes/horizon/spec/system/page_objects/components/user_color_palette_selector.rb
index 6d96bb4c113..0818ba982ff 100644
--- a/themes/horizon/spec/system/page_objects/components/user_color_palette_selector.rb
+++ b/themes/horizon/spec/system/page_objects/components/user_color_palette_selector.rb
@@ -44,11 +44,9 @@ module PageObjects
end
def has_computed_color?(color)
- computed_background_color =
- page.evaluate_script(
- "getComputedStyle(document.querySelector(\"li.sidebar-section-link-wrapper[data-list-item-name='everything'] .active\")).backgroundColor",
- )
- computed_background_color == color
+ expect(
+ find("li.sidebar-section-link-wrapper[data-list-item-name='everything'] .active"),
+ ).to have_computed_style(backgroundColor: color)
end
end
end