XCUITest using Robot pattern can't print the erroneous line

落花浮王杯 提交于 2021-02-11 16:58:46

问题


I'm trying to refactor the UI test of my project to use Robot pattern. But it seems like it can't show what line in the code is the one with the error. Here's the screenshot:

As you can see here, the testShowAppHealthAndBackWithoutRobot() can show the error in red line while the testShowAppHealthAndBack() doesn't.

Here are the robots's code:

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
    }
}

So my question is, how do I show the erroneous lines in the test functions using Robot pattern? Thanks.


回答1:


When adding utility functions with XCTAssert (XCTFail) calls you should pass file and line arguments.

This is an example of this behaviour.

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
}



来源:https://stackoverflow.com/questions/60841886/xcuitest-using-robot-pattern-cant-print-the-erroneous-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!