Set an accessibility identifier in your application code, and then search for the button using that identifier in your tests.
// app code
let button: UIButton!
button.accessibilityIdentifier = "myButton"
// UI test code
func testMyButtonIsDisplayed() {
let app = XCUIApplication()
let button = app.buttons["myButton"]
XCTAssertTrue(button.exists)
}
The accessibility identifier is set independently of text on the button, and is also independent of the accessibility label. It's not best practice to put identifiers for UI elements as the accessibility label, since the accessibility label is read to VoiceOver users to explain the element to them.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…