[英]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 自从升级到xcode 9以来,我的一些UI测试开始抛出带有消息的
NSInternalInconsistencyException
Activity cannot be used after its scope has completed
范围完成后无法使用活动
The problem occurs when I call tap()
on an XCUIElement
. 当我在
XCUIElement
上调用tap()
时会出现问题。 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] +
In Xcode 9, this can be caused due to a bug between XCUITest
and the new Xcode 9 main thread checker. 在Xcode 9中,这可能是由于
XCUITest
和新的Xcode 9主线程检查程序之间的错误引起的。 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: 要在Apple修复此问题之前暂时解决此问题,请在您的方案的“测试”部分中禁用主线程检查器:
I ended up fixing this by retaining the element and just using the same reference to it for subsequent calls. 我最后通过保留元素并对后续调用使用相同的引用来解决这个问题。
XCUIElement *searchBar = self.app.searchFields[@"Search"];
[searchBar tap];
[searchBar typeText:@"Test Guy"];
I had same issue with my XCUItestcase. 我的XCUItestcase遇到了同样的问题。 Though I unselected "Main Thread checker" in schema settings but still my test case was failing.
虽然我在架构设置中取消选择“主线程检查器”,但我的测试用例仍然失败。 The strange thing that it was passing on iphone 6 plus but same code was failing for 5s, 6s iphone.
奇怪的是它传递了iphone 6 plus但相同的代码却失败了5s,6s iphone。 Here is the approach I took to resolve the issue:
以下是我解决问题的方法:
1. Make sure you 'unselected "Main Thread checker" in test target
scheme settings
2. Take the reference of the UI element where test is crashing.
3. Before performing action. Put some sleep or have some delay.
4. Then perform the action on that element.
sample : 样品:
let confirmBtn = app.buttons["go"]
sleep(2)
confirmBtn.tap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.