Search code examples
javascriptiosios-ui-automation

How to clear a text field using UIAutomation


I'm trying to make sure a text field is clear before I type text into it. There's some state saving issues where this text field keeps getting populated. If there's an easy way to just clear it that would be great. Note that there is no clear button in this text field (the little x). The only option I see is to use the keyboard and press the back button over and over until this text field is cleared. That also means I'll need to be able to extract the text from the text field to know when it is empty. Anyways, does anyone know what the name of the key is on the keyboard for the back button? I've tried "back" and "delete" and both did not work.

function typeNewUserNameAndGoNext()
{
    target.frontMostApp().mainWindow().scrollViews()[0].scrollViews()[0].textFields()[0].tap();
    target.frontMostApp().keyboard().buttons()["delete"].tap(); //what goes in place of delete?
    target.frontMostApp().keyboard().typeString("AutomatedTestUser");
    target.frontMostApp().mainWindow().scrollViews()[0].scrollViews()[0].buttons()[2].tap();    
}

Obviously I would wrap that back button press into a loop until the text field is cleared.


Solution

  • Nevermind I got it by using the recorder.

    target.frontMostApp().keyboard().keys()["Delete"].touchAndHold(3.6);
    

    where you hold for as long as you need until it erases everything.