1

我正在围绕 XCUITest 编写一个框架,它添加了各种方便的功能(例如本地 HTML 报告、Testrail 集成等),我发现为了我们的目的,拥有一个扩展 XCTestCase 并运行我们所有测试的类是有意义的从此(而不是让每个测试用例类都从 XCTestCase 扩展并通过非常尴尬的方案启动它)。然后,我们的实际测试类(称为 Features)从一个 XCUITest 方法调用开始按顺序启动。

当我想在测试用例之间删除并重新安装测试的应用程序时,这一切都很好。

我正在使用来自Is there a way to reset the app between tests between Swift XCTest UI in Xcode 7 中的类?通过 XCUITest 控制跳板。

该应用程序在每个测试用例之前重新安装,它在第一个测试之前成功,但在第二个测试用例之前我总是得到一个错误并且测试运行器退出:

t =    47.84s         Find the Application "com.apple.springboard" 0x6080000ac2a0 (retry 2)
t =    47.84s             Snapshot accessibility hierarchy for com.apple.springboard
t =    47.90s         Assertion Failure: Springboard.swift:46: (null)

再次调用 resolve() 时会发生错误:

// Resolve the query for the springboard rather than launching it
springboard.resolve()

我删除并重新安装应用程序的相关框架方法:

func reinstallApp()
{
    Springboard.deleteApp()
    app.launchArguments.append("--uitesting")
    app.launch()
}

有谁知道可以防止此错误的解决方法?

Xcode 9 更新:

我希望用 Xcode 9 和 Swift 4 以及新XCUIApplication.activate()方法来解决这个问题。我的 Springboard 代理类如下所示:

class Springboard
{
    static let springboard:XCUIApplication? = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    static let settings:XCUIApplication? = XCUIApplication(bundleIdentifier: "com.apple.Preferences")

    class func deleteApp()
    {
        XCUIApplication().terminate()

        if let springboard = springboard
        {
            springboard.activate()

            /* Force delete the app from the springboard. */
            let icon = springboard.icons["appname"]
            if icon.isHittable
            {
                let iconFrame = icon.frame
                let springboardFrame = springboard.frame
                icon.press(forDuration: 1.3)

                /* Tap the little "X" button at approximately where it is. The X is not exposed directly. */
                springboard.coordinate(withNormalizedOffset: CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX, dy: (iconFrame.minY + 3) / springboardFrame.maxY)).tap()
                springboard.alerts.buttons["Delete"].tap()

                /* Press home once to make the icons stop wiggling. */
                XCUIDevice.shared.press(.home)
                /* Press home again to go to the first page of the springboard. */
                XCUIDevice.shared.press(.home)
                /* Wait some time for the animation to end. */
                Thread.sleep(forTimeInterval: 0.5)

                if let settings = settings
                {
                    let settingsIcon = springboard.icons["Settings"]
                    if settingsIcon.isHittable
                    {
                        settingsIcon.tap()
                        settings.tables.staticTexts["General"].tap()
                        settings.tables.staticTexts["Reset"].tap()
                        settings.tables.staticTexts["Reset Location & Privacy"].tap()
                        settings.buttons["Reset Warnings"].tap()
                        settings.terminate()
                    }
                    else
                    {
                        XCUIDevice.shared.press(.home)
                        let settingsIcon = springboard.icons["Settings"]
                        if settingsIcon.isHittable
                        {
                            settingsIcon.tap()
                            settings.tables.staticTexts["General"].tap()
                            settings.tables.staticTexts["Reset"].tap()
                            settings.tables.staticTexts["Reset Location & Privacy"].tap()
                            settings.buttons["Reset Warnings"].tap()
                            settings.terminate()
                        }
                    }
                }
                else
                {
                    print("iOS Settings app not found!")
                }
            }
            else
            {
                print("App icon not found!")
            }
        }
        else
        {
            print("Springboard not found!")
        }
    }
}

但现在我收到以下错误:

Non-critical error encountered: Automatic screenshot failure.
Linked XCTest.framework from /Users/user/Library/Developer/CoreSimulator/Devices/B9AC848C-37C8-46D6-8322-ED78E7201CFE/data/Containers/Bundle/Application/5AB3F814-9DF8-4F1C-BC46-478C42951E60/UITests-Runner.app/Frameworks/XCTest.framework/XCTest, built with Xcode 0900(9A221a), modified on Thursday, September 28, 2017 at 3:10:06 PM Japan Standard Time
XCTest.framework bundle version: 13201
Executing on OS: Version 11.0 (Build 15A372)
Screenshot request returned nil with no additional error information.
Please file a bug and attach the details above.

有谁知道如何解决这个新问题?

4

0 回答 0