是否可以在XCUITest中验证通知标语是否已发送到屏幕?
我可以将可访问性标识符添加到通知中,但当横幅进入屏幕时,我在让XCUITest与其交互时遇到问题。我知道XCUITest运行在一个独立于应用程序的进程中,但是我想知道是否仍然可以与通知交互,或者它是否超出了XCUITest的范围?
谢谢,
发布于 2017-09-07 16:39:09
随着Xcode9的出现,这一点现在成为可能。您可以访问其他应用程序。这包括包含通知横幅的跳板。
XCUIApplication现在有了一个新的初始化器,它接受捆绑标识符作为参数。它为您提供了使用该捆绑包标识符的应用程序的引用。然后,您可以使用普通查询来访问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
您还可以使用UITests测试远程通知。这需要更多的工作,因为您不能直接从您的代码调度远程通知。为此,您可以使用一个名为NWPusher的服务。我写了一篇关于how to test Remote Notifications with Xcode UITests的博文,在github上也有一个demo project。
发布于 2021-04-01 04:07:27
Xcode 12 iOS 14*
也可以在通知中访问/断言:
let notification = springBoard.otherElements["Notification"].descendants(matching: .any)["APPNAME, now, TITLE, BODY"]
if notification.waitForExistence(timeout: 10) {
notification.tap()
}发布于 2017-06-14 21:19:39
不幸的是,现在还没有。XCUITest不提供对通知栏的访问。对于你可能想出的几乎每一个测试用例场景,Appium都是一个很好的选择,然而Apple仍然没有提供测试推送通知的方法。解决此问题的一种方法是强制您的应用程序发送通知,在显示通知弹出窗口时截屏,然后使用图像识别技术验证通知是否正确(例如OpenCV)。这是我想要使用的更多的“变通方法”,但这是我目前所知道的唯一可用的方法,希望这能有所帮助。
https://stackoverflow.com/questions/42537678
复制相似问题