From 0aefab53d9c9d29c2dc907dc85d77230107d097e Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Fri, 4 Dec 2026 11:01:52 +0200 Subject: [PATCH] Attributes table row order, empty markers, form checks; 2.0.0.beta3 Adds AttributesTable#row_keys and the empty-value checks (empty?, has_empty_row?, has_no_empty_row?), plus Form#has_input? and #has_submit?. The field check is named has_input? because a section's has_field? is Capybara's own lookup by label. A table in a panel needs no helper: the README shows it declared as a section inside the panel's section. Co-Authored-By: Clanker --- CHANGELOG.md | 9 +++ README.md | 20 ++++++ .../attributes_table.rb | 38 ++++++++++++ lib/active_admin_page_objects/form.rb | 31 ++++++++++ lib/active_admin_page_objects/selectors.rb | 1 + lib/active_admin_page_objects/version.rb | 2 +- .../attributes_table_spec.rb | 61 +++++++++++++++++++ spec/active_admin_page_objects/form_spec.rb | 19 ++++++ spec/active_admin_page_objects/panel_spec.rb | 18 ++++++ spec/active_admin_page_objects/text_spec.rb | 2 +- spec/dummy/app/admin/documents.rb | 13 ++++ spec/support/pages.rb | 13 ++++ 12 files changed, 225 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1b357..8abdd5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 2.0.0.beta3 + +### Added + +- `AttributesTable#row_keys`: the row keys in page order. +- `AttributesTable#empty?(key)`, `#has_empty_row?(key)` and `#has_no_empty_row?(key)`: whether a row shows ActiveAdmin's empty marker for a blank value. +- `Form#has_input?(name)` and `#has_no_input?(name)`: whether the field of `f.input :name` is on the page. A form that declares its fields takes only declared names. +- `Form#has_submit?(label = nil)` and `#has_no_submit?`: the form's submit button, by label ignoring case. + ## 2.0.0.beta2 ### Added diff --git a/README.md b/README.md index 59bc4e0..4f09163 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,24 @@ expect(table).to have_row(:name, exact_text: "Acme") expect(table).to have_no_row(:secret) table.row(:category) # the table.value(:email) # => "acme@example.com" +table.row_keys # => ["name", "email", ...], in page order +expect(table).to have_empty_row(:category) # ActiveAdmin's empty marker; table.empty?(:category) does not wait +``` + +A page with several attributes tables or a `table_for` in a panel declares the table inside its panel's section: + +```ruby +class AccountShow < ActiveAdminPageObjects::Page + section :details, ActiveAdminPageObjects::Panel, :active_admin_panel, "Details" do + section :attributes_table, ActiveAdminPageObjects::AttributesTable + end + section :users, ActiveAdminPageObjects::Panel, :active_admin_panel, "Users" do + section :table, ActiveAdminPageObjects::Table + end +end + +show.details.attributes_table.value(:state) +expect(show.users.table).to have_row_with(:email, "admin@example.com") ``` ### Panel @@ -373,6 +391,8 @@ form = form_page.form # section :form, ActiveAdminPageObjects::Form form.field(:email).fill("acme@example.com") form.fill(name: "Acme", category: "Retail", active: false) # in this order form.submit +expect(form).to have_submit("Create Customer") # the submit button, ignoring case +expect(form).to have_no_input(:password) # the field of `f.input :password` is absent ``` `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`. The field ids and names start with the form's object name, which the gem reads from the first control named like `customer[name]`. When a form names its fields otherwise, set it on the form class: `object_name "batch_action_form_inputs"`. diff --git a/lib/active_admin_page_objects/attributes_table.rb b/lib/active_admin_page_objects/attributes_table.rb index 26ec518..6891c70 100644 --- a/lib/active_admin_page_objects/attributes_table.rb +++ b/lib/active_admin_page_objects/attributes_table.rb @@ -46,11 +46,49 @@ def value(key, visible: nil) text_of(find(:css, cell_selector(key), **visibility), visibility).strip end + # The row keys in page order, as `data-row` writes them: ["id", "uuid"]. + # A row without a title has no key and is left out. + # @return [Array] + def row_keys(visible: nil) + page_element_keys(**given(visible: visible)) + end + + # True when row +key+ shows ActiveAdmin's empty marker for a blank value. + # Does not wait. A hidden row is found with `visible: :all`. + # @raise [Capybara::ElementNotFound] when there is no such row + def empty?(key, visible: nil) + visibility = visibility_of(key, visible) + empty_marker_in?(find(:css, cell_selector(key), wait: false, **visibility), visibility) + end + + # True when row +key+ shows the empty marker. Takes `visible:` and `wait:`. + def has_empty_row?(key, visible: nil, **options) + visibility = visibility_of(key, visible) + eventually(**options) { empty_row?(key, visibility) } + end + + # Waits until row +key+ shows no empty marker, or is gone. + def has_no_empty_row?(key, visible: nil, **options) + visibility = visibility_of(key, visible) + eventually(**options) { !empty_row?(key, visibility) } + end + # `has_declared_rows?`, `has_exact_rows?` and their `has_no_` forms. Declarations.define_predicates(self, :rows) private + def empty_row?(key, visibility) + cell = first(:css, cell_selector(key), minimum: 0, wait: false, **visibility) + !cell.nil? && empty_marker_in?(cell, visibility) + end + + # Looks inside the cell, so a theme's marker selector with a comma stays + # scoped to the row. + def empty_marker_in?(cell, visibility) + cell.has_css?(sel(:attributes_table_empty_value), wait: false, **visibility) + end + def row_selector(key) declaration_for(key) sel(:attributes_table_row, slug(key)) diff --git a/lib/active_admin_page_objects/form.rb b/lib/active_admin_page_objects/form.rb index f574f33..4f9f8ea 100644 --- a/lib/active_admin_page_objects/form.rb +++ b/lib/active_admin_page_objects/form.rb @@ -134,6 +134,30 @@ def submit find(:active_admin, :form_submit, match: :first).click end + # True when the field +name+ (`f.input :name`) is on the page. Not named + # has_field?, which is Capybara's lookup by label, id or name. A form that + # declares its fields takes only declared names, so declare a field that + # may be absent. + def has_input?(name, **options) + has_css?(field_selector(name), **visibility_of(name), **options) + end + + # Waits until the field +name+ is not on the page. + def has_no_input?(name, **options) + has_no_css?(field_selector(name), **visibility_of(name), **options) + end + + # True when the form has a submit button, or one labelled +label+ + # (ignoring case): "Create Customer". + def has_submit?(label = nil, **options) + eventually(**options) { submit_labels.any? { |text| label.nil? || same_text?(text, label) } } + end + + # Waits until the form has no submit button, or none labelled +label+. + def has_no_submit?(label = nil, **options) + eventually(**options) { submit_labels.none? { |text| label.nil? || same_text?(text, label) } } + end + # The `has_many` block of +association+, e.g. `has_many(:line_items)`. A form # that declares has_many blocks takes the declared name. A hidden block is # found with `visible: :all`. @@ -207,6 +231,13 @@ def semantic_error_expectation(messages) private + # An `` is labelled by its value, a `