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

问题描述 投票:10回答:3

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

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

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

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读取到我的应用程序的能力?

ios xcode health-kit xcode-ui-testing
3个回答
9
投票

Xcode 9

这是使用Xcode 9的解决方案

let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()

您可以在this post中找到系统应用程序的包标识符列表

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

迅速

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")
        }
    }
}

2
投票

斯威夫特4

let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")

0
投票

您使用应用程序限制了UI测试,当您按下主页按钮并离开应用程序UI时,您无法执行UI操作,因为代码中的app变量指向您的应用程序。

你可能有像这样的代码

let app = XCUIApplication()

所以你应该修改那条XCUIApplication()行。

let app = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")

现在你可以退出申请。

根据我的知识,有一个独立的UITestAgent应用程序并启动其带有跳板软件包ID的UiTestcases是很好的,所以你可以在该应用程序的帮助下测试任何应用程序,就像我在产品XYZ代码库中编写一些测试用例和下一个产品ABC我将在ABC产品的代码库中编写测试!

© www.soinside.com 2019 - 2024. All rights reserved.