Ui Test Deleting Text in Text Field

UI Test deleting text in text field

I wrote an extension method to do this for me and it's pretty fast:

extension XCUIElement {
/**
Removes any current text in the field before typing in the new value
- Parameter text: the text to enter into the field
*/
func clearAndEnterText(text: String) {
guard let stringValue = self.value as? String else {
XCTFail("Tried to clear and enter text into a non string value")
return
}

self.tap()

let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: stringValue.count)

self.typeText(deleteString)
self.typeText(text)
}
}

This is then used pretty easily: app.textFields["Email"].clearAndEnterText("newemail@domain.example")

How can i clean the XCUIElement which type is textField?

I am using this helper function:

-(void)clearField:(XCUIElement *)textField {
[textField doubleTap];
[app.menuItems[@"Select All"]tap];
}

and calling it via:

[self clearField: app.textFields[@"Last Name"]];

How to test UITextField in Xcode UI Testing when the text field is empty?

There are a few ways to do this.

  1. If the textField has placeholder text you can use the placeholder text to find the textField.

  2. You can use the elementBoundByIndex method to get a textField at an index. i.e. If that is the only textField in the app at the time you can use

    XCUIApplication().textFields.elementBoundByIndex(0) 

to get the textField. (or whatever query you need to get that particular textField at that time)


  1. You can set an accessibility identifier in code or in the storyboard to find the textField.

It is nice to use option 2 before setting up accessibility identifiers, so long as your textField is always going to be findable with the same query. Also, option 1 will fail if multiple textFields have the same placeholder text at the same time, so option 2 is even more appealing.

Xcode UI test fails to edit multiple UITextFields

As @Thisura Dodangoda pointed out, this code runs correctly on iOS 14.0.1 and 14.3, and I confirmed that it runs correctly on the 15.0 beta simulators. It appears to be a problem only on iOS 14.5, and perhaps on other versions between 14.3 and 15.0.



Related Topics



Leave a reply



Submit