33

After building my app in Xcode 11 and running my suite of XCUITests I am getting many random failures with the following.

Failed to get matching snapshots: Error getting main window kAXErrorServerNotFound

No matter how long I increase timeouts the issues pop up intermittently. It seems to be having issues Snapshotting the UI hierarchy. Our tests pass consistently in Xcode 10.

I have reinstalled Xcode. Deleted all simulators. Cleared derived data. Modified timeouts. Upgraded from Xcode 11.1 to Xcode 11.2.1.

Thanks!

 

Luke Street
  • 441
  • 1
  • 4
  • 8

7 Answers7

34

I had the problem with matching while I was running the simple UITest in Xcode 11.3. To make it work I had to paste at first: XCUIApplication().activate() or XCUIApplication().launch().

shim
  • 9,289
  • 12
  • 69
  • 108
7

I have experienced the same issue with Xcode 11 and realized that the test runner was not getting killed when stopping the tests (or if the test crashed for some reason). Running the tests a second time would spawn a new test runner and at that point I had two runners trying to interact with the same application, leading to this very strange error.

To prove that I did the following:

  1. Created a UI test that types a long text in a text view
  2. Ran the test, and manually stopped it when there were a few sentences in the text view
  3. Manually opened the app in the simulator (not by running the test)
  4. Observed that random characters were appearing in the text view, even though no tests were running.

The workaround was to quit and reopen the simulator to make sure all processes were getting killed. Hope this solves your issues

PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
erudel
  • 482
  • 4
  • 11
  • Thanks for the insight! Now that you say this that does line up with what I have observed. Unfortunately, like I said in my original post, I have already tried deleting all simulators. These tests are run as a part of our builds both locally and on our CI system. Do you know of anyway to automate this process? – Luke Street Dec 01 '19 at 05:51
  • I What we ended up doing on our CI system was explicitly killing the Simulator before running a test suite. You can for example run `killall "Simulator" 2> /dev/null; xcrun simctl erase all`, or if you use fastlane there are options in scan to reset the simulator. – erudel Dec 01 '19 at 08:14
  • You helped me man. I just saw 3rd point only and it's working. Thank you bro. – Mayank Bhaisora Nov 05 '20 at 11:10
  • In my case, I need delete the app and reinstall it between every two test cases (I do this in the setUp()). Sometimes, it will meet this error for some test cases. How can I delete the simulator between two test cases? – csujiabin Feb 25 '21 at 14:07
3

I meet the issue from time to time in Xcode 11.1. I observed that the issue happen when waiting for UI elements especially there are web view being shown during the test. When the issue happened I was using XCUIElement.waitForExistence(timeout:) or expectation with NSPredicate(format: "exists == true"). When I changed to use expectation with NSPredicate(format: "hittable == true") the issue seems to be gone but I don't know why. The difference between the 2 attributes is that hittable only detect onscreen elements while exists detect off-screen elements such as off-screen cells of a table view.

expectation(for: NSPredicate(format: "hittable == true"), evaluateWith: element, handler: nil)
waitForExpectations(timeout: 60, handler: nil)
Juny
  • 51
  • 4
0

I had a similar issue on Xcode 11.

It turned out that before it was allowed to have the same accessibility identifier in many page.

But now using the new modal presentation style, you should use different identifiers within your pages to avoid conflicts.

shim
  • 9,289
  • 12
  • 69
  • 108
touti
  • 1,164
  • 6
  • 18
0

I use different machines. My older Macs experience this error far more often. My guess is older macs do not have the memory required to run certain XCUITests correctly.

Charlie S
  • 4,366
  • 6
  • 59
  • 97
0

If you are using fastlane it can be solve easily with the following lane:

# Unit tests

  lane :tests do
    clear_derived_data
    scan(
      workspace: "AppTest.xcworkspace",
      devices: ["iPhone 8"], # it can be with the iPhone that has your VM
      force_quit_simulator: true,
      reset_simulator: true,
      reinstall_app: true,
      scheme: "AppTest"
    )
  end

In theory, you must reset your simulator and reinstall your app.

Happy coding

Alfredo Luco G
  • 876
  • 7
  • 18
0

For me, calling activate() twice was the solution. It's not the first time I have had issues with that. I had other tests that needed to call the app back to the foreground and calling activate once wasn't enough.

XCUIApplication().activate()
XCUIApplication().activate()

It sounds weird, I know, but give it a try.

RawKnee
  • 324
  • 3
  • 11