How to clear a TextField in Firefox

Hello friends,

I know I’m not the first one asking this. But I think I have a further problem on this topic.
I read that other members had the same issue that I have right know (with sendKeys backspace).

When I try to clear a TextField under Firefox (94.0.2) using the sendKeys method with backspace:

WebUI.sendKeys(testObject, Keys.chord(Keys.CONTROL, 'a'))
WebUI.sendKeys(testObject, Keys.chord(Keys.BACK_SPACE))

instead of totally removing the content it puts a weird symbol into the field:
backspace

So I tried using the clearText() method (from WebUiBuiltInKeywords), but it is not working as expected.
In my TestCase I have to validate, that after clearing a required field, the user gets a message, which says that it is required and can not be empty.
If I clear it by hand, everything works as expected, but if I execute the clearText()-method on the textfield it clears it but the expected message doesn’t show up and the TestCase fails.

This is the point where I couldn’t find any other experience about.
Is it a bug and should I open a Bug-Report? Or is it a problem with our (angular) front-end?

Btw. I also tried the java.awt.Robot class. But I don’t like it :smiley:

static void clearText(TestObject testObject) {
	WebUI.click(testObject)
	WebUI.sendKeys(testObject,
		Keys.chord(Keys.CONTROL, 'a'))
	// System.setProperty("java.awt.headless", "false");
	Robot robot = new Robot()
	robot.keyPress(KeyEvent.VK_BACK_SPACE)
	robot.keyRelease(KeyEvent.VK_BACK_SPACE)
}

When I run it on my Windows System and focus another Window while this test is running, it will execute the BACK_SPACE command on any other kind of text, and my TestCase fails again.
But yeah if I handle it carefully it works :stuck_out_tongue:

Summary:

  1. sendKeys - BACK_SPACE → not working on firefox
  2. WebUI.clearText() → not working for me
  3. java.awt.Robot() → not a really safe solution

Any suggestions how I can solve this for now?
If not I will continue on using Robot, but hope there is a brain out there who can help me haha.

Thanks and have a nice day,
Simon

I’ve heard of (read) cases like this, but never experienced it myself. I think (but can’t prove) that robot might need affordances from you that it can have sole use of the mouse and keyboard when executing compiled actions.

That said, if you have a css selector for the input text control, you can use JavaScript to clear it quite easily:

String js = 'document.querySelector("your-selector").value = "";'
WebUI.executeJavaScript(js, null)

If I were you, I would rewrite clearText as clearInputText and use the code above – that will give you back your freedom to do other things while the test case is running.

1 Like

Hi thanks for your answer.
I just tried this, but I get the same result like using WebUI.clearText()

Still not working.

Well, I didn’t tell you to use WebUI.clearText().

Take a screenshot of the element in Firefox (not Chrome) devtools. If you see a gray event button next to the element, please expand it and include it in the screenshot. Like this:

I didn’t use WebUI.clearText().
I took your code as you told me and it replaced the content of the field with an empty string “” as expected.

static void clearInputText(String cssSelector) {
    String js = 'document.querySelector("' + cssSelector +'").value = "";'
    WebUI.executeJavaScript(js, null)
}  

But same behavior like as using WebUI.clearText().

Just an FYI, this has historically not worked as expected. Try doing it with webdriver instead:

WebElement element = WebUiCommonHelper.findWebElement(testObject, 30);
element.sendKeys(Keys.chord(Keys.CONTROL, 'a'));
element.sendKeys(Keys.DELETE); // or Keys.BACKSPACE

Also, double check that you are targeting the correct element with your test object (i.e. the <input> element in question).

2 Likes

Awesome!
This worked fine :slight_smile:
Thanks so much!

Simon

1 Like