Ratnesh Shukla
Ratnesh Shukla

Reputation: 1178

How to access the `customLabel` in a `customTableViewCell` at UITests?

There are many rows. I want to access particular label. Then based on that label text, perform either Tap or ignore.

Upvotes: 0

Views: 1567

Answers (1)

Oletha
Oletha

Reputation: 7649

Set an accessibilityIdentifier on the label inside the cell, then find that label using the identifier in your test. You can then inspect its text using the label property of XCUIElement and decide whether to tap it or not.

// app code
let label = UILabel!
label.accessibilityIdentifier = "myLabel"

// test code
let app = XCUIApplication()
let labels = app.staticTexts.matching(identifier: "myLabel")
for i in 0..<labels.count {
    let label = labels.element(boundBy: i)
    if label.label == "interesting text" {
        label.tap()
    }
}

You could also use an NSPredicate to narrow down the query before you loop through and tap each element.

// test code
let app = XCUIApplication()
let predicate = NSPredicate(format: "label MATCHES 'interesting text'")
let labels = app
    .staticTexts
    .matching(identifier: "myLabel")
    .matching(predicate)
for i in 0..<labels.count {
    labels.element(boundBy: i).tap()
}

Upvotes: 2

Related Questions