Skip to content

test(button).click() does not work with buttons rendered in Grid cells. #1966

@TatuLund

Description

@TatuLund

Lets consider a following code in a test:

        var component = grid_.getCellComponent(0, 5);
        var button = $(Button.class).withCaption("Edit").first();
        test(button).click();

While the view has something like this:

        grid.addComponentColumn(address -> {
            Button editButton = new Button("Edit");
            editButton.addClickListener(event -> {
                // Edit code here
            });
            Button deleteButton = new Button("Delete");
            deleteButton.addClickListener(event -> {
                // Delete code here
            });
            HorizontalLayout actions = new HorizontalLayout(editButton,
                    deleteButton);
            return actions;
        }).setHeader("Actions").setWidth("220px");

The test will fail in error "Button[caption=‘Edit’] is not usable"

See also: https://vaadin.com/forum/t/vaadintestbench-grid-componentrenderer-problem/164902/2

This happens because GridTester just calls the component creator lambda, but does not attach the resulting component anywhere, hence the button.isAttached() is false, as the parent HorizontalLayout is not attached.

The problem is possibly not limited to ButtonTester failing, but anything in rendered components may fail in test, if the case requires the component to be attached, most importantly the onAttach is not going to be triggered.

Workaround

Use button.click() instead of test(button).click();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions