Skip to content

Instantly share code, notes, and snippets.

@pkuecuekyan
Created September 11, 2018 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkuecuekyan/4d59f7b29ea6f84a74b62ee88ca39a10 to your computer and use it in GitHub Desktop.
Save pkuecuekyan/4d59f7b29ea6f84a74b62ee88ca39a10 to your computer and use it in GitHub Desktop.
Clearing text and adding enter for an input UI field (UISearchBar e.g.), for UIViewController testing
extension XCUIElement {
func clearText() {
//
// cf. and tip courtesy of
// https://stackoverflow.com/questions/32821880/ui-test-deleting-text-in-text-field
//
guard let stringValue = self.value as? String else {
return
}
// workaround for apple bug
if let placeholderString = self.placeholderValue, placeholderString == stringValue {
return
}
var deleteString = String()
for _ in stringValue {
deleteString += XCUIKeyboardKey.delete.rawValue
}
self.typeText(deleteString)
}
func typeTextAndPressEnter(_ text: String) {
self.typeText("\(text)\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment