From ca34d2484768cac203f6c75c706779d89f97deb3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 11 Aug 2025 17:07:42 +1000 Subject: [PATCH] Replace long waits with better polling Capybara polls under the hood as well. So we do something similar here but tailored to the tested code. This reduced the test run time on my machine from 35 seconds to 15 seconds. --- .../admin/enterprises/dfc_permissions_spec.rb | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/spec/system/admin/enterprises/dfc_permissions_spec.rb b/spec/system/admin/enterprises/dfc_permissions_spec.rb index a69bbf9d1e..1414cc2f67 100644 --- a/spec/system/admin/enterprises/dfc_permissions_spec.rb +++ b/spec/system/admin/enterprises/dfc_permissions_spec.rb @@ -24,7 +24,7 @@ RSpec.describe "DFC Permissions", feature: "cqcm-dev", vcr: true do # The component displays something and then replaces it with the real # list. That leads to a race condition and we have to just wait until # the component is loaded. :-( - sleep 10 + wait_for_component_loaded within(platform_list("without-permissions")) do expect(page).to have_content "Proxy Dev Portal" @@ -38,24 +38,49 @@ RSpec.describe "DFC Permissions", feature: "cqcm-dev", vcr: true do find("button", text: "Agree and share").native.trigger("click") end - sleep 5 - within(platform_list("approved")) do + within_platform_list("approved") do expect(page).to have_content "Proxy Dev Portal" find("button", text: "Stop sharing").native.trigger("click") end - sleep 5 - within(platform_list("without-permissions")) do + within_platform_list("without-permissions") do expect(page).to have_content "Proxy Dev Portal" find("button", text: "Agree and share").native.trigger("click") end - sleep 5 - within(platform_list("approved")) do + within_platform_list("approved") do expect(page).to have_content "Proxy Dev Portal" end end + def wait_for_component_loaded + retry_expectations do + within(page.find('solid-permissioning').shadow_root) do + expect(page).to have_content "APPROVED PLATFORMS" + end + end + end + + def within_platform_list(variant, &block) + retry_expectations(on: Ferrum::JavaScriptError) do + within(platform_list(variant), &block) + end + end + + # Handy helper adopted from CERES Fair Food and modified. + # We may want to share this but don't have a need for it now. + def retry_expectations(on: RSpec::Expectations::ExpectationNotMetError) + start = Time.now.utc + finish = start + Capybara.default_max_wait_time + + yield + rescue on + if Time.now.utc < finish + sleep 0.1 + retry + end + end + def platform_list(variant) page.find('solid-permissioning').shadow_root .find("platform-block[variant='#{variant}']").shadow_root