Swift 如何等待标签出现且字符数大于零UI测试

Swift 如何等待标签出现且字符数大于零UI测试,swift,xcode-ui-testing,ui-testing,Swift,Xcode Ui Testing,Ui Testing,实际上,在api调用之前,只有标签是空文本可见的。获取响应后,标签字符计数大于零。但在这里,如何等待标签出现,字符数大于零,并执行一些操作 这是我使用的代码,但我得到了谓词错误 捕获到“NSUnknownKeyException”,“[valueForUndefinedKey:]:这不是对关键字符的键值编码投诉 func testForWishListCountIsReflecting() { let app = XCUIApplication() app.decide()

实际上,在api调用之前,只有标签是空文本可见的。获取响应后,标签字符计数大于零。但在这里,如何等待标签出现,字符数大于零,并执行一些操作

这是我使用的代码,但我得到了谓词错误

捕获到“NSUnknownKeyException”,“[valueForUndefinedKey:]:这不是对关键字符的键值编码投诉

func testForWishListCountIsReflecting() {

    let app = XCUIApplication()
    app.decide()
    cellTap()
    app.navigationBars["MMRecoPageView"].buttons["MenuButton"].tap()

    let wishListLable =  app.staticTexts["WishListLabel"]
    wishListLable.label.characters

    let exists = NSPredicate(format: "label.characters.count > 0")

    expectationForPredicate(exists, evaluatedWithObject:wishListLable, handler: nil)

    waitForExpectationsWithTimeout(30, handler: nil)


    let expectedValue:String = app.staticTexts["WishListLabel"].value as! String
    app.buttons["Wishlist"].tap()

   XCTAssertTrue(app.tables.cells.count == UInt(expectedValue))

}

您的问题出现在错误消息中。
characters()
选择器不能在
NSPredicate
中使用,因为它不适用于KVO

对于键
字符

我建议一种稍微不同的方法。你知道实际文本应该是什么吗?如果是这样,你可以更改你的谓词以完全匹配它。你可以手动删除并设置“WishListLabel”可访问性,让内容自动设置

func testForWishListCountIsReflecting() {
    // ...setup...         
    let wishListLabel = app.staticTexts["contents of label"]
    waitForElementToAppear(wishListLabel)
}

private func waitForElementToAppear(element: XCUIElement, file: String = __FILE__, line: UInt = __LINE__) {
    let existsPredicate = NSPredicate(format: "exists == true")
    expectationForPredicate(existsPredicate, evaluatedWithObject: element, handler: nil)

    waitForExpectationsWithTimeout(5) { (error) -> Void in
        if (error != nil) {
            let message = "Failed to find \(element) after 5 seconds."
            self.recordFailureWithDescription(message, inFile: file, atLine: line, expected: true)
        }
    }
}

文本将类似于1或2。某些唯一的字符串格式的数字。在您告诉的代码中,让内容自动显示,但在收到响应之前,这里的标签最初是空的。我是否应该编写一些类似于app.staticText[“”]。如果我在5秒后写入此内容,则即使响应中出现了实际值,也会出现相同的空文本。查询将每隔一段时间重新计算一次,直到找到标签或超时。因此,您不应按要求使用空查询。我在上面的示例中设置了5秒超时。如果您不知道确切的标签内容,则测试将变得更难进行如果你知道内容的开头,你可以用a。