👻
GhostHand您是否曾经尝试过使用XCUITest测试自定义方案或通用链接?您可能尝试过使用模拟器的内置应用(如消息或Safari),但遇到了挫折。随着苹果不断在模拟器中对这些应用进行调整,这就像试图击中移动的目标。
但是,如果有一种简单、可靠且快速的方式来测试自定义方案/通用链接怎么办?这就是GhostHand的作用!GhostHand库包含一个预建的应用程序,它将安装到您的模拟器上。只需调用GhostHandLib的静态函数,它就会打开与您提供的URL配套的应用程序,并在打开时调用UIApplication.shared.open(url
):
安装
当前GhostHand支持CocoaPods、Carthage和手动安装
Cocoapods
首先在您的Podfile
中将GhostHand pod添加到项目的UI Test Target
target 'MyProjectUITests' do
# Other UI Test pods....
pod 'GhostHand'
end
然后转到Xcode中的UI Test Target,点击构建阶段
,并添加一个运行脚本,其中包含以下代码:
"${PODS_ROOT}/GhostHand/ghosthand_install.sh"
Carthage
将GhostHand库添加到您的Cartfile中
github "mattstanford/GhostHand"
正确链接库到您的UI测试目标后,在Xcode中点击项目,选择UI测试目标,点击构建阶段
,然后添加一个新的运行脚本,代码如下
"${PROJECT_DIR}/Carthage/Build/iOS/GhostHandLib.framework/ghosthand_install.sh"
手动安装
将GhostHandLib
框架适当地链接到您的UI测试目标,在Xcode中点击项目,选择UI测试目标,点击构建阶段
,然后添加一个新的运行脚本,代码如下
"<PATH_TO_GHOSTHANDLIB>/ghosthand_install.sh"
使用方法
当GhostHand在您的UI测试目标中正确设置后,就可以开始使用了!
以下是一个简单的UI测试,用它来测试GhostHand。请注意,我们假设正在测试的应用程序在info.plist中正确注册了自定义方案myApp://
func testSample() {
let app = XCUIApplication()
app.launch()
let appBooted = app.staticTexts["Sample App for 👻"].waitForExistence(timeout: 5)
XCTAssert(appBooted)
//Tap the home button to put the app under test in the background
XCUIDevice.shared.press(XCUIDevice.Button.home)
//Use the GhostHand companion app to launch the custom scheme
GhostHand.launch(url: "myApp://TEST")
//The app should be called by the GhostHand companion app
let appShowedAgain = app.staticTexts["Sample App for 👻"].waitForExistence(timeout: 5)
XCTAssert(appShowedAgain)
}