5

I have a test written in Java using Selenium Webdriver which visually clears a text field written in Angular 2 but Selenium doesn't actually clear the field in the DOM.

I need to get an alert from the text field that says that you can't create an empty title, and that alert doesn't appear because the DOM still says that there is a title even though Selenium supposedly cleared the field:

ng-reflect-length="10"
ng-reflect-model="New Survey"

If it actually did clear, the DOM should not include the above angular code.


I originally tried using the correct format of Java Selenium to clear:

public void clearTextField(){
 webElement.clear();
}

And this will visually clear the text field, but those Angular 2 attributes and values do not disappear in the DOM as they should. This causes the character counter in the text field to remain at 90 chars instead of what it should be at 100 chars. If it was 100 chars, I would get my alert.


The above Angular 2 attributes and values will reflect a sendKeys change if I:

public void sendKeysToTextField(String inputText){        
 webElement.sendKeys(inputText);
} 

But still, the DOM doesn't update when clearing the text field.


I have a hacky workaround, which forces a RobotUtility to backspace and it needs coordinates to locate the page. But this only works locally.

Please help! Thank you.

  • some information available here related to `clear` method, http://stackoverflow.com/a/15172140/2575259 – Naveen Kumar R B Jan 07 '17 at 09:20
  • Hi @Naveen I appreciate you comment. But this issue appears to be specific to how Selenium is handling Angular 2 in the DOM. .clear() works perfect with other languages. – Cyndi Russell Jan 11 '17 at 19:50

1 Answers1

0

Have you tried sending a Control-A to select all in the field and then a backspace to clear the field?

object.sendkeys("^a{BACKSPACE}");

Mike Kiewicz
  • 83
  • 1
  • 11
  • Hi MK thanks. I've tried that and a similar RobotUtility. Basically string.length, loops and "types" char for any keycode you want. It requires a separate Java thread to run which kicks Selenium off the browser so I have to force it to re-click specific coordinates (so only runs local). Have to type a char first for field to recognize backspace. Method: `RobotUtilities.type("char"); object.sendKeys(""); object.click(); RobotUtlities.moveMouse(x, y); RobotUtlities.moveLeftClick(); RobotUtlities.type("char"); RobotUtlities (loops backspace);` – Cyndi Russell Feb 06 '17 at 17:53