XCUITest与通知横幅进行交互. [英] XCUITest interact with a Notification Banner.

查看:135
本文介绍了XCUITest与通知横幅进行交互.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible in XCUITest to validate that a notification banner is sent to the screen?

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

I can add an accessibility identifier to the notification, but I am having a problem getting XCUITest to interact with it when the banner is ent to the screen. I know that XCUITest runs in a separate process from the app, but was wondeering if it was still possible to interact with the notification or is it beyond the scope of XCUITest?

谢谢

推荐答案

使用 Xcode 9 ,现在可以做到这一点.您可以访问其他应用.其中包括包含通知横幅的跳板.

With Xcode 9 this is now possible. You can get access to other apps. That includes the springboard which contains the Notification banner.

XCUIApplication现在具有一个新的初始化程序,该初始化程序将包标识符作为参数.它为您提供了使用该捆绑标识符的应用程序的参考.然后,您可以使用常规查询来访问UI元素.就像您以前使用自己的应用程序一样.

XCUIApplication now has a new initializer that takes a bundle identifier as parameter. It gives you a reference to the app that uses that bundle identifier. You can then use the normal queries to access UI elements. Just like you did before with your own app.

这是一项测试,用于检查在关闭应用程序时是否显示本地通知:

This is a test that checks if a Local Notification is being displayed when the app is closed:

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
    }
}

您可以在此处

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

You can also test Remote Notifications with UITests. That's needs a bit more work, because you cannot directly schedule Remote Notifications from your code. You can use a service called NWPusher for that. I wrote a blogpost about how to test Remote Notifications with Xcode UITests and there also is a demo project on github.

这篇关于XCUITest与通知横幅进行交互.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