Can't po objects in Swift UITests (Objective-C++ project)

Because UI tests run as a separate process from the main app, I'm using UI tests as an opportunity to learn Swift.

The project is Objective C++ (there are a handful of .mm files), and the UI tests are a contained in a separate target, with nothing but a single .swift file containing tests.

My understanding is that no bridging header is necessary because the UI tests are not talking to the Objective-C code in the host application.

(and at any rate, adding such a file result in absolutely no change to the below-written behavior)


In Xcode 8.3.3, I found that the Swift 3.1 tests ran fine in all Simulators, but entering a breakpoint and running

po app.debugDescription

(app is a local var initialized as XCUIApplication.init()

worked properly in Simulator 10.x, but in Simulator 9.x returned the error

error: in auto-import:
failed to get module '<REDACTED>UITests' from AST context:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "UIKitOverlayShims.h"


With Xcode 9 (up to and including Beta 5), the above error happens in every Simulator regardless of how trivial the Swift UI test code.

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        let app = XCUIApplication.init()
        app.launch()
    }


Adding a line into the setUp() method (for example) works fine...

print(app.debugDescription)

The Variables View shows proper output. And in the console running

fr v app

at least doesn't complain and attempts to spit out some useful information.


But trying to po anything from the Swift REPL now shows the above errors.

This makes for a huge productivity block, as I cannot rely on an interactive debugging session to understand failures in XCUIElement discovery.

Replies

Ah, I may have mis-spoke about the nature of the problem.
It seems that Xcode 9 (Beta 5) & iOS Simulator 11.0 work properly

Xcode 8.3.3 & iOS Simulator 10.2 work properly (iOS 10.3 doesn't work)


So, something about OS version + Swift version, only specific combinations work properly in the debugger?

(macOS 10.12.6 (16G29), Version 8.3.3 (8E3004b)

Here's a minimal test to illustrate the problem:

  1. In Xcode, File > New > Project
    1. Template: iOS Single View Application
      1. Language: Objective C
      2. Devices: Universal
      3. Everything else, default
    2. Save it out to Desktop
  2. Add a Target "iOS UI Testing Bundle"

    Language: Swift

  3. In the .swift file, setup as follows
override func setUp() {
  super.setUp()
  continueAfterFailure = false
  let app = XCUIApplication.init()
  app.launch()
  //set a breakpoint here, after app.launch()
}


Build & Run in iOS Simulator 10.3 (should be the default)

When the breakpoint (mentioned above) is hit, run in the console

po app


You should get failure in the form

error: in auto-import:
failed to get module '103_test_appUITests' from AST context:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/CoreGraphics.h"
        ^
.
.
.
(goes on for a long while)


Next, Project > Build Settings > Deployment Target, set it to iOS 10.2 (default should be 10.3)

Build & Run in iOS Simulator 10.3, run po app. Same problem.


Build & Run in iOS Simulator 10.2, run po app, you should see success in the form

Attributes: Application 0x600000166c00: {{0.0, 0.0}, {414.0, 736.0}}, label: '103_test_app'
Element subtree:
→Application 0x600000166c00: {{0.0, 0.0}, {414.0, 736.0}}, label: '103_test_app'
    Window 0x600000166900: Main Window, {{0.0, 0.0}, {414.0, 736.0}}
      Other 0x600000166540: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
    Window 0x600000166600: {{0.0, 0.0}, {414.0, 736.0}}
      StatusBar 0x6000001645c0: {{0.0, 0.0}, {414.0, 20.0}}
        Other 0x600000164680: {{0.0, 0.0}, {414.0, 20.0}}
        Other 0x600000164c80: {{0.0, 0.0}, {414.0, 20.0}}
          Other 0x600000165e80: traits: 8388608, {{6.0, 0.0}, {39.0, 20.0}}
          Other 0x600000167a40: traits: 8388608, {{50.0, 0.0}, {13.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
          Other 0x600000169480: traits: 8389120, {{184.0, 0.0}, {50.0, 20.0}}, label: '2:29 PM'
          Other 0x600000165d00: traits: 8388608, {{376.0, 0.0}, {33.0, 20.0}}, label: '-100% battery power'
Path to element:
→Application 0x600000166c00: {{0.0, 0.0}, {414.0, 736.0}}, label: '103_test_app'
Query chain:
→Find: Target Application 0x6000000b5f60
  Output: {
    Application 0x600000166c00: {{0.0, 0.0}, {414.0, 736.0}}, label: '103_test_app'
  }

A work around for this is to use command line tool from an order Xcode.

Is there any other solution to fix this issue as to use the command line tool?


We have the same issue to use "po" at the debugger :


error: in auto-import:

failed to get module 'IdealoUITests' from AST context:

<module-includes>:1:9: note: in file included from <module-includes>:1:

#import "UIKitOverlayShims.h"

^

error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/shims/UIKitOverlayShims.h:24:11: error: use of undeclared identifier 'UIFocusSystem'

return [UIFocusSystem environment:environment containsEnvironment:otherEnvironment];

^

error: could not build Objective-C module '_SwiftUIKitOverlayShims'

Here's how I got this working. The problem looks to be in UIKitOverlayShims.h (in /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/shims). I removed the code that was throwing the error:

#if TARGET_OS_TV || TARGET_OS_IOS
static inline BOOL _swift_UIKit_UIFocusEnvironmentContainsEnvironment(id<UIFocusEnvironment> environment, id<UIFocusEnvironment> otherEnvironment) {
  return [UIFocusSystem environment:environment containsEnvironment:otherEnvironment];
}
#endif // TARGET_OS_TV || TARGET_OS_IOS


After which the problem appears to be resolved. I can po from the console for iOS 10.x as well as 11.


Hope that helps!

I've filed a radar about this issue: http://www.openradar.me/34601923

It's only working with XCode 9. I couldn't find the same file in XCode 8


Thanks! 🙂

This is occurring for me on XCode 9 when using 10.x simulators, but does not occur when using 11.0 simulators.



/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.3.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:465:56: note: insert '_Nonnull' if the array parameter should never be null

UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);

^

<module-includes>:1:9: note: in file included from <module-includes>:1:

#import "UIKitOverlayShims.h"

^

error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/shims/UIKitOverlayShims.h:24:11: error: use of undeclared identifier 'UIFocusSystem'

return [UIFocusSystem environment:environment containsEnvironment:otherEnvironment];

^

error: could not build Objective-C module '_SwiftUIKitOverlayShims'

Yes, I'm continuing to have this issue as well on 10.x and 9.x simulators in Xcode 9.2 release.

This works.. don't think that Focus is a thing in iOS, so just need to remove the '|| TARGET_OS_IOS' part