From 92cbfd13c6ccf4484e589d1cebe632b3bf284231 Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Wed, 2 Dec 2026 12:14:54 +0200 Subject: [PATCH 1/7] Add check-box filters, scope hrefs, submenu and labelled fieldsets Also fix Comments#add with automatic reload off and keep a leading underscore in Symbol field keys. Co-Authored-By: Clanker --- CHANGELOG.md | 15 +++++ README.md | 12 +++- lib/active_admin_page_objects/comments.rb | 4 +- lib/active_admin_page_objects/filters.rb | 4 +- .../filters/check_boxes_filter.rb | 63 +++++++++++++++++++ lib/active_admin_page_objects/has_many.rb | 42 ++++++++++++- lib/active_admin_page_objects/main_menu.rb | 17 +++++ lib/active_admin_page_objects/modal_dialog.rb | 13 ++-- lib/active_admin_page_objects/scopes.rb | 13 ++++ lib/active_admin_page_objects/selectors.rb | 1 + lib/active_admin_page_objects/text.rb | 5 +- .../comments_spec.rb | 11 ++++ .../active_admin_page_objects/filters_spec.rb | 28 +++++++++ spec/active_admin_page_objects/form_spec.rb | 9 +++ .../has_many_spec.rb | 19 ++++++ .../main_menu_spec.rb | 8 +++ .../modal_dialog_spec.rb | 4 ++ spec/active_admin_page_objects/scopes_spec.rb | 9 +++ spec/active_admin_page_objects/text_spec.rb | 2 + spec/dummy/app/admin/customers.rb | 8 ++- spec/dummy/app/models/customer.rb | 4 +- 21 files changed, 276 insertions(+), 15 deletions(-) create mode 100644 lib/active_admin_page_objects/filters/check_boxes_filter.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fedb1..eaede1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## Unreleased + +### Added + +- `Filters` returns a `CheckBoxesFilter` for a check_boxes filter (`filter :tier, as: :check_boxes`): `set`, `check`, `uncheck`, `options`, `checked`, and `have_checked`. +- `Scopes#href(label)` and `#hrefs`. +- `MainMenu#has_submenu?(*labels)` and `#has_no_submenu?`: whether an item is a parent. A parent links to "#", so `href:` cannot tell it apart. +- `HasMany#fieldset(label:)`, `#has_fieldset?(label:)` and `#has_no_fieldset?(label:)` find a fieldset by the label of one of its fields, ignoring case and the required "*". This fits a has_many that labels each record's input with the record's name. +- `ModalDialog#has_field?` and `#has_no_field?` take the filters of Capybara's `:field` selector, such as `with:`, `type:`, `disabled:` and `placeholder:`. + +### Fixed + +- `Comments#add` checks for the new comment in the reloaded page. With `Capybara.automatic_reload` off, it raised `ObsoleteNode` on the panel found before the submit. +- A Symbol field, column or filter key keeps a leading underscore: `field(:_memo)` finds `#customer__memo_input`. + ## 1.1.0 (2026-12-01) ### Added diff --git a/README.md b/README.md index b31d078..646cf2d 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ page_object.main_menu.go("Catalog", "Orders") page_object.main_menu.click("Catalog", "Orders") expect(page_object.main_menu).to have_item("Catalog", "Categories") expect(page_object.main_menu).to have_item("Catalog", "Orders", href: "/admin/orders") +expect(page_object.main_menu).to have_submenu("Catalog") # a parent; its link is "#" page_object.main_menu.href("Catalog", "Orders") # => "http://127.0.0.1:3000/admin/orders" in a browser page_object.main_menu.current_labels # => ["Catalog", "Orders"] page_object.main_menu.labels("Catalog") # => ["Categories", "Orders"] @@ -289,7 +290,7 @@ comments.delete("Called back") # accepts the browser confirmation ### Filters -`filter(key)` returns the section typed by the filter input: `StringFilter`, `NumericFilter`, `SelectFilter`, `BooleanFilter` or `DateRangeFilter`. The key is the one given to `filter :key`; the section wraps `#q__input`. Filters that [declare their filters](#declared-labels) take the declared name. A filter whose key holds its predicate, such as `filter :uuid_eq`, has a bare input and no predicate select: `fill_in(with:)` and `have_value` work, and a `predicate:` raises. +`filter(key)` returns the section typed by the filter input: `StringFilter`, `NumericFilter`, `SelectFilter`, `BooleanFilter`, `DateRangeFilter` or `CheckBoxesFilter`. The key is the one given to `filter :key`; the section wraps `#q__input`. Filters that [declare their filters](#declared-labels) take the declared name. A filter whose key holds its predicate, such as `filter :uuid_eq`, has a bare input and no predicate select: `fill_in(with:)` and `have_value` work, and a `predicate:` raises. ```ruby filters = index.filters @@ -298,6 +299,7 @@ filters.filter(:balance).fill_in(with: 100, predicate: :gt) filters.filter(:status).select_option("Active") filters.filter(:active).set(true) # false, or nil for "Any" filters.filter(:created_at).set("2026-01-01", Date.new(2026, 1, 31)) +filters.filter(:tier).set(%w[Gold Silver]) # check_boxes: ticks these, unticks the rest filters.submit filters.clear # needs a JavaScript driver @@ -305,6 +307,7 @@ expect(filters.filter(:name)).to have_value("Acme", predicate: "Equals") expect(filters.filter(:status)).to have_selected("Active") expect(filters.filter(:active)).to have_value(true) expect(filters.filter(:created_at)).to have_range("2026-01-01", "2026-01-31") +expect(filters.filter(:tier)).to have_checked("Gold", "Silver") ``` `DateRangeFilter` types the dates into the two inputs. It does not use the jQuery UI datepicker: it closes the picker through jQuery UI and waits until it is hidden, so the picker never covers the **Filter** button. `set` takes Date objects or ISO strings; `nil` or `""` clears that end. @@ -335,6 +338,8 @@ index.scopes.labels # => ["All", "Active"] index.scopes.select("Active") expect(index.scopes).to have_selected("Active") index.scopes.count("Active") # => 12, or nil without a count +index.scopes.href("Active") # => "/admin/customers?scope=active" +index.scopes.hrefs # => {"All" => "/admin/customers?scope=all", "Active" => "..."} ``` ### Pagination @@ -386,6 +391,7 @@ dialog = index.modal_dialog dialog.title # => "Are you sure you want to do this?" dialog.fill("status", with: "pending") # input, select or datepicker, by field name expect(dialog).to have_field("status") +expect(dialog).to have_field("status", with: "pending", disabled: false) # filters of Capybara's :field dialog.confirm # or dialog.cancel expect(index).to have_no_modal_dialog ``` @@ -403,7 +409,7 @@ form.fill(name: "Acme", category: "Retail", active: false) # in this order form.submit ``` -`field(name)` returns the section typed by the formtastic input class. `fill(value)` sets the field the way its type needs. +`field(name)` returns the section typed by the formtastic input class. `fill(value)` sets the field the way its type needs. A Symbol name stays as it is, so `field(:_memo)` finds `#customer__memo_input`. - `Form::StringField` for `string`, `email`, `url`, `phone`, `password`, `number` and `text`: `fill` types the value. `value`. - `Form::SelectField` for `select`: `fill` picks the option with that label. `options` (without the blank option) and `selected`. The `` is visible: see [Hidden selects](#hidden-selects). @@ -424,7 +428,7 @@ form.submit - `Form::FileField` for `file`: `fill` takes the path of the file and attaches it. A JavaScript driver needs an absolute path. - `Form::Field` for any other class: `fill` types text, picks a select option or ticks a checkbox, by what the wrapper holds. -Every field also has `errors`, `disabled?`, `enabled?`, `required?` and `input`, the field's first control, and `choose(option)` for a select or a radio button. `force_fill(text)` sets a text control's value with JavaScript, past its `maxlength`, so a spec reaches the server's own check. +Every field also has `errors`, `disabled?`, `enabled?`, `required?` and `input`, the field's first control, and `choose(option)` for a select or a radio button. `required?` is true when formtastic marks the wrapper `required`, or when the label holds ``. `force_fill(text)` sets a text control's value with JavaScript, past its `maxlength`, so a spec reaches the server's own check. ```ruby form.field(:status).options # => ["active", "pending", "closed"] diff --git a/lib/active_admin_page_objects/fields.rb b/lib/active_admin_page_objects/fields.rb index 6335386..ab9833b 100644 --- a/lib/active_admin_page_objects/fields.rb +++ b/lib/active_admin_page_objects/fields.rb @@ -124,8 +124,14 @@ def detectable_classes # The `name` of the first namespaced control, which the id prefix of the # wrappers is built from. That control may be hidden: a hidden input, or # the `'); + JS + expect(orders.fieldset(label: "REF-B1").field(:reference).value).to eq("REF-B1") + end + + it "keeps a trailing * that is not the required marker" do + page.execute_script(<<~JS) + document.querySelectorAll("fieldset.has_many_fields")[1].querySelector("li.input:not(.hidden) label[for$='_quantity']").textContent = "Warehouse*"; + JS + expect(orders.fieldset(label: "Warehouse*").field(:reference).value).to eq("REF-B1") + expect(orders).to have_no_fieldset(label: "Warehouse") + end end shared_examples "saved fieldsets" do diff --git a/spec/active_admin_page_objects/text_spec.rb b/spec/active_admin_page_objects/text_spec.rb index ed3b0b9..b87dd0e 100644 --- a/spec/active_admin_page_objects/text_spec.rb +++ b/spec/active_admin_page_objects/text_spec.rb @@ -21,8 +21,13 @@ it "slugs keys the way ActiveAdmin writes the col- and row- classes" do expect(ActiveAdminPageObjects.slug("EXTERNAL ID")).to eq("external_id") expect(ActiveAdminPageObjects.slug(:external_id)).to eq("external_id") - expect(ActiveAdminPageObjects.slug(:_memo)).to eq("_memo") - expect(ActiveAdminPageObjects.slug("_memo")).to eq("memo") + expect(ActiveAdminPageObjects.slug(:_memo)).to eq("memo") + end + + it "keeps a Symbol field key as formtastic ids it" do + expect(ActiveAdminPageObjects.field_slug(:_memo)).to eq("_memo") + expect(ActiveAdminPageObjects.field_slug("_memo")).to eq("memo") + expect(ActiveAdminPageObjects.field_slug("Signed up")).to eq("signed_up") expect(ActiveAdminPageObjects.slug("Signed up")).to eq("signed_up") end