How to test that staticTexts contains a string using XCTest

前端 未结 4 1767
走了就别回头了
走了就别回头了 2021-02-07 04:19

In Xcode UI testing, how do I test that staticTexts contains a string?

In the debugger, I can run something like this to print out all the content of staticTexts:

4条回答
  •  被撕碎了的回忆
    2021-02-07 04:37

    I had this problem while I was building my XCTest, I had a dynamic string inside of my block of text I should verify. I had built this two functions to solve my problem:

    func waitElement(element: Any, timeout: TimeInterval = 100.0) {
        let exists = NSPredicate(format: "exists == 1")
    
        expectation(for: exists, evaluatedWith: element, handler: nil)
        waitForExpectations(timeout: timeout, handler: nil)
    }
    
    func waitMessage(message: String) {
        let predicate = NSPredicate(format: "label CONTAINS[c] %@", message)
        let result = app.staticTexts.containing(predicate)
        let element = XCUIApplication().staticTexts[result.element.label]
        waitElement(element: element)
    }
    

    I know this post is old, but I hope this can help someone.

提交回复
热议问题