XC UITesting with Dynamic Labels

时间:2016-01-05 22:48:20

标签: xcode swift xctest

我试图断言标签的值(动态填充)包含一个子字符串作为UITest结果的一部分。

我的问题是XCTAssert似乎不允许子串或近似匹配(无论如何我都能找到)。有没有人对如何编写以下内容以找到“小时前”而非特定“xx小时前”的匹配有任何建议?

目前我只能使用精确匹配(如下所示)。

    //Set up an expectation
    let textToFind = app.staticTexts["13 hours ago"]

    let exists = NSPredicate(format: "exists == true")
    expectationForPredicate(exists, evaluatedWithObject: textToFind, handler: nil)

    //Give the app time to get network data & Update UI
    waitForExpectationsWithTimeout(5, handler: nil)

    //Assert that we get results
    XCTAssert(textToFind.exists)

1 个答案:

答案 0 :(得分:2)

  1. 通过名称以外的方法更改查找UI元素的查询。 Xcode中的UI测试记录功能可以帮助您完成此操作 - 请参阅WWDC2015 session introducing UI testing以获取此操作的示例。

  2. 一旦找到了你的UI元素而没有使用它的名字,你的期望测试就可以使用一个测试子字符串名称的谓词。

  3. 或者:

    1. 明确使用查询查找匹配谓词的app子元素(请参阅elementMatchingPredicate中的XCUIElementQuery)而不是下标,然后您可以使用{{1}测试验证它在那里。再次,请参阅WWDC session示例。