gpt4 book ai didi

iOS - 在 UITests 目标中使用推送通知

转载 作者:行者123 更新时间:2023-11-28 21:24:06 25 4
gpt4 key购买 nike

我需要在 AppUITests 中使用推送通知。有什么方法可以使用自定义授权文件添加/更新 AppUITests 目标的设置吗?

最佳答案

在 Xcode 9 中,您可以使用名为 NWPusher 的框架在 UITest 中使用远程通知。

要在您的 UITest 中测试远程通知,您必须执行以下步骤:

  1. 将 NWPusher 添加到您的 UITest 目标
  2. 从 Apple 的开发中心下载 APN 开发证书并将其添加到您的钥匙串(keychain)(如果尚不存在)
  3. 将证书导出为 p12 文件
  4. 编写测试

我试过了,这是我的测试文件:

我的演示应用程序根据收到的推送通知呈现三个不同的模态视图 Controller 。所以本次测试有3种不同的推送通知。

import XCTest
import PusherKit

class PushNotificationUITests: XCTestCase {

override func setUp() {
super.setUp()
continueAfterFailure = false
}

func testPushNotifications() {
let app = XCUIApplication()
app.launchArguments.append("isRunningUITests")
app.launch()

// access to the springboard (to be able to tap the notification later)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

// dismiss the system dialog if it pops up
allowPushNotificationsIfNeeded()

// get the current deviceToken from the app
let deviceToken = app.staticTexts.element(matching: .any, identifier: "tokenLabel").label

// close app
XCUIDevice.shared.press(XCUIDevice.Button.home)
sleep(1)

// trigger red Push Notification
triggerPushNotification(
withPayload: "{\"aps\":{\"alert\":\"Hello Red\"}, \"vcType\":\"red\"}",
deviceToken: deviceToken)

// tap on the notification when it is received
springboard.otherElements["PUSHNOTIFICATION, now, Hello Red"].tap()

// check if the red view controller is shown
XCTAssert(app.staticTexts["Red"].exists)

// dismiss modal view controller and close app
app.buttons["Close"].tap()
XCUIDevice.shared.press(XCUIDevice.Button.home)
sleep(1)

// trigger green Push Notification
triggerPushNotification(
withPayload: "{\"aps\":{\"alert\":\"Hello Green\"}, \"vcType\":\"green\"}",
deviceToken: deviceToken)

// tap on the notification when it is received
springboard.otherElements["PUSHNOTIFICATION, now, Hello Green"].tap()

// check if the green view controller is shown
XCTAssert(app.staticTexts["Green"].exists)

// dismiss modal view controller and close app
app.buttons["Close"].tap()
XCUIDevice.shared.press(XCUIDevice.Button.home)
sleep(1)

// trigger blue Push Notification
triggerPushNotification(
withPayload: "{\"aps\":{\"alert\":\"Hello Blue\"}, \"vcType\":\"blue\"}",
deviceToken: deviceToken)

// tap on the notification when it is received
springboard.otherElements["PUSHNOTIFICATION, now, Hello Blue"].tap()

// check if the blue view controller is shown
XCTAssert(app.staticTexts["Blue"].exists)

// dismiss modal view controller
app.buttons["Close"].tap()
}
}

extension XCTestCase {
func triggerPushNotification(withPayload payload: String, deviceToken: String) {
let uiTestBundle = Bundle(for: PushNotificationUITests.self)
guard let url = uiTestBundle.url(forResource: "pusher.p12", withExtension: nil) else { return }

do {
let data = try Data(contentsOf: url)
let pusher = try NWPusher.connect(withPKCS12Data: data, password: "pusher", environment: .auto)
try pusher.pushPayload(payload, token: deviceToken, identifier: UInt(arc4random_uniform(UInt32(999))))
} catch {
print(error)
}
}

func allowPushNotificationsIfNeeded() {
addUIInterruptionMonitor(withDescription: "“RemoteNotification” Would Like to Send You Notifications") { (alerts) -> Bool in
if(alerts.buttons["Allow"].exists){
alerts.buttons["Allow"].tap();
}
return true;
}
XCUIApplication().tap()
}
}

我写的很详细blogpost关于这个,你可以下载演示here

关于iOS - 在 UITests 目标中使用推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com