mvysny / karibu-testing

Vaadin Server-Side Browserless Containerless Unit Testing
Apache License 2.0
105 stars 14 forks source link

testing Dialog #166

Closed avdhootu27 closed 3 months ago

avdhootu27 commented 3 months ago

I am testing a method which opens a dialog by myDialog.open() and I am checking it as _get(Dialog.class) But getting `java.lang.AssertionError: /my-view: No visible Dialog in UI[] matching Dialog

Is there any different way to test Dialog? I am using karibu-testing-v10-spring with vaadin 14.

mvysny commented 3 months ago

The dialog should append itself as a child to the UI upon open, therefore _get(Dialog.class) should be able to locate it. Can you observe this behavior?

avdhootu27 commented 3 months ago

The dialog should append itself as a child to which component? Since dialog is shown on the top of all things who can be its parent?

mvysny commented 3 months ago

The UI itself.

avdhootu27 commented 3 months ago

Can you please elaborate what do you mean by "Can you observe this behavior?", The dialog opens and I can see it when the app is running, but in tests it doesn't get append to UI, and I am not getting any errors

mvysny commented 3 months ago

Sure thing. Say you have this kind of a test:

    @Test
    public void testGreeting() {
        MockVaadin.setup();
        new Dialog().open();
        MockVaadin.clientRoundtrip();
        System.out.println(PrettyPrintTreeKt.toPrettyTree(UI.getCurrent()));
    }

It should print the following tree:

└── MockedUI[]
    └── Dialog[]

Note that the Dialog component attaches itself as the direct child of the UI (represented by the MockedUI component). Also note the clientRoundtrip() function - dialog attaches itself lazily by default, and clientRoundtrip() forces eager attachment.

In your case, can you see the Dialog component in the component dump?

If not, that might be a bug in Karibu-Testing. I'll need ideally a very simple project which reproduces the issue.

avdhootu27 commented 3 months ago

Thanks. It worked. I just added MockVaadin.clientRoundtrip() and _get(Dialog.class) was able to locate it. I didn't know about MockVaadin.clientRoundtrip(). I am closing this issue now.

mvysny commented 3 months ago

What's strange is that _get() should call MockVaadin.clientRoundtrip() automatically - you should not have to worry about it. I'll rather reopen this ticket and investigate.

avdhootu27 commented 3 months ago

Ok. FYI, while closing the dialog I needed to use MockVaadin.clientRoundtrip()

mvysny commented 3 months ago

I've upgraded Karibu-Testing testing suite to Vaadin 14.11.5 and ran the test suite. The test suite is really extensive and tests all sorts of scenarios, including the Dialogs. The tests seem to pass and I don't have to call MockVaadin.clientRoundtrip() manually. In other words, I can't reproduce the issue. I'll need the simplest possible app/test case which reproduces the issue.

Side question: Maybe you have hooked into TestingLifecycleHook and removed the call to clientRoundtrip()?

avdhootu27 commented 3 months ago

Right, I have hooked into TestingLifecycleHook, I didn't know that caused it. Thanks

mvysny commented 3 months ago

Alrighty, no worries. I've updated the docs to stress out the importance of calling the original hook.

avdhootu27 commented 3 months ago

Is there any case in which we get NullPointerException on MockVaadin.clientRoundtrip() ?