Class CardTester<T extends Card>

java.lang.Object
com.vaadin.browserless.ComponentTester<T>
com.vaadin.flow.component.card.CardTester<T>
Type Parameters:
T - component type
All Implemented Interfaces:
Clickable<T>

public class CardTester<T extends Card> extends ComponentTester<T>
Tester for Card components.

Gives access to the content of the card as the user sees it. A card that has a header component shows the header instead of the title and subtitle, so the title and subtitle accessors reject that case rather than reporting content that is not on screen.

The accessors return the component sitting in a slot. To reach a component nested deeper inside a slot, use findInHeader(Class), findInFooter(Class) or find(Class), which searches every slot of the card and not only its default content slot.

  • Constructor Details

    • CardTester

      public CardTester(T component)
      Wrap given component for testing.
      Parameters:
      component - target component
  • Method Details

    • getTitleAsText

      public String getTitleAsText()
      Gets the title text shown on the card.
      Returns:
      the title text, or an empty string if no textual title is set
      Throws:
      IllegalStateException - if the card is not visible to the user, or if a header component is set so the title is not shown
    • getTitle

      @Nullable public @Nullable Component getTitle()
      Gets the component used as the card's title.
      Returns:
      the title component, or null if the title is not a component
      Throws:
      IllegalStateException - if the card is not visible to the user, or if a header component is set so the title is not shown
    • getSubtitle

      @Nullable public @Nullable Component getSubtitle()
      Gets the component used as the card's subtitle.
      Returns:
      the subtitle component, or null if no subtitle is set
      Throws:
      IllegalStateException - if the card is not visible to the user, or if a header component is set so the subtitle is not shown
    • getSubtitleAsText

      @Nullable public @Nullable String getSubtitleAsText()
      Gets the subtitle text shown on the card.

      Card.setSubtitle(String) wraps the text in a Span, so a subtitle set as text is reported here, and so is the text of a Span set with Card.setSubtitle(Component) ? the card stores the two the same way and cannot tell them apart. A subtitle set as any other component is not text and reports null; use getSubtitle() to get the component itself.

      Note that this differs from getTitleAsText(), which reports an empty string rather than null when the card shows no textual title, because a card keeps its title text in a property of its own instead of in a component.

      Returns:
      the subtitle text, or null if no subtitle is set or the subtitle is not a text subtitle
      Throws:
      IllegalStateException - if the card is not visible to the user, or if a header component is set so the subtitle is not shown
    • getHeader

      @Nullable public @Nullable Component getHeader()
      Gets the component used as the card's header. When set, the header is shown instead of the title and subtitle.
      Returns:
      the header component, or null if no header is set
      Throws:
      IllegalStateException - if the card is not visible to the user
    • getHeaderPrefix

      @Nullable public @Nullable Component getHeaderPrefix()
      Gets the component shown before the card's header content.
      Returns:
      the header prefix component, or null if none is set
      Throws:
      IllegalStateException - if the card is not visible to the user
    • getHeaderSuffix

      @Nullable public @Nullable Component getHeaderSuffix()
      Gets the component shown after the card's header content.
      Returns:
      the header suffix component, or null if none is set
      Throws:
      IllegalStateException - if the card is not visible to the user
    • getMedia

      @Nullable public @Nullable Component getMedia()
      Gets the component shown as the card's media.
      Returns:
      the media component, or null if none is set
      Throws:
      IllegalStateException - if the card is not visible to the user
    • getFooterComponents

      public List<Component> getFooterComponents()
      Gets the components shown in the card's footer, in the order they were added.
      Returns:
      the footer components, empty if the footer is empty
      Throws:
      IllegalStateException - if the card is not visible to the user
    • find

      public <R extends Component> ComponentQuery<R> find(Class<R> componentType)
      Gets a ComponentQuery to search for component of the given type nested inside the wrapped component.

      The query walks the server-side component tree. A component that another component renders per item, such as the component a ComponentRenderer column renders for a grid row, is rendered into the column and not into the tree, and the content of an overlay, such as a context menu, is attached only while the overlay is open. Neither is reachable this way, and the lookup returns an empty result rather than failing, so reach those components through the owning component tester instead: GridTester.getCellComponent(row, column) for grid cells, ContextMenuTester.open() and then clickItem(...) for a context menu, and GridTester.contextMenu(row).open() for a grid context menu.

      Searches the whole card, so components in the title, subtitle, header, header prefix, header suffix, media and footer slots are found as well as the ones in the default content slot. Example usage:

       // view:
       Card card = new Card();
       card.setHeaderSuffix(new Button("Dismiss"));
       card.addToFooter(new Button("Book"));
      
       // test:
       CardTester<Card> cardTester = test(view.card);
       cardTester.find(Button.class).withText("Dismiss").single().click();
       Button book = cardTester.find(Button.class).withText("Book").single();
       

      To narrow the search to the header or the footer, use findInHeader(Class) or findInFooter(Class). Any other slot is reachable with ComponentQuery.withinSlot(String), for example find(Button.class).withinSlot("header-suffix").

      The default content slot has no name, so it has no such filter: components added with Card.add(...) carry no slot attribute at all and are singled out with withoutAttribute("slot"), which ? unlike ComponentQuery.withinSlot(String) ? only sees the components sitting directly in the slot, not the ones nested inside them.

      Overrides:
      find in class ComponentTester<T extends Card>
      Type Parameters:
      R - type of the component to search.
      Parameters:
      componentType - type of the component to search.
      Returns:
      a ComponentQuery instance, searching for wrapped component children.
    • findInHeader

      public <R extends Component> ComponentQuery<R> findInHeader(Class<R> componentType)
      Searches the card's header for components of the given type.

      The header is the component set with Card.setHeader(Component), shown instead of the title and subtitle. Components nested inside it are found too, so a button inside a header layout is reached as well as a button set as the header itself.

      Type Parameters:
      R - the type of the components to search for
      Parameters:
      componentType - the type of the components to search for
      Returns:
      a query for components of the given type in the card's header
      Throws:
      IllegalStateException - if the card is not visible to the user
    • findInFooter

      public <R extends Component> ComponentQuery<R> findInFooter(Class<R> componentType)
      Searches the card's footer for components of the given type.

      The footer holds the components added with Card.addToFooter(Component...). Components nested inside them are found too, so a button inside a footer layout is reached as well as a button added to the footer directly.

      Type Parameters:
      R - the type of the components to search for
      Parameters:
      componentType - the type of the components to search for
      Returns:
      a query for components of the given type in the card's footer
      Throws:
      IllegalStateException - if the card is not visible to the user