From e19f0ca796e77b6e1bd106a08e0b534886c72de3 Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Wed, 2 Dec 2026 16:02:52 +0200 Subject: [PATCH] Find fieldsets after errors and comments in hidden tabs Co-Authored-By: Clanker --- CHANGELOG.md | 7 +++++++ lib/active_admin_page_objects/comments.rb | 7 +++++-- lib/active_admin_page_objects/has_many.rb | 4 +++- lib/active_admin_page_objects/version.rb | 2 +- spec/active_admin_page_objects/comments_spec.rb | 14 ++++++++++++++ spec/active_admin_page_objects/has_many_spec.rb | 13 ++++++++++++- spec/active_admin_page_objects/text_spec.rb | 2 +- spec/dummy/app/admin/customers.rb | 9 ++++++--- 8 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0310fed..c395c56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 1.2.1 (2026-12-02) + +### Fixed + +- `HasMany#fieldset(label:)` finds a fieldset after a validation error, when Rails wraps the field's label in `div.field_with_errors`. +- `Comments#add` passes when the comments panel sits in a tab that the reload hides: it checks the new comment's text whether or not it is visible. + ## 1.2.0 (2026-12-02) ### Added diff --git a/lib/active_admin_page_objects/comments.rb b/lib/active_admin_page_objects/comments.rb index c3a2c63..29baf64 100644 --- a/lib/active_admin_page_objects/comments.rb +++ b/lib/active_admin_page_objects/comments.rb @@ -41,8 +41,11 @@ def add(text) find(:active_admin, :comment_input).set(text) session = root_element.session find(:active_admin, :comment_submit).click - # The submit reloads the page, so this panel's node goes stale: look in the new page. - session.assert_selector(:css, "#{sel(:comments)} #{sel(:comment_body)}", text: text) + # The submit reloads the page, so this panel's node goes stale: look in the + # new page. The panel may sit in a tab that the reload hides. + session.assert_selector(:css, "#{sel(:comments)} #{sel(:comment_body)}", visible: :all) do |body| + body.text(:all).include?(text) + end end # Deletes the comment whose body contains +text+, accepting the browser confirmation. diff --git a/lib/active_admin_page_objects/has_many.rb b/lib/active_admin_page_objects/has_many.rb index 7a3e66a..efadb4f 100644 --- a/lib/active_admin_page_objects/has_many.rb +++ b/lib/active_admin_page_objects/has_many.rb @@ -146,8 +146,10 @@ def labelled_fieldset(label) # The container's own fieldsets, and their own field labels (a radio # group's too), so a nested has_many or a check box choice never matches. + # Rails' field_error_proc wraps the label of a field with an error in + # `div.field_with_errors`. OWN_FIELDSETS_XPATH = "./fieldset[contains(concat(' ', normalize-space(@class), ' '), ' has_many_fields ')]" - OWN_LABELS_XPATH = "./ol/li/label | ./ol/li/fieldset/legend/label" + OWN_LABELS_XPATH = "./ol/li/label | ./ol/li/div/label | ./ol/li/fieldset/legend/label" private_constant :OWN_FIELDSETS_XPATH, :OWN_LABELS_XPATH def find_labelled_fieldset(label) diff --git a/lib/active_admin_page_objects/version.rb b/lib/active_admin_page_objects/version.rb index a0cef33..8506718 100644 --- a/lib/active_admin_page_objects/version.rb +++ b/lib/active_admin_page_objects/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActiveAdminPageObjects - VERSION = "1.2.0" + VERSION = "1.2.1" end diff --git a/spec/active_admin_page_objects/comments_spec.rb b/spec/active_admin_page_objects/comments_spec.rb index 8e6a3d7..b715b07 100644 --- a/spec/active_admin_page_objects/comments_spec.rb +++ b/spec/active_admin_page_objects/comments_spec.rb @@ -47,6 +47,20 @@ def create_comment(body) Capybara.automatic_reload = true end + # After the reload, the first tab is open and the comments tab is hidden. + it "adds a comment in a tab that is hidden after the reload" do + tabbed = Class.new(ActiveAdminPageObjects::Page) do + set_url "/admin/customers/{id}{?query*}" + section :comments, ActiveAdminPageObjects::Comments + section :tabs, ActiveAdminPageObjects::Tabs + end.new + tabbed.load(id: customer.id, query: { comments_in_tab: 1 }) + tabbed.tabs.switch("Comments") + tabbed.comments.add("In a tab") + expect(tabbed.tabs.current).to eq("Overview") + expect(ActiveAdmin::Comment.pluck(:body)).to eq(["In a tab"]) + end + it "reads existing comments" do create_comment("First comment") create_comment("Second comment") diff --git a/spec/active_admin_page_objects/has_many_spec.rb b/spec/active_admin_page_objects/has_many_spec.rb index 481cc02..18e6ade 100644 --- a/spec/active_admin_page_objects/has_many_spec.rb +++ b/spec/active_admin_page_objects/has_many_spec.rb @@ -17,10 +17,12 @@ def declared_orders(&) end describe "a fieldset by the label of one of its fields" do + let(:customer) { create_customer(status: "pending", external_id: "LABELLED-ORDERS") } + before do create_order("REF-1", "paid", 2) create_order("REF-B1", "new", 1) - form_page.load(id: customer.id, action: "edit", query: { labelled_orders: 1 }) + form_page.load(id: customer.id, action: "edit") end it "finds it ignoring case and the required marker" do @@ -34,6 +36,15 @@ def declared_orders(&) expect { orders.fieldset }.to raise_error(ArgumentError) end + # Rails wraps the label of a field with an error in `div.field_with_errors`. + it "finds it after a validation error" do + orders.fieldset(label: "REF-B1").field(:quantity).fill("0") + form.submit + expect(form).to have_field_errors + expect(page).to have_css("div.field_with_errors > label") + expect(orders.fieldset(label: "REF-B1").field(:quantity).value).to eq("0") + end + it "looks at the fieldset's own fields, not at a nested has_many" do page.execute_script(<<~JS) document.querySelector("fieldset.has_many_fields > ol").insertAdjacentHTML("beforeend", diff --git a/spec/active_admin_page_objects/text_spec.rb b/spec/active_admin_page_objects/text_spec.rb index b87dd0e..6a5417b 100644 --- a/spec/active_admin_page_objects/text_spec.rb +++ b/spec/active_admin_page_objects/text_spec.rb @@ -66,6 +66,6 @@ end it "has a version" do - expect(ActiveAdminPageObjects::VERSION).to eq("1.2.0") + expect(ActiveAdminPageObjects::VERSION).to eq("1.2.1") end end diff --git a/spec/dummy/app/admin/customers.rb b/spec/dummy/app/admin/customers.rb index a482c4a..80e888b 100644 --- a/spec/dummy/app/admin/customers.rb +++ b/spec/dummy/app/admin/customers.rb @@ -116,8 +116,10 @@ column :state end end + # `?comments_in_tab=1` moves the comments into a tab that is hidden on load. + tab("Comments") { active_admin_comments } if params[:comments_in_tab] end - active_admin_comments + active_admin_comments unless params[:comments_in_tab] end form do |f| @@ -150,8 +152,9 @@ o.input :tags, as: :check_boxes, collection: %w[Gift Rush] if f.object.external_id == "ORDER-TAGS" o.input :reference o.input :state, as: :select, collection: Order::STATES - # `?labelled_orders=1` labels each quantity with its order's reference. - quantity_label = params[:labelled_orders] ? { label: o.object.reference.to_s, required: true } : {} + # A customer with external id "LABELLED-ORDERS" labels each quantity with + # its order's reference, also on the form re-rendered with errors. + quantity_label = f.object.external_id == "LABELLED-ORDERS" ? { label: o.object.reference.to_s, required: true } : {} o.input :quantity, **quantity_label, input_html: { disabled: params[:lock_quantity].present? } end f.actions