Reputation: 5149
How can I tap a button after check if it is enabled?
I found this sample here, but It didn't work for my case.
Delay/Wait in a test case of Xcode UI testing
Here a part of code:
let app = XCUIApplication()
let button = app.buttons["Tap me"]
let exists = NSPredicate(format: "exists == 1")
expectationForPredicate(exists, evaluatedWithObject: button){
button.tap()
return true
}
waitForExpectationsWithTimeout(5, handler: nil)
but my test fails by tapping of the button. Thanks
Upvotes: 2
Views: 1877
Reputation: 7649
Your code sample doesn't check if the button is enabled, only if it exists.
The block that you pass into expectationForPredicate
will be executed if the button exists, so the button will be tapped even if the button is disabled.
To include a check for the button being enabled:
let app = XCUIApplication()
let button = app.buttons["Tap me"]
let exists = NSPredicate(format: "exists == 1")
expectationForPredicate(exists, evaluatedWithObject: button) {
// If the button exists, also check that it is enabled
if button.enabled {
button.tap()
return true
} else {
// Do not fulfill the expectation since the button is not enabled
return false
}
}
waitForExpectationsWithTimeout(5, handler: nil)
Upvotes: 8