vaadin / testbench

Vaadin TestBench is a tool for automated user interface testing of Vaadin applications.
https://vaadin.com/testbench
Other
20 stars 22 forks source link

Add missing RouterLinkTester for RouterLink #1805

Open joelpop opened 2 weeks ago

joelpop commented 2 weeks ago

RouterLink is missing its "RouterLinkTester" ComponentTester.

Attempting to use AnchorTester in its place (analogous to using AnchorElement in e2e testing), such as

    test(find(Anchor.class)
            .withText("See previous visitors →")
            .single());

results in an error something like:

java.util.NoSuchElementException: /hello: No visible Anchor in HelloView[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing'] matching Anchor and text='See previous visitors →': []. Component tree:
└── HelloView[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing']
    └── HorizontalLayout[#hello-view, @style='justify-content:center;align-items:baseline', @theme='spacing']
        ├── TextField[label='Name', value='', placeholder='your name', clearButtonVisible='true']
        ├── Button[caption='Greet', @theme='primary']
        └── RouterLink[text='See previous visitors →']

(Using RouterLink.class in place of Anchor.class results in a compilation error.)

joelpop commented 1 week ago

This is probably all that is necessary.

/**
 *
 * Tester for RouterLink components.
 *
 * @param <T>
 *            component type
 */
@Tests(RouterLink.class)
public class RouterLinkTester<T extends RouterLink> extends ComponentTester<T> {

    /**
     * Wrap given component for testing.
     *
     * @param component target component
     */
    public RouterLinkTester(T component) {
        super(component);
    }

    /**
     * Gets the registered route class for the router-link.
     * Returns an empty optional if there is no corresponding navigation target.
     *
     * @return an {@link Optional} containing the navigation target class or empty if not found
     */
    public Optional<Class<? extends Component>> getRoute() {
        ensureComponentIsUsable();

        return RouteConfiguration.forSessionScope()
                .getRoute(getComponent().getHref());
    }

    /**
     * Click the router-link for navigation.
     *
     * @return navigated view
     */
    public Component click() {
        return getRoute()
                .map(navigationTarget -> UI.getCurrent().navigate(navigationTarget).orElseThrow(IllegalStateException::new))
                .orElseThrow(IllegalStateException::new);
    }
}