使用 Xcode 7.2 录制时 XCUITest 崩溃

Posted

技术标签:

【中文标题】使用 Xcode 7.2 录制时 XCUITest 崩溃【英文标题】:XCUITest crashes when recording with Xcode 7.2 【发布时间】:2016-01-18 14:19:05 【问题描述】:

我正在使用 Xcode 7.2 试验新的 ios UI 测试框架。

我遵循了一些教程并观看了 Apple 的演示视频并尝试记录一些测试用例。当目标是“模拟器”时,第一次测试成功。当我尝试在真实设备中重复这些步骤时,Xcode 在启动应用程序后立即崩溃。我使用了来自 Apple 的 iPhoneCoreDataRecipes 演示应用程序,以便开始使用一些简单且“经过验证”的东西。

下面是一个简单的测试类:

#import <XCTest/XCTest.h>

@interface RecipesUITests : XCTestCase

@end

@implementation RecipesUITests

- (void)setUp 
    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.
    self.continueAfterFailure = NO;
    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
    [[[XCUIApplication alloc] init] launch];

    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.


- (void)tearDown 
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];


- (void)testExample 
    // Use recording to get started writing UI tests.
    // Use XCTAssert and related functions to verify your tests produce the correct results.


    XCUIApplication *app = [[XCUIApplication alloc] init];
    [app.navigationBars[@"Recipes"].buttons[@"Add"] tap];
    [app.textFields[@"Recipe Name"] typeText:@"Lef"];
    [app.navigationBars[@"Add Recipe"].buttons[@"Save"] tap];

    XCUIElementQuery *tablesQuery = app.tables;
    XCUIElement *descriptionTextField = tablesQuery.textFields[@"Description"];
    [descriptionTextField tap];
    //[descriptionTextField tap];
    //[descriptionTextField tap];

    XCUIElement *lefNavigationBar = app.navigationBars[@"Lef"];
    [lefNavigationBar.buttons[@"Edit"] tap];

    XCUIElement *overviewTextField = tablesQuery.textFields[@"Overview"];
    [overviewTextField tap];
    //[overviewTextField typeText:@"Lef2"];
    [lefNavigationBar.buttons[@"Done"] tap];
    [lefNavigationBar.buttons[@"Recipes"] tap];



这是一个已知问题吗?关于如何调试问题的任何想法?我需要“以某种方式”来启用设备吗?

更新 #1

当我尝试在设备中运行测试用例时,我在 Xcode 中收到了一些日志。

2016-01-19 13:18:36.772 XCTRunner[9331:2287347] Running tests...
2016-01-19 13:18:36.776 XCTRunner[9331:2287347] Looking for test bundles in /var/mobile/Containers/Bundle/Application/42229178-A3D8-4BB1-A4CD-7E2FC28E90F9/RecipesUITests-Runner.app/PlugIns
2016-01-19 13:18:36.778 XCTRunner[9331:2287347] Found test bundle at /var/mobile/Containers/Bundle/Application/42229178-A3D8-4BB1-A4CD-7E2FC28E90F9/RecipesUITests-Runner.app/PlugIns/RecipesUITests.xctest
2016-01-19 13:18:36.780 XCTRunner[9331:2287347] Looking for configurations in /var/mobile/Containers/Bundle/Application/42229178-A3D8-4BB1-A4CD-7E2FC28E90F9/RecipesUITests-Runner.app/PlugIns/RecipesUITests.xctest
2016-01-19 13:18:36.784 XCTRunner[9331:2287347] Found configuration <XCTestConfiguration: 0x145e317d0>
                      testBundleURL:file:///Users/unifyit/Downloads/iPhoneCoreDataRecipes_3/Build/Products/Debug-iphoneos/RecipesUITests-Runner.app/PlugIns/RecipesUITests.xctest/
                  productModuleName:RecipesUITests
                        testsToSkip:(null)
                         testsToRun:RecipesUITests/testExample
                 reportResultsToIDE:YES
                  sessionIdentifier:<__NSConcreteUUID 0x145e235a0> DD878763-999D-467F-968D-599B82EC5651
         pathToXcodeReportingSocket:(null)
          disablePerformanceMetrics:no
    treatMissingBaselinesAsFailures:no
                    baselineFileURL:(null)
              targetApplicationPath:/Users/unifyit/Downloads/iPhoneCoreDataRecipes_3/Build/Products/Debug-iphoneos/Recipes.app
          targetApplicationBundleID:com.example.apple-samplecode.Recipes
                   reportActivities:YES
           testsMustRunOnMainThread:YES
***** Begin XCTest Status Log *****
***** End XCTest Status Log *****

看起来什么都没有发生,没有执行测试用例,Xcode 尝试加载应用程序但它显示黑屏。

