From a30f764a225f1b999fda538bb1b4aa4d828cfa7c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 11 Dec 2024 16:21:56 +1100 Subject: [PATCH 1/5] Add timepicker helper with added expectation --- spec/support/features/datepicker_helper.rb | 11 +++++++++ spec/system/admin/order_cycles/edit_spec.rb | 25 +++++---------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/spec/support/features/datepicker_helper.rb b/spec/support/features/datepicker_helper.rb index 74444316a53..e86e32a2f95 100644 --- a/spec/support/features/datepicker_helper.rb +++ b/spec/support/features/datepicker_helper.rb @@ -15,6 +15,17 @@ def select_dates_from_daterangepicker(from, to) select_date_from_datepicker(to) end + def select_datetime_from(element, datetime) + datetime = Time.zone.parse(datetime) if datetime.is_a? String + + # Wait for timepicker element to be loaded: + expect(page).to have_css "#{element}.datetimepicker" + + find(element).click + select_datetime_from_datepicker(datetime) + close_datepicker + end + def select_date_from_datepicker(date) within ".flatpickr-calendar.open" do # Unfortunately, flatpickr doesn't notice a change of year when we do diff --git a/spec/system/admin/order_cycles/edit_spec.rb b/spec/system/admin/order_cycles/edit_spec.rb index f6223edfad9..6a09d7ea22d 100644 --- a/spec/system/admin/order_cycles/edit_spec.rb +++ b/spec/system/admin/order_cycles/edit_spec.rb @@ -39,10 +39,7 @@ expect(page).to have_field 'order_cycle_name', with: "Order cycle name updated" expect(page).to have_content('Your order cycle has been updated.') - # change date range field value - find('#order_cycle_orders_close_at').click - select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00") - close_datepicker + select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" expect(page).to have_content('You have unsaved changes') # click save to open warning modal @@ -61,10 +58,7 @@ login_as_admin visit edit_admin_order_cycle_path(order_cycle) - # change date range field value - find('#order_cycle_orders_close_at').click - select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00") - close_datepicker + select_datetime_from "#order_cycle_orders_open_at", "2024-03-30 00:00" # click save to open warning modal click_button('Save') @@ -94,10 +88,7 @@ expect(page).to have_field 'order_cycle_name', with: 'OC1 name updated' expect(page).to have_content('Your order cycle has been updated.') - # Now change date range field value - find('#order_cycle_orders_close_at').click - select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00") - close_datepicker + select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" expect(page).to have_content('You have unsaved changes') click_button('Save') @@ -126,10 +117,7 @@ expect(page).to have_content('Your order cycle has been updated.') expect(page).to have_field 'order_cycle_name', with: "Order cycle name updated" - # change date range field value - find('#order_cycle_orders_close_at').click - select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00") - close_datepicker + select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" expect(page).to have_content('You have unsaved changes') @@ -161,10 +149,7 @@ expect(page).to have_content('Your order cycle has been updated.') expect(page.find('#order_cycle_name').value).to eq 'OC1 name updated' - # Now change date range field value - find('#order_cycle_orders_close_at').click - select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00") - close_datepicker + select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" expect(page).to have_content('You have unsaved changes') sleep(2) From 72376da98f4e1cd4b6f91aa8ec73748f4785cee9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 11 Dec 2024 16:22:21 +1100 Subject: [PATCH 2/5] Assess stability without retry --- spec/system/admin/order_cycles/edit_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/admin/order_cycles/edit_spec.rb b/spec/system/admin/order_cycles/edit_spec.rb index 6a09d7ea22d..77e406e2717 100644 --- a/spec/system/admin/order_cycles/edit_spec.rb +++ b/spec/system/admin/order_cycles/edit_spec.rb @@ -75,7 +75,7 @@ end context 'with no attached order' do - it "does not show warning modal", retry: 3 do + it "does not show warning modal" do login_as_admin visit edit_admin_order_cycle_path(order_cycle) @@ -135,7 +135,7 @@ end context 'with no attached orders' do - it "does not show warning modal", retry: 3 do + it "does not show warning modal" do login_as_admin visit edit_admin_order_cycle_path(order_cycle) From 874c4640888b663d4459b1efaa8fea07b7f14fad Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 19 Dec 2024 16:39:49 +1100 Subject: [PATCH 3/5] Tidy one spec example --- spec/system/admin/order_cycles/edit_spec.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/spec/system/admin/order_cycles/edit_spec.rb b/spec/system/admin/order_cycles/edit_spec.rb index 77e406e2717..a5a6e8b4210 100644 --- a/spec/system/admin/order_cycles/edit_spec.rb +++ b/spec/system/admin/order_cycles/edit_spec.rb @@ -74,26 +74,27 @@ end end - context 'with no attached order' do + context "with no attached order" do it "does not show warning modal" do login_as_admin visit edit_admin_order_cycle_path(order_cycle) # change non-date range field value - fill_in 'order_cycle_name', with: "OC1 name updated" - expect(page).to have_content('You have unsaved changes') + fill_in "order_cycle_name", with: "OC1 name updated" + expect(page).to have_content "You have unsaved changes" # click save - click_button('Save') - expect(page).to have_field 'order_cycle_name', with: 'OC1 name updated' - expect(page).to have_content('Your order cycle has been updated.') + click_button "Save" + expect(page).not_to have_content "You have unsaved changes" + expect(page).to have_content "Your order cycle has been updated." + expect(page).to have_field "order_cycle_name", with: "OC1 name updated" select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" - expect(page).to have_content('You have unsaved changes') + expect(page).to have_content "You have unsaved changes" - click_button('Save') - expect(page).to have_field 'order_cycle_orders_close_at', with: '2024-03-30 00:00' - expect(page).to have_content('Your order cycle has been updated.') + click_button "Save" + expect(page).to have_field "order_cycle_orders_close_at", with: "2024-03-30 00:00" + expect(page).to have_content "Your order cycle has been updated." end end end From f74492190d12eb661653081c99e84f7b5ee02554 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 19 Dec 2024 16:39:49 +1100 Subject: [PATCH 4/5] Select time like the user does It makes it easier to understand. It didn't improve stability. --- spec/support/features/datepicker_helper.rb | 6 ++---- spec/system/admin/order_cycles/edit_spec.rb | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/support/features/datepicker_helper.rb b/spec/support/features/datepicker_helper.rb index e86e32a2f95..4244da3b999 100644 --- a/spec/support/features/datepicker_helper.rb +++ b/spec/support/features/datepicker_helper.rb @@ -42,11 +42,9 @@ def select_date_from_datepicker(date) end def select_datetime_from_datepicker(datetime) - ## First of all select date select_date_from_datepicker(datetime) - # Then select time - find(".flatpickr-calendar.open .flatpickr-hour").set datetime.strftime("%H").to_s.strip - find(".flatpickr-calendar.open .flatpickr-minute").set datetime.strftime("%M").to_s.strip + fill_in "Hour", with: datetime.strftime("%H") + fill_in "Minute", with: datetime.strftime("%M") end def pick_datetime(calendar_selector, datetime_selector) diff --git a/spec/system/admin/order_cycles/edit_spec.rb b/spec/system/admin/order_cycles/edit_spec.rb index a5a6e8b4210..47a03e87f9e 100644 --- a/spec/system/admin/order_cycles/edit_spec.rb +++ b/spec/system/admin/order_cycles/edit_spec.rb @@ -89,12 +89,13 @@ expect(page).to have_content "Your order cycle has been updated." expect(page).to have_field "order_cycle_name", with: "OC1 name updated" - select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00" + select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 01:20" expect(page).to have_content "You have unsaved changes" click_button "Save" - expect(page).to have_field "order_cycle_orders_close_at", with: "2024-03-30 00:00" + expect(page).not_to have_content "You have unsaved changes" expect(page).to have_content "Your order cycle has been updated." + expect(page).to have_field "order_cycle_orders_close_at", with: "2024-03-30 01:20" end end end From c1f8d3035a22ff19a62e4235fad5c2abb7c48569 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 19 Dec 2024 16:39:49 +1100 Subject: [PATCH 5/5] Sleep after selecting time for stability --- spec/support/features/datepicker_helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/support/features/datepicker_helper.rb b/spec/support/features/datepicker_helper.rb index 4244da3b999..1fb7748188f 100644 --- a/spec/support/features/datepicker_helper.rb +++ b/spec/support/features/datepicker_helper.rb @@ -45,6 +45,14 @@ def select_datetime_from_datepicker(datetime) select_date_from_datepicker(datetime) fill_in "Hour", with: datetime.strftime("%H") fill_in "Minute", with: datetime.strftime("%M") + + # Flatpickr needs time to update the time. + # Otherwise submitting the form may not work. + # CI experimentation: 10ms -> 7% success + # 50ms -> 87% success + # 100ms -> 100% success in 112 runs + # Let's double that to reduce flakiness even further. + sleep 0.2 end def pick_datetime(calendar_selector, datetime_selector)