playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Add locator.clear() for input fields

Open azad-derakhshani-GS opened this issue 2 years ago • 7 comments

I would like to request a locator.clear() functionality (similar to the one found in other frameworks) to empty the content of input fields (shouldn't be limited to input and textarea elements though since elements don't always follow these conventions).

Unfortunately, the workaround of using locator.fill('') doesn't work reliably, it doesn't change the content of some input fields at all.

azad-derakhshani-GS avatar May 09 '22 17:05 azad-derakhshani-GS

Unfortunately, the workaround of using locator.fill('') doesn't work reliably, it doesn't change the content of some input fields at all.

Can you submit a bug report for it instead with a repro which we can run locally?

shouldn't be limited to input and textarea elements though since elements don't always follow these conventions

how? Inserting text only works in my opinion only with input, textarea or [contenteditable] elements.

mxschmitt avatar May 10 '22 10:05 mxschmitt

Can you submit a bug report for it instead with a repro which we can run locally?

You mean add our testsuite so you can run the tests locally against our product? I don't think I'm allowed to post that publicly tbh, even more so because it touches bank account fields.

how? Inserting text only works in my opinion only with input, textarea or [contenteditable] elements.

I suggested that because I ran into the same limitation with checkboxes and selects. According to the documentation, those locator functions only work with input and select respectively, as well as label elements. And in an ideal world where everything adheres to standards, that would be suffice. But unfortunately, many checkboxes and selects and even buttons are actually a div or other elements, which means those built-in locator functions can't be used with them (so I wrote my own wrapper functions). I just wanted to avoid that same limitation for this feature suggestion.

azad-derakhshani-GS avatar May 11 '22 13:05 azad-derakhshani-GS

You mean add our testsuite so you can run the tests locally against our product?

No, I mean creating a minimal reproducible so it does not affect any NDAs.

But unfortunately, many checkboxes and selects and even buttons are actually a div or other elements, which means those built-in locator functions can't be used with them (so I wrote my own wrapper functions).

Yeah you need to target the exact <input/> element, then it works. This is unfortunately not something which Playwright can fix or workaround, since any UI component is different.


From what I understand you want to clear checkboxes, but for that we have locator.uncheck() and for normal text inputs we recommend fill('') which deletes internally.

mxschmitt avatar May 11 '22 13:05 mxschmitt

Ah ok! Thanks, I'll look into that and try to provide a minimal reproducible.

But independent of my specific issue, wouldn't it be better to have a dedicated locator.clear() function in the long term?

Yeah you need to target the exact <input/> element

That's the problem, there is no <input/> element. :D

azad-derakhshani-GS avatar May 11 '22 14:05 azad-derakhshani-GS

Actually, I solved my issue with that input field with the good old workaround of hitting backspace in a while loop until the field is empty. The issue is most likely specific to that input field and not PW.

However, this feature request wasn't so much about my specific issue but rather about improving PW in general by adding a locator.clear() function so people don't need to do locator.fill('') which is less elegant. :)

azad-derakhshani-GS avatar May 11 '22 16:05 azad-derakhshani-GS

However, this feature request wasn't so much about my specific issue but rather about improving PW in general by adding a locator.clear() function so people don't need to do locator.fill('') which is less elegant. :)

I agree there, fill('') is not very idiomatic!

mxschmitt avatar May 11 '22 16:05 mxschmitt

+1

hex0cter avatar Sep 21 '22 15:09 hex0cter

Im up for this one as well

await page.fill('[data-testid="input-name"] input', 'Slim Shady'); await page.fill('[data-testid="input-name"] input', ''); await page.fill('[data-testid="input-name"] input', 'Dr Dre');

This doesnt sit right.

akwasin avatar Oct 07 '22 12:10 akwasin

Hi @dgozman and everyone,

The clear feature does not work on a numeric input. I am using 1.36.1 playwright version. Only this code worked

let keyboardKey = os.platform().toString() === 'darwin'?"Meta+A": "Control+A"; await page.keyboard.press(keyboardKey); await page.keyboard.press("Backspace");

Not sure if i should create a new issue for this.

Thanks

ciradu2204 avatar Jul 27 '23 15:07 ciradu2204

Please file a new issue for it, thanks!

mxschmitt avatar Jul 27 '23 16:07 mxschmitt