Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# Changelog

## Unreleased
## 1.1.0 (2026-12-01)

### Added

- `SelectFilter#select_option` and `Form::SelectField#fill` take `search:`, the text a select widget types when it differs from the option label. It reaches `SelectControl#choose_option` only when given, so a widget module that defines `choose_option(scope, label)` keeps working; the native `<select>` ignores it.
- Flashes of any type: `have_flash(:ok_message)` finds an app's `flash[:ok_message]`, rendered as `.flash.flash_ok_message`. A type that is not a non-empty Symbol or String still raises `ArgumentError`.
- `BatchActions#open_dialog(label)` opens the dialog of an action with `confirm:` or `form:` and returns it without confirming.
- `ModalDialog#fill` picks a select through `SelectControl#choose_option`, so a select widget module drives dialog selects too, and types a date into a datepicker input.
- `Table#has_no_sortable_column?`; `has_sortable_column?` takes `wait:` and the other Capybara options.
- `HasMany::Fieldset#has_remove?` and `#has_no_remove?`: whether the fieldset has a remove link or a delete checkbox.
- `Form::SelectField#selected_options`, `#has_selected?(*labels)` and `#has_no_selected?(*labels)`, for single and multiple selects.
- `Table#footer`, `has_footer?` and `has_no_footer?`: the `<tfoot>` row, such as the totals row of activeadmin_table_footer. `table.footer.cell(:balance)` and `have_cell` read its cells by column key, as on a body row.

- `StringFilter` and `NumericFilter` drive a filter whose key holds its predicate, such as `filter :uuid_eq`: ActiveAdmin renders a bare input without a predicate select.
- `Form::Field#required?`, `#input` (the field's first control) and `#force_fill(text)`, which sets a text control's value with JavaScript past its `maxlength`.
- `Table#row_with(key, text)`, `#has_row_with?` and `#has_no_row_with?` find a row by a cell's text, for tables without record ids.
- `MainMenu#href(*labels)`, and `has_item?` and `has_no_item?` take `href:`: a path, a full URL or a Regexp.
- `have_semantic_errors` and `have_no_semantic_errors` take a Regexp, which matches part of a message.
- `ActiveAdminPageObjects.register_field_class(wrapper_class, field_class)` and `.register_filter_class(wrapper_class, filter_class)`: a theme or plugin maps the formtastic class of its own input to its section class, and `form.field` and `filters.filter` return that class without a declaration. A registered class wins over the gem's class.

### Fixed

- `DateRangeFilter#set` and `Form::DatePickerField#fill` close the jQuery UI datepicker through jQuery UI, blur the input, and wait until the picker is hidden and no longer animating, whatever `Capybara.ignore_hidden_elements` says. Its fade-out used to leave it over the **Filter** button, so the next click could hit the picker. Escape is sent only as a fallback without jQuery UI, and only while the picker shows: an Escape after it closed would close an enclosing modal dialog.

## 1.0.0 (2026-11-24)

### Added
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ gem "rubocop", "~> 1.80", require: false
# Dummy ActiveAdmin 3 app (spec/dummy): sprockets, jQuery from jquery-rails,
# Sass for active_admin.scss.
gem "activeadmin", "3.5.2"
# Renders the <tfoot> totals row that Table#footer reads.
gem "activeadmin_table_footer", "~> 1.0"
gem "cuprite"
gem "dartsass-sprockets"
gem "puma"
Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ expect(more).to have_item("Report")

### Flashes

ActiveAdmin 3 renders the `notice`, `alert`, `error` and `warning` types. It renders the container on every page, so assert on the content: `have_no_flash` works without flashes.
ActiveAdmin 3 renders the `notice`, `alert`, `error` and `warning` types, and any other key an app puts in `flash`, such as `flash[:ok_message]`, as `.flash.flash_ok_message`: `have_flash(:ok_message)` finds it. It renders the container on every page, so assert on the content: `have_no_flash` works without flashes.

```ruby
expect(page_object.flashes).to have_flash(:notice, "Customer was successfully created.")
Expand Down Expand Up @@ -178,8 +178,11 @@ expect(table).to have_row(customer.id) # by record id
expect(table).to have_no_row(deleted.id)
table.row_with(:server, "10.0.0.1:7080") # by a cell's whole text; a Regexp matches part of it
expect(table).to have_row_with(:status, "Failed")
expect(table.footer).to have_cell(:balance, exact_text: "30.0") # the <tfoot> row
```

`table.footer` is the `<tfoot>` row, such as the totals row that activeadmin_table_footer renders. It reads cells by column key, as a body row does, and `have_footer` and `have_no_footer` check that it is there.

`row_with`, `have_row_with` and `have_no_row_with` find rows that have no record id, such as a `table_for` of plain values. `row`, `have_row` and `have_no_row` take the record id: an Integer or a non-empty String. Anything else, such as the record itself or `nil`, raises `ArgumentError`.

Sorting reads and clicks the header links:
Expand All @@ -189,6 +192,7 @@ table.sort_by(:name) # one click
table.sort_by(:name, :asc) # clicks until the column is sorted that way
table.sorted_by # => ["name", :asc], or nil
expect(table).to have_sortable_column(:name)
expect(table).to have_no_sortable_column(:category)
```

The first click on an unsorted column sorts descending.
Expand Down Expand Up @@ -303,7 +307,7 @@ expect(filters.filter(:active)).to have_value(true)
expect(filters.filter(:created_at)).to have_range("2026-01-01", "2026-01-31")
```

`DateRangeFilter` types the dates into the two inputs. It does not use the jQuery UI datepicker. `set` takes Date objects or ISO strings; `nil` or `""` clears that end.
`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.

Declare one filter directly when a page uses it often:

Expand Down Expand Up @@ -370,6 +374,7 @@ index.batch_actions.select_all
index.batch_actions.apply(:destroy)
index.batch_actions.apply("Set Status Selected", status: "pending")
expect(index.batch_actions).to have_action("Flag Selected")
dialog = index.batch_actions.open_dialog("Set Status Selected") # unconfirmed: see Modal dialog
```

### Modal dialog
Expand All @@ -379,12 +384,14 @@ expect(index.batch_actions).to have_action("Flag Selected")
```ruby
dialog = index.modal_dialog
dialog.title # => "Are you sure you want to do this?"
dialog.fill("status", with: "pending") # input or select, by field name
dialog.fill("status", with: "pending") # input, select or datepicker, by field name
expect(dialog).to have_field("status")
dialog.confirm # or dialog.cancel
expect(index).to have_no_modal_dialog
```

A select goes through `SelectControl`, so a select widget module drives it in the dialog too. A datepicker input takes a Date or an ISO string, and the dialog stays open.

### Form

Fields are named by attribute, as in `f.input :email`. A form that [declares its fields](#declared-labels) takes the declared names.
Expand Down Expand Up @@ -412,6 +419,9 @@ Every field also has `errors`, `disabled?`, `enabled?`, `required?` and `input`,
```ruby
form.field(:status).options # => ["active", "pending", "closed"]
form.field(:status).selected # => "pending", or nil for the blank option
expect(form.field(:status)).to have_selected("pending")
form.field(:roles).selected_options # => ["Admin", "Support"], a multiple select
expect(form.field(:roles)).to have_selected("Support", "Admin") # exactly these, in any order
expect(form.field(:active)).to be_checked
form.field(:tier).selected # => "Gold", the chosen radio button
form.field(:channels).fill(%w[Email Post])
Expand Down Expand Up @@ -472,6 +482,7 @@ expect(orders.fieldset(0)).to have_exact_field_errors(quantity: "must be greater
orders.remove(2) # a new fieldset: its remove link
orders.remove(0) # a saved one: ticks its delete checkbox
expect(orders.fieldset(0)).to be_marked_for_destroy
expect(orders.fieldset(0)).to have_no_remove # no remove link and no delete checkbox
```

Declare the block on the form to declare the fields of its fieldsets: see [Declared has_many blocks](#declared-has_many-blocks).
Expand Down Expand Up @@ -885,15 +896,15 @@ The field class is a subclass of `ActiveAdminPageObjects::Form::Field` and the f

### Select widgets

`ActiveAdminPageObjects::SelectControl` drives a visible native `<select>` for `SelectFilter` and `Form::Field`. For a widget on some selects, declare your own class: see [Hidden selects](#hidden-selects). A gem that renders a JavaScript select widget for every select prepends a module into `SelectControl` instead. It overrides the private methods `choose_option(scope, label)`, `option_offered?(scope, label)`, `control_disabled?(scope)` and `chosen_item?(scope, label)`, and `native_select_visibility` for the methods that still read the hidden `<select>`:
`ActiveAdminPageObjects::SelectControl` drives a visible native `<select>` for `SelectFilter` and `Form::Field`. For a widget on some selects, declare your own class: see [Hidden selects](#hidden-selects). A gem that renders a JavaScript select widget for every select prepends a module into `SelectControl` instead. It overrides the private methods `choose_option(scope, label, search: nil)`, `option_offered?(scope, label)`, `control_disabled?(scope)` and `chosen_item?(scope, label)`, and `native_select_visibility` for the methods that still read the hidden `<select>`:

```ruby
module MyWidget
module SelectControl
private

def choose_option(scope, label)
# open the widget and click the option
def choose_option(scope, label, search: nil)
# open the widget, type search || label, and click the option
end

def native_select_visibility
Expand All @@ -904,6 +915,8 @@ end
ActiveAdminPageObjects::SelectControl.prepend(MyWidget::SelectControl)
```

`select_option` and `fill` take `search:`, the text a widget types when it differs from the option label: `filters.filter(:city).select_option("Kyiv (Kyiv Oblast)", search: "Kyiv")`. They pass it to `choose_option` only when a spec gives it, and the native `<select>` ignores it.

## Themes and plugins

A theme or plugin ships its own page objects in its own repository, with one entry point. The rule:
Expand Down
38 changes: 27 additions & 11 deletions lib/active_admin_page_objects/batch_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ def select_all
# dialog by field name (`status: "pending"` picks the option of a select,
# or types into an input) and clicks OK.
def apply(label, inputs = {})
button = find(:active_admin, :batch_actions_button)
# The button toggles the menu, so leave an open menu open.
button.click unless has_css?(sel(:batch_actions_menu), wait: false)
item = action_item(label)
opens_dialog = !item[:"data-confirm"].to_s.empty?
item.click
confirm_dialog(inputs) if opens_dialog
item = open_menu_item(label)
return item.click if item[:"data-confirm"].to_s.empty?

dialog = click_for_dialog(item)
inputs.each { |name, value| dialog.fill(name, with: value) }
dialog.confirm
end

# Opens the menu, clicks the action labelled or named +label+ and returns
# its modal dialog, unconfirmed, to check or fill it.
# @raise [ArgumentError] when the action has no `confirm:` or `form:`
# @return [ModalDialog]
def open_dialog(label)
item = open_menu_item(label)
raise ArgumentError, "batch action #{label.inspect} opens no dialog" if item[:"data-confirm"].to_s.empty?

click_for_dialog(item)
end

# True when the menu has an action labelled +label+ or named +label+.
Expand All @@ -41,11 +51,17 @@ def has_no_action?(label, **options)

private

# The button toggles the menu, so leave an open menu open.
def open_menu_item(label)
button = find(:active_admin, :batch_actions_button)
button.click unless has_css?(sel(:batch_actions_menu), wait: false)
action_item(label)
end

# The dialog is appended to <body>, outside this section.
def confirm_dialog(inputs)
dialog = ModalDialog.new(self, capybara_session.find(:active_admin, :modal_dialog))
inputs.each { |name, value| dialog.fill(name, with: value) }
dialog.confirm
def click_for_dialog(item)
item.click
ModalDialog.new(self, capybara_session.find(:active_admin, :modal_dialog))
end

def action_items
Expand Down
7 changes: 4 additions & 3 deletions lib/active_admin_page_objects/filters/select_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module ActiveAdminPageObjects
class SelectFilter < Filter
include SelectControl

# Chooses the option labelled +label+.
def select_option(label)
choose_option(root_element, label)
# Chooses the option labelled +label+. +search+ is the text a select
# widget types to find it; the native <select> ignores it.
def select_option(label, search: nil)
choose_option(root_element, label, **{ search: search }.compact)
end

# True when the control offers +label+.
Expand Down
11 changes: 7 additions & 4 deletions lib/active_admin_page_objects/flashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module ActiveAdminPageObjects
class Flashes < Section
selector_key :flashes

# The types with a selector of their own, `flash_<type>`. Any other type,
# such as an app's `flash[:ok_message]`, matches `.flash.flash_<type>`.
TYPES = %i[notice alert error warning].freeze

# True when a flash of +type+ contains +text+.
# @raise [ArgumentError] when +type+ is not one of TYPES
# @raise [ArgumentError] when +type+ is not a non-empty Symbol or String
def has_flash?(type, text = nil, **options)
has_css?(flash_selector(type), **given(text: text), **options)
end
Expand All @@ -29,11 +31,12 @@ def messages(type = nil)
private

def flash_selector(type)
unless type.respond_to?(:to_sym) && TYPES.include?(type.to_sym)
raise ArgumentError, "flash type must be one of #{TYPES.inspect}, got #{type.inspect}"
unless (type.is_a?(Symbol) || type.is_a?(String)) && !type.empty?
raise ArgumentError, "flash type must be a non-empty Symbol or String, got #{type.inspect}"
end

sel(:"flash_#{type}")
key = :"flash_#{type}"
Selectors::MAP.key?(key) ? sel(key) : sel(:flash_of_type, type)
end
end
end
37 changes: 32 additions & 5 deletions lib/active_admin_page_objects/form/select_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class Form
# A select field. `fill` picks an option by its text. A subclass for a JS
# select widget overrides `native_select_visibility`.
class SelectField < Field
# Picks the option labelled +label+.
def fill(label)
choose_option(root_element, label.to_s)
# Picks the option labelled +label+. +search+ is the text a select
# widget types to find it; the native <select> ignores it.
def fill(label, search: nil)
choose_option(root_element, label.to_s, **{ search: search }.compact)
end

# The option texts, without the blank option.
Expand All @@ -16,14 +17,40 @@ def options
option_texts(option_nodes)
end

# The text of the selected option, or nil when the blank option is selected.
# The text of the selected option, or nil when the blank option is
# selected. A multiple select: the first one; see #selected_options.
# @return [String, nil]
def selected
option_texts(option_nodes.select(&:selected?)).first
selected_options.first
end

# The texts of the selected options, for a multiple select.
# @return [Array<String>]
def selected_options
option_texts(option_nodes.select(&:selected?))
end

# True when exactly +labels+ are selected, in any order: one label for a
# single select, several for a multiple select.
# @raise [ArgumentError] when no labels are given
def has_selected?(*labels)
selected_option?(root_element, selection(labels))
end

# Waits until the selected options are no longer +labels+.
# @raise [ArgumentError] when no labels are given
def has_no_selected?(*labels)
not_selected_option?(root_element, selection(labels))
end

private

def selection(labels)
raise ArgumentError, "no option labels given" if labels.empty?

labels.size == 1 ? labels.first : labels
end

def option_nodes
native_select(root_element).all("option", visible: :all, wait: false)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/active_admin_page_objects/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,29 @@ def remove
end
end

# True when the fieldset has what #remove uses: the remove link of a
# new record or the delete checkbox of a saved one.
def has_remove?(**options)
has_css?(remove_selector, **options)
end

# Waits until the fieldset has neither, as for a saved record of a
# has_many without `allow_destroy`.
def has_no_remove?(**options)
has_no_css?(remove_selector, **options)
end

# True when the delete checkbox of a saved record is ticked.
def marked_for_destroy?
has_css?(sel(:has_many_delete), wait: false) && find(:active_admin, :has_many_delete).checked?
end

private

def remove_selector
"#{sel(:has_many_remove)}, #{sel(:has_many_delete)}"
end

# Formtastic ids a field `<name with [] turned into _>`, so the prefix of
# `customer[orders_attributes][0][reference]` is `customer_orders_attributes_0`.
# A check_boxes or multiple select name ends in `[]` after the attribute.
Expand Down
12 changes: 9 additions & 3 deletions lib/active_admin_page_objects/modal_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module ActiveAdminPageObjects
# The modal dialog ActiveAdmin opens for batch actions that ask to confirm or
# for input. Needs a JavaScript driver. Reached with `page.modal_dialog`.
class ModalDialog < Section
include SelectControl

selector_key :modal_dialog

# The dialog title text.
Expand All @@ -12,12 +14,16 @@ def title
find(:active_admin, :modal_dialog_title).text.strip
end

# Sets the field named +name+: types into an input, or picks the option
# labelled +with+ in a select.
# Sets the field named +name+: types into an input, picks the option
# labelled +with+ in a select, or types a date (a Date or an ISO string)
# into a datepicker input. A select goes through SelectControl, so a
# select widget module drives it here too.
def fill(name, with:)
field = find(:active_admin, :modal_dialog_form).find(:css, field_selector(name))
if field.tag_name == "select"
field.select(with.to_s)
choose_option(field.find(:xpath, ".."), with.to_s)
elsif class_names(field).include?("datepicker")
type_date(field, iso_date(with))
else
field.set(with)
end
Expand Down
Loading
Loading