2

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

  • 1
    I’m away from my computer so unable to dig into this or provide code, but if all else fails your should be able to use the pasteboard for this. If you feel this is a suitable solution I can provide what I’ve found to be the most reliable code for using it. In recent testing I found this method to be 40% slower than typing, but if it’s your only option ‍♂️ – Mike Collins Apr 11 '22 at 14:59
  • @MikeCollins I would be very grateful to you! Any working solution would be helpful – Holistic Developer Apr 11 '22 at 15:11

1 Answers1

2

typeText() should function regardless of the current keyboard language. I've just tested typing text in Bulgarian(Cyrillic) and Chinese without issues.

  1. 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).
  2. Also make sure the element has keyboard focus - use tap() on it before attempting typeText() just in case.
  3. Make sure if running on simulator that I/O > Keyboard > Connect hardware keyboard is disabled
  4. 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.

drunkencheetah
  • 354
  • 1
  • 8
  • Points 2 and 3 have already been completed, but typeText() still stacked and entered only numbers from a string containing foreign letters and numbers, with the English keyboard opened. Conversely, if the string contains English letters and numbers, and a foreign keyboard is open, only numbers will also be printed. I'm using accessebility ids to access a textField, but I'd like to learn more about point 1. What do you mean? – Holistic Developer Apr 11 '22 at 15:54
  • 2
    If you are using accessibility ID to find the element then point 1 doesn't matter. If you were identifying the element by a label or placeholder text it could've been different on different app languages. I've observed what you are describing only when my Mac language was changed while running the test but if my Mac is in English and the simulator keyboard is in English I could successfully type both in Cyrillic and in Chinese. Can you share a code snippet? – drunkencheetah Apr 11 '22 at 15:59
  • I updated the question with an example – Holistic Developer Apr 11 '22 at 16:24
  • 1
    Regarding the very long time to print, are you by any chance running on iOS 15.0 with Xcode 13? If sim is 15.0 can you increase the version? – drunkencheetah Apr 11 '22 at 16:37
  • I meant that typeText() works for a very long time exactly in the case when the keyboard language does not match the language of inputing string, and only numbers are printed. Time delays correspond to the position of the digits in the string. Example: "qwe1rty2uio45" -> ~delay~ 1 ~delay~ 2 ~delay~ 45 -> Final string: 1245 I am using Xcode 13.3 and simulator iOS 15.4 – Holistic Developer Apr 11 '22 at 16:44
  • 1
    Oh, wow, that is interesting, I will test it out tomorrow with that configuration, I might encounter the same problem. In the meantime you could handle switching the keyboard language via the test although it might not be the best solution: ``` app.keyboards.firstMatch.keys["Next keyboard"].tap() ``` and handle any system messages as they appear. The sim will need to have the keyboards set up in advance and you need to somehow validate when and if to switch the keyboard at that moment - by checking if specific key based on language is available :P – drunkencheetah Apr 11 '22 at 16:50
  • I'll try to use the UIPasteboard as a solution to the problem. In any case, thanks for the help! I'm guessing the problem is in the settings of Xcode / project / simulator / macOS. One way or another, there is little time left to research the problem, I will try through the UIPasteboard. I will mark your answer as correct, because I am sure that the first 3 points in your answer will solve 99% of the possible problems with typeText(). It will be helpful for other users. Unfortunately, I have a special case of 1%.... =\ – Holistic Developer Apr 11 '22 at 16:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243818/discussion-between-drunkencheetah-and-holistic-developer). – drunkencheetah Apr 12 '22 at 10:58