Backchannel for UITesting。
Snapshot超级棒。
现在它使用 UI 测试。
UI 测试比 UI 自动化好得多 - 但有时,你只是想作弊。
HSTestingBackchannel 允许您通过通知的形式以简简单单的方式将消息从您的 UITesting 测试直接发送到您正在运行的应用程序。
安装
使用 CocoaPods 安装
pod 'HSTestingBackchannel', :configuration => ['Debug']
用法
- 在您的 swift 项目中设置一个 'Active Compilation Condition' 来定义 SNAPSHOT
- 在您的 App Delegate 中安装辅助工具
#if SNAPSHOT
import HSTestingBackchannel
#endif
然后在 application(_:didFinishLaunchingWithOptions:)
中
#ifdef SNAPSHOT
HSTestingBackchannel.installReceiver()
#endif
- 从您的 UITesting 类发送通知
HSTestingBackchannel.sendNotification("SnapshotTest")
或者
HSTestingBackchannel.sendNotification("SnapshotTest",with: ["aKey":"aValue"])
- 在您的应用程序中响应通知
#if SNAPSHOT
NotificationCenter.default.addObserver(
forName: NSNotification.Name("SnapshotTest"),
object: nil,
queue: .main) { _ in
//Do Something
})
#endif
奖励 - 将模拟文件复制到模拟器
在测试方法(或setUp中),调用类似下面的代码:
HSTestingBackchannel.installFiles(from: "..pathTo/fastlane/DummyImages",
to: HSTestingResources)
这将把DummyImages中的内容安装到你正在运行的应用的资源文件夹中。你还可以直接将其安装到应用中的Documents目录。
多个同时模拟器
默认情况下,Fastlane现在会同时运行多个模拟器。这意味着你需要确保每个模拟器的服务器在不同的测试上都运行。
使用设置方法执行以下操作:
let app = XCUIApplication()
HSTestingBackchannel.port = UInt.random(in: 8000 ... 60000)
app.launchArguments.append(contentsOf: ["-HSTestingBackchannelPort", "\(HSTestingBackchannel.port)"])
Snapshot.setupSnapshot(app, waitForAnimations: true)
app.launch()
如何工作
HSTestingBackchannel会在你的主应用(GCDWebServer)中安装一个web服务器。
你只需要直接向该服务器发送请求 - 它会识别请求并广播NSNotifications。