Created
September 11, 2018 20:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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