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.
If the textField has placeholder text you can use the placeholder text to find the textField.
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 useXCUIApplication().textFields.elementBoundByIndex(0)
to get the textField. (or whatever query you need to get that particular textField at that time)
- 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
How to Dispatch Functions in Swift the Right Way
Is There a Prefix Header (Or Something with This Functionality) in Swift
Sort Array by Calculated Distance in Swift
Making Simple Accordion Tableview in Swift
How to Clear the Terminal Screen in Swift
What Is the Swift Syntax " .Bar" Called
Arkit: Placing an Scntext at a Particular Point in Front of the Camera
Simpliest Solution to Check If File Exists on a Webserver. (Swift)
Variable 'Xxx' Was Never Mutated, Consider Changing to 'Let'
Swift - Could Not Cast Value of Type '_Nscfstring' to 'Nsdictionary'
Trouble Using Callbacks with Cgpattern in Swift3
Read-Only Properties of Protocols in Swift
How to Use a @Fetchrequest with the New Searchable Modifier in Swiftui
Replacing Calayer and Cabasicanimation with Skscene and Skactions
Where Is Info.Plist in Xcode 13? (Missing, Not Inside Project Navigator)
Interface Builder, @Iboutlet and Protocols for Delegate and Datasource in Swift