I'm trying to assert that the value of a label (which is populated dynamically) contains a substring as part of the result of a UITest.
My issue is that XCTAssert doesn't seem to allow for substrings or approximate matches (from what I can find anyway). Does anyone have any advice on how I might write the following to find a match for "hours ago" instead of the specific "xx hours ago"?
At present I'm only able to get this to work with exact matches (as below).
//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)
Change the query that finds your UI element to do so by a method other than its name. The UI test recording feature in Xcode can help you work through this — see the WWDC2015 session introducing UI testing for an example of doing this.
Once you've found your UI element without using its name, your expectation test can use a predicate that tests the name for a substring.
Or:
app's subelement matching a predicate (see elementMatchingPredicate in XCUIElementQuery) rather than subscripting, then you can use your exists test to verify that it's there. Again, see the WWDC session for examples.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With