Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XC UITesting with Dynamic Labels

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)
like image 687
Andrew Lombard Avatar asked Jun 11 '26 18:06

Andrew Lombard


1 Answers

  1. 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.

  2. 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:

  1. Explicitly use a query to find 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.
like image 189
rickster Avatar answered Jun 14 '26 08:06

rickster