繁体   English   中英

使用机器人模式的 XCUITest 无法打印错误的行

[英]XCUITest using Robot pattern can't print the erroneous line

我正在尝试重构我的项目的 UI 测试以使用机器人模式。 但它似乎无法显示代码中的哪一行是出错的那一行。 这是屏幕截图:

机器人模式 vs 原始模式

正如你在这里看到的, testShowAppHealthAndBackWithoutRobot()可以用红线显示错误,而testShowAppHealthAndBack()没有。

这是机器人的代码:

class Robot {
    let app: XCUIApplication

    init(app: XCUIApplication) {
        self.app = app
    }

    func tap(_ element: XCUIElement, timeout: TimeInterval = 5) {
        guard assertExists(element, timeout: timeout), element.isHittable else {
            XCTFail("Element: \(element) is not hittable!")
            return
        }
        element.tap()
    }

    func assertExists(_ element: XCUIElement, timeout: TimeInterval = 5) -> Bool {
        guard element.waitForExistence(timeout: timeout) else {
            XCTFail("Element: \(element) does not exist!")
            return false
        }
        return true
    }

    func assertExists(_ elements: [XCUIElement], timeout: TimeInterval = 5) {
        for _ in 0 ... Int(timeout) {
            if elements.filter({ $0.exists == false }).isEmpty {
                return
            }
            Thread.sleep(forTimeInterval: 1)
        }
        XCTFail("Elements: \(elements) do not exist!")
    }
}

class MainPageRobot: Robot {
    lazy var mainTitleText = app.staticTexts["My App"]
    lazy var appHealthButton = app.buttons["App Health"]

    func isInMainPageViewController() -> Self {
        _ = assertExists(mainTitleText)
        return self
    }
    func tapAppHealthButton() -> Self {
        tap(appHealthButton)
        return self
    }
}

class AppHealthRobot: Robot {
    lazy var navigationTitle = app.navigationBars["App Health"].staticTexts["App Health"]
    lazy var backButton = app.staticTexts["Red this shit up"]

    func isInAppHealthViewController() -> Self {
        assertExists(navigationTitle, line: line)
        return self
    }

    func tapBackButton() -> Self {
        tap(backButton)
        return self
    }
}

所以我的问题是,如何使用机器人模式在测试函数中显示错误的线条? 谢谢。

使用XCTAssert ( XCTFail ) 调用添加实用程序函数时,您应该传递fileline参数。

这是这种行为的一个例子。

func verify(something: Bool, file: StaticString = #file, line: UInt = #line) {
     XCTAssertTrue(something, file: file, line: line)
}

func testVerify() {
    verify(false) // This line would be marked as failed
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM