[英]Dismissing alert XCUITest
I am trying to dismiss alerts on my XCUITest suite, I am currently using我正在尝试关闭我的 XCUITest 套件上的警报,我目前正在使用
app.alerts.buttons["OK"].tap()
Using this dismisses the alert, but then the application carries on trying to tap the button and then fails with the error message " UI TESTING FAILURE- No matches found for alert "使用它会解除警报,但随后应用程序继续尝试点击按钮,然后失败并显示错误消息“ UI TESTING FAILURE- No matches found for alert ”
This is the console output这是控制台 output
t = 0.00s Start Test at 2016-11-03 14:07:30.827
t = 0.00s Set Up
t = 0.00s Launch uk.co.myapp
t = 4.89s Waiting for accessibility to load
t = 12.55s Wait for app to idle
t = 18.46s Snapshot accessibility hierarchy for uk.co.myapp
t = 19.24s Find: Descendants matching type Button
t = 19.25s Find: Elements matching predicate '"Enable notifications" IN identifiers'
t = 19.26s Tap "Enable notifications" Button
t = 19.26s Wait for app to idle
t = 19.40s Find the "Enable notifications" Button
t = 19.40s Snapshot accessibility hierarchy for uk.co.myapp
t = 27.43s Find: Descendants matching type Button
t = 27.48s Find: Elements matching predicate '"Enable notifications" IN identifiers'
t = 27.52s Wait for app to idle
t = 27.93s Synthesize event
t = 28.31s Wait for app to idle
t = 33.94s Tap "OK" Button
t = 33.94s Wait for app to idle
t = 35.19s Find the "OK" Button
t = 35.20s Snapshot accessibility hierarchy for uk.co.myapp
t = 39.68s Find: Descendants matching type Alert
t = 39.69s Wait for app to idle
t = 39.82s App event loop idle notification not received, will attempt to continue.
t = 39.82s App animations complete notification not received, will attempt to continue.
t = 39.83s Check for UI interruption: Descendants matching type Alert
t = 39.83s Snapshot accessibility hierarchy for com.apple.springboard
t = 39.93s Find: Descendants matching type Alert
t = 39.94s Find the "“VoucherCodes” Would Like to Send You Notifications" Alert
t = 39.94s Use cached accessibility hierarchy for com.apple.springboard
t = 39.97s Find: Descendants matching type Alert
t = 39.97s Find: Identity Binding
t = 39.98s Use cached accessibility hierarchy for com.apple.springboard
t = 39.99s Find: Descendants matching type Alert
t = 40.00s Find: Identity Binding
t = 40.01s Find: Descendants matching type Button
t = 40.01s Find: Elements matching predicate 'userTestingAttributes CONTAINS "cancel-button"'
t = 40.01s Get number of matches for: Descendants matching type Button
t = 40.13s Snapshot accessibility hierarchy for com.apple.springboard
t = 40.23s Find: Descendants matching type Alert
t = 40.23s Find: Identity Binding
t = 40.24s Find: Descendants matching type Button
t = 40.24s Get all elements bound by accessibility element for: Elements matching predicate 'userTestingAttributes CONTAINS "default-button"'
t = 40.34s Snapshot accessibility hierarchy for com.apple.springboard
t = 40.43s Find: Descendants matching type Alert
t = 40.45s Find: Identity Binding
t = 40.46s Find: Descendants matching type Button
t = 40.46s Find: Elements matching predicate 'userTestingAttributes CONTAINS "default-button"'
t = 40.47s Use cached accessibility hierarchy for com.apple.springboard
t = 40.49s Find: Descendants matching type Alert
t = 40.49s Find: Identity Binding
t = 40.50s Find: Descendants matching type Button
t = 40.50s Find: Elements matching predicate 'userTestingAttributes CONTAINS "default-button"'
t = 40.50s Find: Identity Binding
t = 40.50s Tap "OK" Button
t = 40.50s Wait for app to idle
t = 40.61s App event loop idle notification not received, will attempt to continue.
t = 40.61s App animations complete notification not received, will attempt to continue.
t = 40.62s Find the "OK" Button
t = 40.62s Snapshot accessibility hierarchy for com.apple.springboard
t = 40.70s Find: Descendants matching type Alert
t = 40.71s Find: Identity Binding
t = 40.71s Find: Descendants matching type Button
t = 40.71s Find: Elements matching predicate 'userTestingAttributes CONTAINS "default-button"'
t = 40.72s Find: Identity Binding
t = 40.72s Wait for app to idle
t = 40.84s App event loop idle notification not received, will attempt to continue.
t = 40.84s App animations complete notification not received, will attempt to continue.
t = 40.84s Synthesize event
t = 41.17s Wait for app to idle
t = 41.43s App event loop idle notification not received, will attempt to continue.
t = 41.43s App animations complete notification not received, will attempt to continue.
t = 41.44s Snapshot accessibility hierarchy for com.apple.springboard
t = 41.52s Find: Descendants matching type Alert
t = 41.53s Find: Identity Binding
t = 41.54s Wait for view controller view-did-disappear
t = 42.21s Snapshot accessibility hierarchy for uk.co.myapp
t = 45.68s Find: Descendants matching type Alert
t = 45.69s Wait for app to idle
t = 46.82s Find the "OK" Button (retry 1)
t = 46.82s Snapshot accessibility hierarchy for
myapp t = 50.18s Find: Descendants matching type Alert t = 50.19s Wait for app to idle t = 51.38s Find the "OK" Button (retry 2) t = 51.38s Snapshot accessibility hierarchy for uk.myapp t = 55.06s Find: Descendants matching type Alert t = 55.06s Wait for app to idle t = 55.44s Assertion Failure: signUpSetUp.swift:21: UI Testing Failure - No matches found for Alert Query input was {( Application 0x7fb51cf80840: {{0.0, 0.0}, {375.0, 667.0}}, label myapp t = 50.18s Find: Descendants matching type Alert t = 50.19s Wait for app to idle t = 51.38s 找到“OK”按钮(重试 2) t = 51.38s uk.myapp 的快照可访问性层次结构 t = 55.06s Find :后代匹配类型警报 t = 55.06s 等待应用程序空闲 t = 55.44s 断言失败:signUpSetUp.swift:21:UI 测试失败 - 未找到警报查询输入的匹配项是 {(应用程序 0x7fb51cf80840:{{0.0, 0.0} , {375.0, 667.0}}, label
Is there a better way of solving this issue?有没有更好的方法来解决这个问题? Or is there a solution to where I have gone wrong?或者有什么办法可以解决我出错的地方吗? Been puzzled on this for a while now!对此困惑了一段时间!
I improve the answer of @user3271402 我改进了@ user3271402的答案
addUIInterruptionMonitorWithDescription("alert description") { (alert) -> Bool in
let alertButton = alert.buttons["OK"]
if alertButton.exists {
alertButton.tap()
return true
}
return false
}
app.tap()
Just tap if the alert exists. 如果警报存在,只需点击即可。 If the alert doesn't happen return false and after tap the app and the end. 如果没有发生警报则返回false并点击应用程序和结束。
There is a method in XCTest for alert handling: XCTest中有一种方法用于警报处理:
addUIInterruptionMonitorWithDescription("iTunesStoreAlert") { alert in
alert.buttons["OK"].tap()
return true
}
You could put this at the beginning of your test .. Or where you are expected to receive an alert 您可以在测试开始时将其放入..或者您希望收到警报的位置
Note that the addUIInteruptionMonitorWithDescription
approach can only be used for alerts presented outside the normal work flow.请注意, addUIInteruptionMonitorWithDescription
方法只能用于在正常工作流程之外呈现的警报。 Per Apple[1]:每个苹果[1]:
Important重要的
When an alert or other modal UI is an expected part of the test workflow, don't write a UI interruption monitor.当警报或其他模式 UI 是测试工作流的预期部分时,不要编写 UI 中断监视器。 The test won't use the monitor because the modal UI isn't blocking the test.测试不会使用监视器,因为模态 UI 不会阻止测试。 A UI test only tries its UI interruption monitors if the elements it needs to interact with to complete the test are blocked by an interruption from an unrelated UI. UI 测试仅在它需要与之交互以完成测试的元素被来自不相关 UI 的中断阻止时才尝试其 UI 中断监视器。
If the alert is presented by the normal flow, you can just use normal XCTestCase methods:如果警报由正常流程呈现,您可以使用正常的 XCTestCase 方法:
expectation(
for: NSPredicate("exists == true"),
evaluatedWith: XCUIApplication().staticTexts["Error Sending Mail"],
handler: nil) { error in
// handle failure here
}
XCUIApplication().buttons["OK"].tap()
[1] https://developer.apple.com/documentation/xctest/xctestcase/handling_ui_interruptions [1] https://developer.apple.com/documentation/xctest/xctestcase/handling_ui_interruptions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.