我有一个应用程序,其主要目的是将数据输入 HealthKit。我想编写一些 Xcode UI 测试来验证它是否成功写入了这些数据,但我在验证 Health 应用程序中的数据时遇到了一些困难。
当我最初记录我的测试时,它跳过了我模拟的 Home 按钮按下,但是当我滑动到第一个主屏幕并导航到 Health 应用程序以显示数据点时它正在记录。
我搜索了如何按下主页按钮,并找到了这个(有效):
XCUIDevice.shared.press(.home)
但是,它记录的其他调用都没有实际用于应用程序外部的导航。在主屏幕上记录的滑动代码显然看起来不对,当我将 tap()
替换为 swipeRight()
或 swipeLeft( )
:
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()
您可以在 this post 中找到系统应用程序的 bundle 标识符列表。
Xcode 8
对于 Xcode 8 来说有点复杂 为了从 Springboard 启动应用程序,您需要导入以下 header
https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIElement.h https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIApplication.h
然后使用以下(例如健康)
objective-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
swift
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")
}
}
}
关于ios - 如何在 iOS Xcode UI 测试用例中启动系统应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34820225/