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

Dov*_*Dov 10 xcode ios healthkit xcode-ui-testing

我有一个应用程序,其主要目的是将数据输入HealthKit.我想写一些Xcode UI测试来验证它是否成功编写了这些数据,但是我在Health应用程序中验证数据时遇到了一些困难.

当我最初录制我的测试时,它跳过我的模拟主页按钮按下,但它正在录制,当我滑到第一个主屏幕并导航到Health应用程序以显示数据点.

我搜索了如何按Home键,发现这个(有效):

XCUIDevice.shared.press(.home)
Run Code Online (Sandbox Code Playgroud)

但是,它记录的其他调用实际上都不适用于应用程序之外的导航.在主屏幕上滑动的录制代码显然看起来不对,当我tap()用a 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()
Run Code Online (Sandbox Code Playgroud)

接下来几行,用于在主屏幕上启动应用程序,甚至不适用于当前可见页面上的应用程序图标:

let elementsQuery = app.scrollViews.otherElements
elementsQuery.icons["Health"].tap()
Run Code Online (Sandbox Code Playgroud)

有没有办法实现我正在尝试做的事情,或者我需要等待验证端到端测试,直到我添加从HealthKit读取到我的应用程序的能力?

Tit*_*eul 9

Xcode 9

这是使用Xcode 9的解决方案

let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()
Run Code Online (Sandbox Code Playgroud)

您可以在此帖子中找到系统应用程序的软件包标识符列表

Xcode 8

对于Xcode 8来说,它有点复杂为了从Springboard启动应用程序,您需要导入以下标题

https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIElement.h https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIApplication.h

然后使用以下内容(例如使用Health)

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
Run Code Online (Sandbox Code Playgroud)

迅速

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")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)