Disable Connect Hardware Keyboard setting for Simulator when executing Xcode 10 UI Tests in parallel
Asked Answered
D

1

12

When I run my UI Tests with parallel execution enabled, multiple clones of an existing simulator (for example - iPad Air 2) are created to run different test suites.

In a Run Script from this StackOverflow answer, I am injecting a plist key "ConnectHardwareKeyboard" with the value false to disable the hardware keyboard connection which is enabled by default when launching a simulator: Hardware -> Keyboard -> Connect Hardware Keyboard

This script will set the value correctly for the existing simulators (iPad Air 2), but when the tests are executing in parallel, multiple clones of iPad Air 2 are created and the plists for the clones is not a clone from the original device. No flag is set in the cloned devices plists at all, but Hardware -> Keyboard -> Connect Hardware Keyboard is shown as selected.

This is causing my UI Tests to fail, because the keyboard is missing when I'm trying to dismiss the keyboard after typing test into a text field.

I have looked through many posts on StackOverflow and Apple Developer forums to try and resolve this issue but have not found anything. This must be a problem for other people... Has anyone out there had this same problem and/or found a solution?

Daily answered 9/11, 2018 at 16:0 Comment(3)
Ever found a solution to this?Epergne
Any news on this issue? Stil happening with Xcode 11.4Halberd
Same issue. Seems strange there isn't a clear way to write tests in such a way that they are totally agnostic about whether a hardware keyboard is attached.Braud
M
0

Like you, I have scoured the internet to find an elegant solution to this problem and haven't found one.

So in the meantime, until someone much smarter than me figures this out, I've implemented the following throughout my tests:

import Foundation
import XCTest

extension XCUIApplication {

    //Hardware keyboard makes tests fail.  Reporting from these failures is ambiguous.  This function is intended to fix that issue (for now, until we figure out something better)
    public func keyboardReadyCheck() {
        if !self.keys["S"].waitForExistence(timeout: 3) {
            XCTFail("The software keyboard could not be found.  Use Xcode Simulator settings to turn off hardware keyboard (Keyboard shortcut COMMAND + SHIFT + K while simulator has focus)")
        }
    }
}

This allows me to sprinkle XCUIApplication.keyboardReadyCheck throughout my tests, which will cause an immediate failure if the hardware keyboard is turned on.

It's not a great solution, but it's a lot faster than watching the entire suite run and time out on stupid things it can't find, and reporting back with "target not hittable" or "element not found", and then I've got to go figure out what happened.

Maritsa answered 25/6, 2019 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.