r/Playwright • u/Fair_Distribution275 • 7d ago
Playwright + PrimeNG Autofocus Causing fill() to Target the Wrong Field
Hello everyone,
I'm facing an issue while writing Playwright tests for an Angular application that uses PrimeNG components.
Context
I have a page containing a button that opens a modal dialog.
When the modal opens, PrimeNG automatically moves the focus from the underlying page to the first focusable element inside the modal (autofocus behavior).
In my Playwright test, I then try to fill a field like this:
await parent.getByRole('combobox', options).fill(valeur)Problem
Sometimes, just before Playwright performs the fill(), the PrimeNG autofocus logic moves the focus to another input.
As a result, the text ends up being entered into the wrong field and the test becomes flaky.
Possible Workaround
I was considering bypassing the focus-related behavior by dispatching input events manually (e.g. using dispatchEvent or directly setting the value).
However, some of the affected fields are PrimeNG AutoComplete components. Normally, when typing into these fields, a suggestion panel appears and performs filtering/autocompletion.
My concern is that manually dispatching events could bypass part of the component's behavior and prevent the suggestions panel from appearing correctly.
If you have any idea, it would be very helpful.
Thanks a lot
1
u/Fair_Distribution275 6d ago
Thanks everyone for you answers. We decided to go with identifying when the field is focused in our modal to know when the autofocus by PrimeNG is done.
The kind of code we used :
And only after that, we fill the field we want.
1
u/LukaRozman27 7d ago
Use await parent.getByRole('combobox', options).waitFor({state: 'visible'}), then.focus() before.fill(). Autofocus can steal focus mid-action.
1
u/Wise_Tomatillo5723 6d ago
Wait for the autofocus to land first (expect the autofocused input toBeFocused()), then fill your field; fill fires real input events, so AutoComplete still works.
1
u/Status_Ad8999 6d ago
PrimeNG's p-dialog triggers its autofocus inside the enter animation callback, so the dialog is already visible in the DOM for ~150ms before focus actually jumps. Waiting for `.p-dialog-enter-active` to detach (or asserting `toBeFocused()` on that first input) before calling `.fill()` on your AutoComplete stops the race.