Files
discourse/spec/system/page_objects/components/select_kit.rb
Alan Guo Xiang Tan c0c6c55809 DEV: Make expanding select-kit more reliable in system tests (#31868)
Before this commit,
`PageObjects::Components::SelectKit#expanded_component` might not expand
the component if `is_collapsed?` returns true which can return true if
the method was called before the select-kit component appears on the
screen. When that happens, we end up not expanding the `select-kit`
because we think it is collapsed when in fact the select-kit component
is not rendered yet. This lead to system test failures with errors like:

```
Capybara::ElementNotFound:
  Unable to find css "#add-synonyms.is-expanded"
```

This commit updates `PageObjects::Components::SelectKit#is_collapsed?`
to check that the select-kit component has rendered first before
checking if the component is not expanded.
### Reviewer notes

Instances of test flakiness due to this bug:

1.
https://github.com/discourse/discourse/actions/runs/13905226478/job/38906569777
2.
https://github.com/discourse/discourse/actions/runs/13848357836/job/38751122333
2025-03-18 16:46:39 +08:00

105 lines
2.5 KiB
Ruby

# frozen_string_literal: true
module PageObjects
module Components
class SelectKit < PageObjects::Components::Base
attr_reader :context
def initialize(context)
@context = context
end
def component
if @context.is_a?(Capybara::Node::Element)
@context
else
find(@context)
end
end
def visible?
has_css?(@context)
end
def hidden?
has_no_css?(@context)
end
def expanded_component
return expand if is_collapsed?
find(@context + ".is-expanded")
end
def collapsed_component
find(@context + ":not(.is-expanded)")
end
def is_expanded?
has_css?(context + ".is-expanded")
end
def is_collapsed?
has_css?(context) && has_css?("#{context}:not(.is-expanded)", wait: 0)
end
def is_not_disabled?
has_css?(@context + ":not(.disabled)", wait: 0)
end
def value
component.find(".select-kit-header")["data-value"]
end
def has_selected_value?(value)
component.find(".select-kit-header[data-value='#{value}']")
end
def has_selected_name?(name)
component.find(".select-kit-header[data-name='#{name}']")
end
def has_selected_choice_name?(name)
component.find(".selected-choice[data-name='#{name}']")
end
def has_option_name?(name)
component.find(".select-kit-collection li[data-name='#{name}']")
end
def has_option_value?(value)
component.find(".select-kit-collection li[data-value='#{value}']")
end
def has_no_option_value?(value)
component.has_no_css?(".select-kit-collection li[data-value='#{value}']")
end
def expand
collapsed_component.find(".select-kit-header", visible: :all).click
expanded_component
end
def collapse
expanded_component.find(".is-expanded .select-kit-header").click
collapsed_component
end
def search(value = nil)
expanded_component.find(".select-kit-filter .filter-input").fill_in(with: value)
end
def select_row_by_value(value)
expanded_component.find(".select-kit-row[data-value='#{value}']").click
end
def select_row_by_name(name)
expanded_component.find(".select-kit-row[data-name='#{name}']").click
end
def select_row_by_index(index)
expanded_component.find(".select-kit-row[data-index='#{index}']").click
end
end
end
end