PterodactylLib 1.1.1

PterodactylLib 1.1.1

Matt Stanford 维护。



Pterodactyl

用于简化发送 Push 通知的 XCUITests 库

从 Xcode 11.4 开始,模拟器中现在有了推送通知。不幸的是,您执行推送通知的方式不适合自动化测试,尤其是 XCUITests。但请放心,Pterodactyl 库旨在解决这个问题!

只需将 Pterodacyl 库添加到您的 UI 测试目标中,您就能在 UI 测试中轻松发送推送通知!

工作原理

Xcode 11.4 增强了向您的模拟器发送推送通知的能力。您可以通过两种方式来执行此操作

  1. 创建一个 APNS 载荷 JSON 文件,并将其手动拖放到正在运行的模拟器中
  2. 创建一个 APNS 载荷 JSON 文件,并从终端运行一个命令,以 JSON 文件作为参数。

苹果没有向我们提供 API 在 XCUITest 中使用这项新功能。Pterodactyl 通过在您的计算机上启动一个本地的 macOS 服务器并运行相应的 xcrun 命令来处理这一限制,当接收到适当的网络请求时。您添加到应用的库简化了创建合适的 JSON 文件和向本地服务器发送正确网络请求的所有杂乱事情。

用法

首先,使用您的 XCUIApplication 实例初始化一个 Pterodactyl 实例

let app = XCUIApplication()
let pterodactyl = Pterodactyl(targetApp: app)

您可以用类似以下方式进行发送包含自定义消息的推送通知到应用

pterodactyl.triggerSimulatorNotification(withMessage: "here's a simple message")

如果您需要更复杂的应用程序通知服务(APNS)通知,您还可以指定将作为apns有效载荷一部分的自定义密钥。

pterodactyl.triggerSimulatorNotification(withMessage: "here's a more complicated message", additionalKeys: ["badge": 42, "myWeirdCustomKey": "foobar"])

上面的例子将会向应用程序发送一个与以下等效的APNS有效载荷。

{
    "aps": { 
        "alert": "here's a more complicated message",
        "badge": 42,
        "myWeirdCustomKey": "foobar""
    }
}

如果您想对有效载荷有完全的控制(例如,您想要提供一个不在aps有效载荷之外的密钥),可以通过将有效载荷指定为字典来触发通知。

pterodactyl.triggerSimulatorNotification(withFullPayload: ["aps": ["alert": "here's a message with the full payload supplied", "badge": 1, "sound": "default"], "someOtherKey": "some other key defined outside the aps payload"])

这会发送一个与以下等效的有效载荷。

{
    "aps": { 
        "alert": "here's a message with the full payload supplied",
        "badge": 42,
        "myWeirdCustomKey": "foobar""
    },
    "someOtherKey": "some other key defined outside the aps payload"
}

示例

以下是一个使用Pterodactyl应用程序进行测试的简单推送通知用例的UI测试示例。

import XCTest
import PterodactylLib

class MyUITestExample: XCTestCase {

    func testSimulatorPush() {
        var app = XCUIApplication()
        
        let pterodactyl = Pterodactyl(targetApp: app)
        waitForElementToAppear(object: app.staticTexts["Pterodactyl Example"])
        
        //Tap the home button
        XCUIDevice.shared.press(XCUIDevice.Button.home)
        sleep(1)

        //Trigger a push notification
        pterodactyl.triggerSimulatorNotification(withMessage: "here's a simple message")
        
        //Tap the notification when it appears
        let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        let springBoardNotification = springboard.otherElements["NotificationShortLookView"]
        waitForElementToAppear(object: springBoardNotification)
        springBoardNotification.tap()

        waitForElementToAppear(object: app.staticTexts["Pterodactyl Example"])
    }

    func waitForElementToAppear(object: Any) {
        let exists = NSPredicate(format: "exists == true")
        expectation(for: exists, evaluatedWith: object, handler: nil)
        waitForExpectations(timeout: 5, handler: nil)
    }
}

归属

PterodactylServer图标由FreePik在www.flaticon.com设计。