2

Is there a way in UIAutomation to tap on the "Clear text" button of a textfield? Can you add an accessibility identifier to it, or does it already have one?

textfield with clear text button

At this moment I can only tap on the delete button of the keyboard or replace the textfield with an empty string. But I'd like to be able to tap on the clear text button that's being shown in my textfield.

Already found something in a comment of this issue:

textField.buttons["Clear text"].tap()

But this doesn't seem to work for me: "No matches found for this button"

Community
  • 1
  • 1
levaced
  • 21
  • 3
  • Have you checked this? https://jojitsoriano.wordpress.com/2011/06/27/ios-ui-automation-tapping-the-clear-button-in-uitextfield/ – Rafał Sroka Nov 06 '15 at 09:36
  • I did, but this article is using UIAElement object, while I'm using the XCUIElement. Think the latter is the more recent version for UI testing. – levaced Nov 09 '15 at 08:55

2 Answers2

3

You need to tap the text field first, so the button is visible:

let textField = app.textFields["Fanta"] // or however you're matching it
textField.tap()
textField.buttons["Clear text"].tap()

This works for me as a minimal example.

David Lord
  • 628
  • 10
  • 29
0

Try this one, it help you:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   textField.text = @"";
}
Parth Patel
  • 1,250
  • 1
  • 13
  • 21
  • thanks, for your answer, but I don't want to change the behaviour of my application. I'm performing UI tests, so I want to mimic the exact behaviour of my customers. – levaced Nov 06 '15 at 09:36
  • Can you tapWithOptions({offset:{x:textfield.width - 25, y:textfield.height/2}})? – Braains Nov 19 '15 at 16:33