Skip to content

feat: add getItemTexts to the menu testers (#242) (CP: 25.3) - #247

Merged
mcollovati merged 2 commits into
25.3from
cherry-pick-242-to-25.3-1789742352203
Sep 18, 2026
Merged

mcollovati merged 2 commits into
25.3from
cherry-pick-242-to-25.3-1789742352203

Conversation

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This PR cherry-picks changes from the original PR #242 to branch 25.3.

Original PR description

Summary

The menu testers can now tell you which items a menu shows. MenuBarTester, ContextMenuTester and GridContextMenuTester get a getItemTexts() method, so a test can assert the contents of a menu instead of guessing them.

Fixes #216

What changed

This change is purely additive — nothing existing behaves differently.

  • getItemTexts() returns the texts of the top level items, and getItemTexts(String, String...) returns the texts of a sub menu, addressed by the same text path that clickItem(String, String...) uses.
  • The texts are the ones the browser shows: hidden items are skipped at every level, so the positions match the ones clickItem(int, int...) takes.
  • An item created from a component has no text of its own and is reported as an empty string. Use find(Class) to reach such an item.
  • Reading items only requires the menu to be visible, not enabled, so the items of a disabled menu bar can still be read. The context menus still have to be open, because their items are not in the UI before that.
  • For a grid context menu, the items are the ones offered for the row the menu was opened on, so a dynamic content handler has already run.

Use case

A grid has a per-row action menu, and which actions it offers depends on the row and on the user's permissions. A developer wants to check that a read-only user is not offered "Delete", without clicking anything.

@Test
void readOnlyUser_deleteIsNotOffered() {
    GridContextMenuTester<GridContextMenu<Person>, Person> menu = test(view.menu);

    menu.open(0);

    Assertions.assertIterableEquals(List.of("View", "Export"),
            menu.getItemTexts());
    Assertions.assertIterableEquals(List.of("CSV", "PDF"),
            menu.getItemTexts("Export"));
}

API Changes

com.vaadin.flow.component.menubar.MenuBarTester

// Added
public List<String> getItemTexts() // texts of the visible top level items
public List<String> getItemTexts(String topLevelText, String... nestedItemsText) // texts of the visible items of a sub menu

com.vaadin.flow.component.contextmenu.ContextMenuTester

// Added
public List<String> getItemTexts() // texts of the visible top level items, menu has to be open
public List<String> getItemTexts(String topLevelText, String... nestedItemsText) // texts of the visible items of a sub menu

com.vaadin.flow.component.grid.contextmenu.GridContextMenuTester

// Added
public List<String> getItemTexts() // texts of the visible top level items, menu has to be open
public List<String> getItemTexts(String topLevelText, String... nestedItemsText) // texts of the visible items of a sub menu

com.vaadin.browserless.internal.MenuItemNavigation

// Added
public static List<String> visibleTexts(List<? extends MenuItemBase<?, ?, ?>> items)
public static <C extends ContextMenuBase<C, I, S>, I extends MenuItemBase<C, I, S>, S extends SubMenuBase<C, I, S>> List<String> visibleSubMenuTexts(List<I> rootItems, String topLevelText, String... nestedItemsText)

Test summary

  • Reported texts are the visible items in display order; hidden items are left out at the top level and in sub menus.
  • The returned positions line up with the ones clickItem(int, int...) uses, verified by clicking an item found by its index.
  • A component item is reported as an empty text.
  • Asking for the sub menu of an item that has none fails with a clear message, and reading items before a context menu is opened fails.
  • A disabled menu bar still reports its items.
  • For a grid context menu, the items reflect the row the menu was opened on when a dynamic content handler changes them.

The menu testers can now tell you which items a menu shows.
`MenuBarTester`, `ContextMenuTester` and `GridContextMenuTester` get a
`getItemTexts()` method, so a test can assert the contents of a menu
instead of guessing them.

Fixes #216
@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

1 517 tests   1 517 ✅  41s ⏱️
  149 suites      0 💤
  149 files        0 ❌

Results for commit 518800a.

♻️ This comment has been updated with latest results.

mcollovati
mcollovati previously approved these changes Sep 18, 2026
mcollovati added a commit that referenced this pull request Sep 18, 2026
…#251)

The context menu test that checks which item texts a menu reports was
one item short, so it failed on `main` and on the 25.3 cherry-pick
(#247). The expectation now lists every item a user sees.

The two changes involved never met in CI: the test fixture gained a
visible "Duplicated Hidden" item in #238, while #242 was written and
validated on a branch cut before that. The files merged without a
textual conflict, so the mismatch only showed up after both had
landed.
The item texts expectation in `ContextMenuTesterTest` was one item short,
so this cherry-pick branch failed CI. The list now names every item a
user sees.

The mismatch comes from two changes that never met before landing: the
test fixture gained a visible "Duplicated Hidden" item in #238, which is
already on 25.3, while #242 was written on a branch cut before that.

Cherry-pick of #251.
@mcollovati
mcollovati merged commit d93b5b0 into 25.3 Sep 18, 2026
6 checks passed
@mcollovati
mcollovati deleted the cherry-pick-242-to-25.3-1789742352203 branch September 18, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants