Heloo! I am working with uitests on iOS and am using typeText method to enter a string into a textField. The application is multilingual, so the test case involves entering a string in different languages. However, the method fails for strings other than the current keyboard language (cannot switch the keyboard language to enter this string, although the simulator has a keyboard with this language). I haven't been able to solve this problem for a week now. I did not find ways to switch the keyboard language for typeText, or otherwise solve the problem. Please, help!
UPD (for drunkencheetah):
I use this method as XCUIElement extension:
func clearAndTypeText(_ text: String) {
let typedText = self.value as? String
focusIfNeeded()
if typedText != nil {
let deleteText = String(repeating: XCUIKeyboardKey.delete.rawValue, count: typedText!.count)
typeText(deleteText)
}
typeText(text)
}
firstTextField.clearAndTypeText("English12345") // Result - "English12345"
secontTextField.clearAndTypeText("文本123") // Chinese as example. Result -> "123"
// This will take a very long time to print.
If I manage to manually switch the keyboard language (while running the test) from English to Chinese, the text will be printed. Otherwise, only numbers
CodePudding user response:
typeText() should function regardless of the current keyboard language. I've just tested typing text in Bulgarian(Cyrillic) and Chinese without issues.
- Since your application is multilingual you should make sure you are locating the element respective to the current application language(if not using an accessibility identifier).
- Also make sure the element has keyboard focus - use tap() on it before attempting typeText() just in case.
- Make sure if running on simulator that I/O > Keyboard > Connect hardware keyboard is disabled
- As Mike Collins suggests in the comments you could use the pasteboard (only on simulator!) like this:
UIPasteboard.general.string = "teststring"
textElement.doubleTap()
app.menuItems["Paste"].tap()
Note that this will not work on real devices.