【问题标题】:Open iOS push notification from a UI Test从 UI 测试打开 iOS 推送通知
【发布时间】:2022-06-17 13:52:58
【问题描述】:

我已经花了几个小时尝试所有可能的解决方案,但都没有成功,我希望我有更多的运气直接询问。

我正在使用 XCode 13.2.1,并且正在使用 iPhone X (iOS 15.3.1) 进行测试。

我想制作一个 XCTest,向我的(已终止)应用发送推送通知,然后测试打开通知,单击它,应用打开并显示特定视图。

到目前为止,我设法发送了推送通知,它在测试发送后仅一秒钟就显​​示在设备中,然后它就消失了。如果我手动打开通知中心,那么通知就在那里,ok。

但我无法点击通知。这是我尝试过的:

测试 1:通知消失且未被点击。

func testWhenPushNotificationOpenThenCorrectPageIsShown() {

    sendPushNotification()
    let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    springboard.otherElements["MyApp, now, My notification text"].tap()
}

测试 2:通知消失且未被点击。

func testWhenPushNotificationOpenThenCorrectPageIsShown() {

    sendPushNotification()
    let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    springboard.otherElements["Notification"].descendants(matching: .any)["NotificationShortLookView"].tap()
}

测试 3:通知消失且未被点击。

func testWhenPushNotificationOpenThenCorrectPageIsShown() {

    sendPushNotification()
    let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    springboard.otherElements["Notification"].firstMatch.tap()
}

我也添加了 springboard.activate() 测试了这 3 个案例。

我想打开通知中心从状态栏进行扫描这样做:

测试 4:通知消失,显示通知中心,其中包含推送通知,但我不知道如何点击它(考虑到当我手动点击此通知时,它会显示“打开”按钮,我必须单击它才能打开应用程序)。

func testWhenPushNotificationOpenThenCorrectPageIsShown() {

    sendPushNotification()
    let app = XCUIApplication()
    app.launch()
    let coord1 = app.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.01))
    let coord2 = app.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.8))
    coord1.press(forDuration: 0.1, thenDragTo: coord2)
}

最后,我尝试更改我的应用通知设置以使其持久化。现在当设备收到它时,它并没有消失,但是上面详述的每个测试我都没有成功,通知没有被点击。

随着通知的持续,我记录了跳板内容,这就是我得到的关于推送通知的内容:

springboard.debugDescription

Attributes: Application, 0x127f137a0, pid: 62, label: ' '
Element subtree:
 →Application, 0x127f137a0, pid: 62, label: ' '
    ...
    Window (Main), 0x129a20120, {{0.0, 0.0}, {375.0, 812.0}}
      Other, 0x129a20230, {{0.0, 0.0}, {375.0, 812.0}}
        Other, 0x129a0ff60, {{0.0, 0.0}, {375.0, 812.0}}
          BannerNotification, 0x129a10070, {{8.0, 40.0}, {359.0, 75.3}}
            Other, 0x129a12620, {{8.0, 40.0}, {359.0, 75.3}}, label: 'Notification'
              Other, 0x129a12730, {{8.0, 40.0}, {359.0, 75.3}}
                BannerNotification, 0x129a071f0, {{8.0, 40.0}, {359.0, 75.3}}, identifier: 'NotificationShortLookView', label: 'MyApp, now, My notification text'

我不喜欢最后一个选项,因为我必须手动设置通知的持久模式(我想不可能以编程方式完成),但如果这是最好的选项,我会选择它。

我可以尝试什么?

【问题讨论】:

    标签: ios notifications xcuitest springboard


    【解决方案1】:

    第三次尝试时,您就快到了。这就是我在 iOS 14.5 和 15.5 上的项目中始终适用的方法:

    let notification = springboard.otherElements["Notification"]
    notification.tap()
    

    不使用firstMatch

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2016-03-24
      相关资源
      最近更新 更多