【问题标题】:iOS best practice to test push notifcations (UI Test)iOS 测试推送通知的最佳实践(UI 测试)
【发布时间】:2023-03-24 15:43:01
【问题描述】:

我想通过推送通知对我的应用进行 UI 测试,也许你们中的一些人已经找到了最佳实践。有很多教程和问题,但我找不到令人满意的答案。

  1. 开始测试
  2. 发送推送通知
  3. 点击推送通知
  4. 做事

但是发送推送通知的最佳做法是什么?

  • 触发推送通知的休息调用
  • 获得对终端的访问权(但如何?)并运行:xcrun simctl push <device> com.example.my-app ExamplePush.apns
  • 发送本地推送通知?
  • 模拟服务器
  • 使用像Mussel这样的框架

谢谢! :)

【问题讨论】:

    标签: ios swift xcode xcode-ui-testing uitest


    【解决方案1】:

    使用终端通过单行命令测试推送通知

    在您的 Mac 中安装 Houston,在终端中运行以下命令。

    1. gem 安装休斯顿

      如果你遇到这样的错误,

      正在获取 houston-2.4.0.gem 错误:执行 gem 时 ... (Gem::FilePermissionError) 您没有 /Library/Ruby/Gems/2.6.0 目录的写入权限。

      首先在终端中运行以下命令来安装 Ruby

      brew 安装红宝石

      导出 GEM_HOME="$HOME/.gem"

      gem 安装导轨

      安装成功后再次运行

      gem 安装休斯顿

    2. 转到 pem 文件夹并从该文件夹打开终端。

    3. 像下面这样运行命令

      apn push "设备令牌" -c PEM_FILE_NAME -m "MESSAGE"

      喜欢:

      apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_dev.pem -m "测试"

    成功运行上述命令后,它会要求输入 PEM 密码,这是您的 pem 文件的密码。

    如果您的应用已上线,则使用生产 pem 文件名

    像这样,

    apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_pro.pem -m "测试"

    完成。

    对于 UI 测试,您可以使用本地通知,

    您必须为本地通知设置 categoryIdentifier 并确保在 AppExtension Info.plist 文件中将相同的 categoryIdentifier 设置为 UNNotificationExtensionCategory

    更多详情请参考以下链接

    https://levelup.gitconnected.com/custom-push-notification-in-ios-swift-5-210552643e86#:~:text=Go%20to%20xcode%20select%20File,have%20its%20unique%20category%20Identifier.

    以下是使用类别标识符触发本地通知的示例代码

    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.categoryIdentifier = "LOCAL_NOTIFICATION"
        
    if let info = userInfo {
           let dic = ["body" : "Custom Data"]
           content.userInfo = dic as [AnyHashable : Any]
     }
        
    content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: sound))
                
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
                
    let request = UNNotificationRequest(identifier: "LOCAL_NOTIFICATION", content: content, trigger: trigger)
        
    let notificationCenter = UNUserNotificationCenter.current()
         notificationCenter.add(request) { (error) in
              if let error = error {
                    print("Error \(error.localizedDescription)")
               }
     }
    

    【讨论】:

    • 但是我无法使用这种方法从 UI 测试触发推送通知,对吗?
    • 我认为以上信息对于推送通知自定义 UI 测试来说已经足够了。
    • 这实际上是一个很好的解决方案。您的代码在 UI 测试中不起作用,但您可以将其放在应用程序委托中并使用 launchArguments。谢谢!我稍后会发布代码:)
    【解决方案2】:

    如果您想创建测试推送通知,那么出于测试目的

    https://github.com/noodlewerk/NWPusher

    这个库非常有用。

    【讨论】:

    • 您好,非常感谢您的回答。我已经尝试过 NWPusher,但并不是很满意。
    • 您能否分享一下 NWPusher 面临的问题?
    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多