此外,当我尝试重新编码时,Xcode 会导致以下崩溃:

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
ProductBuildVersion: 7C68
ASSERTION FAILURE in /Library/Caches/com.apple.xbs/Sources/IDEFrameworks/IDEFrameworks-9548/IDEKit/Testing/IDEUIRecordingManager.m:429
Details:  (result) should not be nil.
Object:   <IDEUIRecordingManager: 0x7fb40190c640>
Method:   -_workspaceTabController
Thread:   <NSThread: 0x7fb3fb519b00>number = 1, name = main
Hints:   None
Backtrace:
  0  0x0000000103e541fa -[IDEAssertionHandler handleFailureInMethod:object:fileName:lineNumber:assertionSignature:messageFormat:arguments:] (in IDEKit)
  1  0x00000001029e4761 _DVTAssertionHandler (in DVTFoundation)
  2  0x00000001029e4978 _DVTAssertionFailureHandler (in DVTFoundation)
  3  0x0000000103f3256b -[IDEUIRecordingManager _workspaceTabController] (in IDEKit)
  4  0x0000000103f33433 __94-[IDEUIRecordingManager _startRecordingWithLaunchSession:alwaysAskForAPIAccess:reservedNames:]_block_invoke_2 (in IDEKit)
  5  0x0000000102a20a04 __DVTDispatchAsync_block_invoke (in DVTFoundation)
  6  0x00007fff8c97d8f5 _dispatch_call_block_and_release (in libdispatch.dylib)
  7  0x00007fff8c9723c3 _dispatch_client_callout (in libdispatch.dylib)
  8  0x00007fff8c985c1b _dispatch_main_queue_callback_4CF (in libdispatch.dylib)
  9  0x00007fff986ac999 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ (in CoreFoundation)
 10  0x00007fff9866b9cd __CFRunLoopRun (in CoreFoundation)
 11  0x00007fff9866afc8 CFRunLoopRunSpecific (in CoreFoundation)
 12  0x00007fff8a5ead55 RunCurrentEventLoopInMode (in HIToolbox)
 13  0x00007fff8a5eab8f ReceiveNextEventCommon (in HIToolbox)
 14  0x00007fff8a5ea9cf _BlockUntilNextEventMatchingListInModeWithFilter (in HIToolbox)
 15  0x00007fff93319d96 _DPSNextEvent (in AppKit)
 16  0x00007fff933191c5 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] (in AppKit)
 17  0x0000000102fc00c2 -[DVTApplication nextEventMatchingMask:untilDate:inMode:dequeue:] (in DVTKit)
 18  0x00007fff9330dd28 -[NSApplication run] (in AppKit)
 19  0x00007fff932d6fbe NSApplicationMain (in AppKit)
 20  0x00007fff8fec85ad start (in libdyld.dylib)

abort() called

【问题讨论】:

【参考方案1】:

我在黑暗中的最佳拍摄:

IOS 8 增强

iOS 8 在设置 > 开发人员下包含一个新的启用 UI 自动化首选项,它允许第三方开发人员更好地控制他们的设备何时可以执行自动化。对于物理 iOS 设备,此设置默认关闭,必须在执行任何 UI 自动化之前启用。在模拟器中,该设置默认开启。

Source

这是在更明显的要求(ios9 目标、10.10/11 等)之上的。该文档还列出了许多其他要求和链接,您可能会发现对参考有帮助。

【讨论】:

很遗憾没有。我启用了该选项,但行为相同。 重启手机后,好像可以了。也许这可以补充您的答案。 @cateof 并不是说​​ Louis 的回答没有帮助,但我发现很多 Xcode 的 UI 测试错误可以通过重新启动设备或通过 Xcode > Devices 删除应用程序并重新安装来解决(对于例如,当你运行一个 UI 测试并且它给你“成功”,甚至没有运行任何东西,或者记录我也经历过的崩溃) @JamesGoe 我接受了这个答案,因为它是在黑暗中拍摄的,当然我在我的设备中关闭了这个选项。当然,这还不够。正如我在我的 cmets 中所写,设备重启是强制性的。 这对我有用 - iOS 7.2,打开启用 UI 自动化,然后重新启动 iOS 设备。我的 iOS 设备是 iPhone 6 PLUS。如果重新打开设备电源时 Xcode 中未正确识别设备,则断开 USB 电缆与设备的连接并重新应用 - 如果 Xcode 现在可以正确看到设备,那么 RECORD UI 测试功能现在应该可以正常工作而不会导致 Xcode 崩溃。 -rrh【参考方案2】:

确认启用 UI 自动化和电源循环设备确实允许 Apple 的 UI 测试在真实设备上运行。在 Xcode 8 + iOS 10.1 上测试。

【讨论】:

以上是关于使用 Xcode 7.2 录制时 XCUITest 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

XCode9:XCUITest 给出 Runner UIAccessibility 错误

Xcode XCUITest XCUICoordinate 不适用于按下和/或拖动

更新到 Xcode 10 后 XCUITest 的测试目标似乎不起作用

如何在 Jenkins 中将屏幕截图附加到 XCUITest 报告。只有通过 XCODE 运行才能看到截图

无法在 Swift 4 上的 XCUITest 中设置设备方向

如何从 XCUITest(来自 Xcode 11)中的 xcresult 文件中提取详细信息?