ios - 如何在 iOS Xcode UI 测试用例中启动系统应用程序

标签 ios xcode healthkit xcode-ui-testing

我有一个应用程序,其主要目的是将数据输入 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/

相关文章:

ios - Health 处理多步骤源的方式与 HealthKit 不同——swift

ios:当应用程序处于后台或挂起状态时下载大文件

c++ - xcode 关键字语法着色,不识别 c++ std::string

ios - 我应该怎么做才能使 iOS 中所有 UI 控件的大小通用?

iOS 近乎实时地从 Apple Watch 获取心率

ios - Realm AddOrUpdate 一个带有对象列表的对象覆盖后面的对象

ios - MPVolumeView AirPlay 按钮未显示

iphone - 当我在 Cocoa Touch 中加载一个新 View 时,它会将所有内容向上移动大约 20 像素

ios - 无法下载 App + App 不再可用

ios - 有没有办法在 Swift 中从 HealthKit 中提取用户的全名?