testbench icon indicating copy to clipboard operation
testbench copied to clipboard

first() is fundamentally broken

Open mvysny opened this issue 5 months ago • 6 comments

The expression $(ButtonWrapper.class).first() should fundamentally not be used in a test: it promotes creation of flaky tests. If there are multiple Buttons on the web page, TestBench selects an arbitrary one and then the test may (or may not) fail later on, after the button is clicked and the outcome is something else than the test intended.

Instead, there should be a get() function which asserts that there's exactly one Button, failing if there are multiple matching Buttons. That's exactly what Playwright is doing: at https://playwright.dev/java/docs/writing-tests the page.getByRole(xyz) will fail if there are multiple matching elements.

first() should be deprecated and replaced by get() (which selects one), or get(int) which returns a list of elements and asserts that it's of that particular size.

mvysny avatar Jan 30 '24 13:01 mvysny