XCUITest与通知横幅进行交互。

P. *_*lle 2 xcode ui-automation apple-push-notifications ios xctest

是否可以在XCUITest中验证通知横幅是否已发送到屏幕?

我可以在通知中添加可访问性标识符,但是当横幅广告发送到屏幕时,让XCUITest与之交互时遇到了问题。我知道XCUITest是在与应用程序分开的进程中运行的,但是想知道是否仍可以与通知进行交互,或者它是否超出XCUITest的范围?

谢谢,

joe*_*ern 9

现在,使用Xcode 9可以实现。您可以访问其他应用。这包括包含通知横幅的跳板。

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
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处签出包括此测试的演示应用程序

您也可以使用UITests 测试远程通知。这需要做更多的工作,因为您不能直接从代码中安排远程通知。您可以为此使用名为NWPusher的服务。我写了一篇有关如何使用Xcode UITests测试远程通知的博客,并且在github上还有一个演示项目