如何在 iOS Xcode UI 测试用例中启动系统应用程序
How to launch system apps in an iOS Xcode UI test case
我有一个应用程序,其主要目的是将数据输入 HealthKit。我想编写一些 Xcode UI 测试来验证它是否成功写入了这些数据,但我在验证健康应用程序中的数据时遇到了一些困难。
当我最初记录我的测试时,它跳过了我模拟的主页按钮按下,但它在我滑动到第一个主屏幕并导航到健康应用程序以显示数据点时正在记录。
我搜索了如何按下主页按钮,并找到了这个(有效):
XCUIDevice.shared.press(.home)
但是,它记录的其他呼叫中的 none 实际上适用于应用程序外部的导航。在主屏幕上记录的滑动代码显然看起来不对,并且当我将 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 读取的功能添加到我的应用程序中才能验证端到端测试?
您将 UI 测试限制在您的应用程序中,当您按下主页按钮并移出应用程序时 UI 您无法在您的应用程序变量中执行 UI 操作代码指向您的应用程序。
您可能有类似
的代码
let app = XCUIApplication()
因此您应该修改 XCUIApplication() 行。
let app = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
现在您可以移出应用程序了。
据我所知,拥有独立的 UITestAgent 应用程序并使用 springboard bundle id 启动其 UiTestcases 是件好事,这样您就可以在该应用程序的帮助下测试任何应用程序,而不是像我编写的一些测试用例在产品 XYZ 代码库和下一个产品 ABC 中,我将在 ABC 产品的代码库中编写测试!
Xcode 9
这是使用 Xcode 9
的解决方案
let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()
您可以在 this post
中找到系统应用的捆绑标识符列表
Xcode 8
对于Xcode8有点复杂
为了从 Springboard 启动应用程序,您需要导入以下内容 headers
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")
}
}
}
Swift 4
let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
我有一个应用程序,其主要目的是将数据输入 HealthKit。我想编写一些 Xcode UI 测试来验证它是否成功写入了这些数据,但我在验证健康应用程序中的数据时遇到了一些困难。
当我最初记录我的测试时,它跳过了我模拟的主页按钮按下,但它在我滑动到第一个主屏幕并导航到健康应用程序以显示数据点时正在记录。
我搜索了如何按下主页按钮,并找到了这个(有效):
XCUIDevice.shared.press(.home)
但是,它记录的其他呼叫中的 none 实际上适用于应用程序外部的导航。在主屏幕上记录的滑动代码显然看起来不对,并且当我将 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 读取的功能添加到我的应用程序中才能验证端到端测试?
您将 UI 测试限制在您的应用程序中,当您按下主页按钮并移出应用程序时 UI 您无法在您的应用程序变量中执行 UI 操作代码指向您的应用程序。
您可能有类似
的代码let app = XCUIApplication()
因此您应该修改 XCUIApplication() 行。
let app = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
现在您可以移出应用程序了。
据我所知,拥有独立的 UITestAgent 应用程序并使用 springboard bundle id 启动其 UiTestcases 是件好事,这样您就可以在该应用程序的帮助下测试任何应用程序,而不是像我编写的一些测试用例在产品 XYZ 代码库和下一个产品 ABC 中,我将在 ABC 产品的代码库中编写测试!
Xcode 9
这是使用 Xcode 9
的解决方案let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()
您可以在 this post
中找到系统应用的捆绑标识符列表Xcode 8
对于Xcode8有点复杂 为了从 Springboard 启动应用程序,您需要导入以下内容 headers
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")
}
}
}
Swift 4
let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")