我有一个应用程序,其主要目的是将数据输入HealthKit。我想编写一些 Xcode UI 测试来验证它是否成功写入了此数据,但在验证"健康"App 中的数据时遇到了一些困难。
当我最初录制测试时,它跳过了我模拟的主页按钮按下,但是当我滑动到第一个主屏幕并导航到"健康"应用以显示数据点时,它正在录制。
我搜索了如何按下主页按钮,并找到了这个(有效):
XCUIDevice.shared.press(.home)
但是,它记录的其他调用实际上都不适用于应用程序之外的导航。在主屏幕上滑动的录制代码显然看起来是错误的,当我用swipeRight()
或swipeLeft()
替换tap()
时也不起作用:
app.childrenMatchingType(.Window).elementBoundByIndex(1).childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).elementBoundByIndex(0).childrenMatchingType(.ScrollView).element.tap()
接下来的几行,用于在主屏幕上启动应用程序,甚至不适用于当前可见页面上的应用程序图标:
let elementsQuery = app.scrollViews.otherElements
elementsQuery.icons["Health"].tap()
有没有办法实现我想要做的事情,或者我需要等到我从 HealthKit 读取的功能添加到我的应用程序之前验证端到端测试?
Xcode 9
这是使用 Xcode 9 的解决方案
let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()
您可以在这篇文章中找到系统应用程序的捆绑标识符列表
Xcode 8
对于Xcode 8来说,它有点复杂。为了从跳板启动应用程序,您需要导入以下标头
https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIElement.hhttps://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIApplication.h
然后使用以下方法(例如,使用"健康")
目标-C
@interface Springboard : NSObject
+ (void)launchHealth;
@end
@implementation Springboard
+ (void)launchHealth
{
XCUIApplication *springboard = [[XCUIApplication alloc] initPrivateWithPath:nil bundleID:@"com.apple.springboard"];
[springboard resolve];
XCUIElement *icon = springboard.icons[@"Health"];
if (icon.exists) {
[icon tap];
// To query elements in the Health app
XCUIApplication *health = [[XCUIApplication alloc] initPrivateWithPath:nil bundleID:@"com.apple.Health"];
}
}
@end
迅速
class Springboard {
static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
class func launchHealth() {
springboard.resolve()
let icon = springboard.icons["Health"]
if icon.exists {
icon.tap()
// To query elements in the Health app
let health = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.Health")
}
}
}
Swift 4
let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
按下主页按钮并移出应用程序 UI 时,您无法执行 UI 操作,因为代码中的应用程序变量指向您的应用程序。
您可能有类似的代码
let app = XCUIApplication()
所以你应该修改那个XCUIApplication()行。
let app = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
现在,您可以移出应用程序。
据我所知,拥有独立的 UITestAgent 应用程序并使用跳板捆绑包 ID 启动其 UiTestcases是件好事,这样您就可以在该应用程序的帮助下测试任何应用程序,而不是像我在产品 XYZ 代码库中编写一些测试用例一样,对于下一个产品 ABC,我将在 ABC 产品的代码库中编写测试!