So I have a simple view for my SwiftUI application below:
import SwiftUI
struct ContentView: View {
var body: some View {
List {
ForEach(0..<4) { index in
RowView(index: index)
.accessibilityIdentifier("row")
}
}
}
}
struct RowView: View {
let index: Int
var body: some View {
HStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Spacer()
ButtonView(index: index)
}
.padding()
}
}
struct ButtonView: View {
let index: Int
var body: some View {
VStack {
if index != 3 {
Button(action: {}, label: {
Text("Cancel")
})
.accessibilityIdentifier("cancel_button")
}
Button(action: {}, label: {
Text("Submit")
})
.accessibilityIdentifier("submit_button")
}
}
}
My goal is to reference each cancel and submit button per row for a UI Test, which I have tried doing so here:
let rowElements = app.tables.cells.matching(identifier: "row")
for i in 0..<rowElements.count {
let cancelButton = rowElements.element(boundBy: i).buttons["cancel_button"]
let submitButton = rowElements.element(boundBy: i).buttons["submit_button"]
XCTAssertTrue(cancelButton.exists, "Cancel button does not exist in row \(i)")
XCTAssertTrue(submitButton.exists, "Submit button does not exist in row \(i)")
}
Even with the identifiers set like this my tests fail and say it cannot find the buttons labeled as they are with their specific identifiers or the rows. What is the correct way to reference elements if they are in a list or table for UI Testing? I'm specifically trying to use accessibility Identifiers to make my life easy.