簡體   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