How to test that staticTexts contains a string using XCTest

前端 未结 4 1831
走了就别回头了
走了就别回头了 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:33

    You can create an extensions to simply use it over XCUIElement.

    extension XCUIElement {
        
        func assertContains(text: String) {
            let predicate = NSPredicate(format: "label CONTAINS[c] %@", text)
            let elementQuery = staticTexts.containing(predicate)
            XCTAssertTrue(elementQuery.count > 0)
        }
    }
    

    Usage:

    // Find the label
    let yourLabel = app.staticTexts["AccessibilityIdentifierOfYourLabel"].firstMatch
    
    // assert that contains value
    yourLabel.assertContains(text: "a part of content of the staticText")
    
    

提交回复
热议问题