How can I avoid NSInternalInconsistencyException when tapping on UI elements with XCTest? : <p>Since upgrading to xcode 9 some of my UI tests have started throwing an <code>NSInternalInconsistencyException</code> with the message </p>

<blockquote>
  <p>Activity cannot be used after its scope has completed</p>
</blockquote>

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

<p>Any help on fixing or working around this would be much appreciated!</p>

<p>The top of the stack trace for the exception is:</p>

<pre><code>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] + 
</code></pre>
