I have XCUITest automation whose main purpose, beside testing, is to make clean, localized screenshots for use in the App Store. The storyboard view controllers contain UITextViews that do not dismiss on typing/tapping the return key. For the sake of argument, assume I am targeting iPad only--I have a solution for iPhone, because that needed another way to dismiss the keyboard anyway.
I am having difficulty finding an XCUITest keyboard-dismissing solution that works across all devices, independently of Simulator settings and the test's locale settings.
Nominally, the following appears to work for English
if XCUIApplication().keyboards.buttons["Hide keyboard"].exists {
XCUIApplication().keyboards.buttons["Hide keyboard"].tap()
}
if XCUIApplication().keyboards.buttons["Dismiss"].exists {
XCUIApplication().keyboards.buttons["Dismiss"].tap()
}
However, if I specify another language, such as French, and region, such as France, in the Scheme, for either or both of the Run and Test targets, it fails, because (presumably) the keyboard accessibility labels are localized. Except for the "exists" tests above, the taps are essentially the same as what might recorded if I use the record feature in Xcode. There are two alternative button taps, depending in part on the toggle software keyboard setting.
Is there a best practice for dismissing the keyboard, that is more straightforward and locale-agnostic?