简体   繁体   English

使用XCTest点击UI元素时,如何避免NSInternalInconsistencyException?

[英]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.

schema settings 架构设置 tes架构设置

sample : 样品:

   let confirmBtn = app.buttons["go"]
   sleep(2)
   confirmBtn.tap()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在XCTest中设置区域格式? - How can I set region format in XCTest? 如何在我的应用程序在XCTest下运行时设置可以看到的标志? - How can I set flags that my app will see when it's running under XCTest? 如何通过点击图像以编程方式进行搜索? - How can I programmatically segue with tapping on an Image? 如何使用单个标记多次点击 - how can I multiple tapping with single marker 当我只有.app和.xctest文件夹时,如何从命令行运行KIF(xctest)? - How to run KIF (xctest) from command line when I have just the .app and .xctest folder? 我如何解决NSInternalInconsistencyException',原因:'+ entityForName:失败报告 - How can I solve NSInternalInconsistencyException', reason: '+entityForName: fail report 如何在Xcode 8中将UI元素居中? - How can I center UI elements in Xcode 8? 在另一个选项卡中点击另一个按钮时,如何更改按钮的图像 - How can I change the image of a button when tapping on another button in another tab 如何在我的项目中运行XCTest发生运行时错误 - How can I run XCTest in my project there was a runtime error occur 点击按钮时如何获得添加的行文本字段文本? - How can I get added row textfield text when tapping button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM