1

I have a test in Xcode (XCUITests) that uses typeText to enter a string into a searchField. For our test, we do 2 taps into the searchField first. Then, we do:

searchField.typeText(ourStringHere + "\n")

When this line runs, it types the first character 2 extra times. So, if we pass in "tree", it will type "tttree". This obviously causes our tests to fail.

On this screen in our app that we are testing, we have actions occur based on matching as you type. So, after the first character is typed, some results are shown. I believe this is causing a timeout issue. Then Xcode is trying to type the whole word again. I believe this also happens twice. On the third attempt, the screen has settled down with it's background actions and the word it then successfully typed.

Ifs there was some way to override typeText() to delete anything it had previously tried, that would be helpful. Also, adding some pauses in between characters as they are typed would help. We could then have the test wait for the background actions to show all of the results and then type the next letter.

Other than this, I don't know how to fix this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
toolmania1
  • 309
  • 7
  • 15

1 Answers1

2

Based on your preconditions I think you should paste text instead of typing (but it is a little unsafe if you run your tests in parallel)

let pasteMenuItem = app.menuItems.firstMatch
UIPasteboard.general.string = "Preparing Pasteboard"

searchField.tap()
searchField.tap()
_ = pasteMenuItem.waitForExistence(timeout: 5)
UIPasteboard.general.string = ourStringHere + "\n"
pasteMenuItem.tap()

Also, check out comments here typeText() is typing inconsistent characters for a slow typing.

Roman Zakharov
  • 2,185
  • 8
  • 19
  • It's too bad that there isn't some way we can control the typeText method ( delays in between typing, retry the whole word but also delete anything that may have been mistakenly typed, etc. ). I had found similar to the above answer when researching, but hadn't tried it yet. It's not always acceptable in the app to cause less actions to occur on a screen ( business needs etc. ). So, the typeText obviously is not always cutting it anymore when it's expected to do so. Anyways, this is a nice workaround to such problems. Thanks! – toolmania1 Mar 26 '20 at 18:47