Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid NSInternalInconsistencyException when tapping on UI elements with XCTest?

Since upgrading to xcode 9 some of my UI tests have started throwing an NSInternalInconsistencyException with the message

Activity cannot be used after its scope has completed

The problem occurs when I call tap() on an XCUIElement. I can see that the element is a valid button, which is enabled.

Any help on fixing or working around this would be much appreciated!

The top of the stack trace for the exception is:

0   CoreFoundation                      0x00000001035ad1cb __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x0000000102f0ff41 objc_exception_throw + 48
2   CoreFoundation                      0x00000001035b2362 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x00000001029b4089 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4   XCTest                              0x00000001027d4018 -[XCActivityRecord _synchronized_ensureValid] + 153
5   XCTest                              0x00000001027d3b0b -[XCActivityRecord _synchronized_addAttachment:] + 39
6   XCTest                              0x00000001027d3c98 -[XCActivityRecord addAttachment:] + 37
7   XCTest                              0x00000001027c0935 -[XCActivityRecord(UITesting) attachAutomaticScreenshot] + 265
8   XCTest                              0x00000001027f84b3 __43-[XCUIElement resolveHandleUIInterruption:]_block_invoke + 1465
9   XCTest                              0x0000000102814788 -[XCTContext _runActivityNamed:type:block:] + 185
10  XCTest                              0x00000001027f7eed -[XCUIElement resolveHandleUIInterruption:] + 139
11  XCTest                              0x000000010282b5d1 __63-[XCUIElement(XCUIElementEventSynthesis) _dispatchEvent:block:]_block_invoke + 81
12  XCTest                              0x0000000102814788 -[XCTContext _runActivityNamed:type:block:] + 185
13  XCTest                              0x000000010282b500 -[XCUIElement(XCUIElementEventSynthesis) _dispatchEvent:block:] + 315
14  XCTest                              0x000000010282d242 -[XCUIElement(XCUIElementTouchEvents) tap] + 
like image 611
Paul Avatar asked Sep 20 '17 09:09

Paul


1 Answers

In Xcode 9, this can be caused due to a bug between XCUITest and the new Xcode 9 main thread checker. More details are in this blog post.

To temporarily work around this issue until Apple fixes it, disable the Main Thread checker in the Test section of your scheme:

enter image description here

like image 144
Aaron Brager Avatar answered Nov 01 '22 17:11

Aaron Brager