是否可以在 XCUITest 中验证通知横幅已发送到屏幕?
我可以向通知添加可访问性标识符,但是当横幅出现在屏幕上时,我无法让 XCUITest 与之交互。我知道 XCUITest 在与应用程序不同的进程中运行,但我想知道它是否仍然可以与通知交互,或者它是否超出了 XCUITest 的范围?
谢谢,
最佳答案
使用 Xcode 9 现在可以做到这一点。您可以访问其他应用程序。这包括包含通知横幅的跳板。
XCUIApplication
现在有一个新的初始化程序,它将包标识符作为参数。它为您提供了对使用该 bundle 标识符的应用程序的引用。然后您可以使用普通查询来访问 UI 元素。就像您之前使用自己的应用所做的那样。
这是一个检查应用程序关闭时是否显示本地通知的测试:
import XCTest
class UserNotificationUITests: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
}
func testIfLocalNotificationIsDisplayed() {
let app = XCUIApplication()
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
app.launch()
// close app to trigger local notification
XCUIDevice.shared.press(XCUIDevice.Button.home)
// wait for the notification
let localNotification = springboard.otherElements["USERNOTIFICATION, now, Buy milk!, Remember to buy milk from store!"]
XCTAssertEqual(waiterResultWithExpectation(localNotification), XCTWaiter.Result.completed)
}
}
extension UserNotificationUITests {
func waiterResultWithExpectation(_ element: XCUIElement) -> XCTWaiter.Result {
let myPredicate = NSPredicate(format: "exists == true")
let myExpectation = XCTNSPredicateExpectation(predicate: myPredicate,
object: element)
let result = XCTWaiter().wait(for: [myExpectation], timeout: 6)
return result
}
}
您可以查看包含此测试的演示应用程序 here
您还可以使用 UITest 测试远程通知。这需要做更多的工作,因为您不能直接从您的代码中安排远程通知。您可以使用名为 NWPusher 的服务为了那个原因。我写了一篇关于 how to test Remote Notifications with Xcode UITests 的博文还有一个 demo project在 github 上。
关于ios - XCUITest 与通知横幅交互。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537678/