playwright: [BUG] "Clicking the checkbox did not change its state" on rare occasions
Context:
- Playwright Version: @playwright/test 1.17.2
- Operating System: Linux
- Node.js version: 16.14
- Browser: Chrome Headless
- Extra: Run in Bitbucket Pipelines
Code Snippet
Trying to check a checkbox with following code:
await this.page!.check(`data-test-id=iAcceptUsageOfMyData`);
will result in about 1/20 of executions to this error:
Clicking the checkbox did not change its state.
Describe the bug
The checkbox has no logic to be disabled or anything. Also, according to the Playwright trace it is perfectly visible (I would like to avoid the “force” flag if possible). Still, on about 1/10 or 1/20 executions, the test fails with the mentioned error.
The website has a sticky header at the top. Not sure if this might have any influence on the issue.
You can find a trace of a failed run here:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 26 (2 by maintainers)
I’ve tried to debug this a few times in a react app to no avail. From within the react code the checkbox state is always what I expect. I wonder if react’s change handlers are doing something that messes this up. Fwiw I’m on react 16, I’ll have to try on 18 soon and see if there’s a difference.
We work around it by doing
page.getByRole("checkbox").click()instead of.check()I’m having this issue still.
Still reproducible on “@playwright/test”: “^1.48.1” “react”: “^18.3.1” node v20.18.0 npm 10.8.2 macOS 14.7, M1
Even
click({ force: true })doesn’t work for me in all browsers, when the element I’m clicking is visible to screen readers only. These are essentially dummy elements to be controlled by the form framework, and thus they only act to deal with events and hold DOM state.There is at least one way to fix this for me without refactoring:
I’ll probably end up refactoring these dummy inputs out, but this works for now.
I just saw that basically any input action (e.g. also
await page.click()) can have the same problem. Do you have any generic advice how to solve the problem without using everywhere{force: true}?You suggested to “wait until form stops changing”. How would you do this in a framework like vue.js or Angular?
Can confirm. We are now using everywhere
click()instead ofcheck()and we don’t have any issues anymore.Just had this issue in webkit tests with styled radio groups. Webkit trace for the radio check shows consistently off-target clicks. Our current workaround is having
await page.click("text=the text on it")instead ofawait page.check("input[name=its name]", {force: true})Firefox and Chrome tests were working usingpage.check-method thoWell, I think we will now use everywhere
{force: true}as I am not sure how I can wait until the form stops changing (Especially if I don’t use internal live-cycle hooks of the framework). Generally, I think a dynamical build UI is very common nowadays, and this current default behavior of playwright is promoting flaky tests. Perhaps you can have a second thought about it, or at least add a warning to the documentation.I close this ticket now. Thank you for your help.
I experimented a little around and after little code change now the error comes reliably in all runs. This is the trace with the new code:
Alles-2022-04-15-09-08-06-trace.zip
It is interesting that the little red pointer (this is where playwright did the click right?) is completely at a wrong position. The checkbox is at the bottom of the page, but the pointer is at the top:
The form is being created by vue.js and if had to make a guess I think that playwright determines the position of the checkbox but meanwhile vue.js continues to build the form which changes the layout of the form meaning that the checkbox position changed completely. Does this theory make sense?
Everyone: if you experience this problem, please file a new issue by filling in the “Bug Report” template and provide a repro. We’ll happily take a look!
The problem is still exists in playwright version 1.49.1 But can solve it with a new way: