How to launch system apps in an iOS Xcode UI test case
Asked Answered
M

3

11

I've got an app whose main purpose is to enter data into HealthKit. I'd like to write some Xcode UI tests to verify that it's writing this data successfully, but I'm having some difficulty verifying the data in the Health app.

When I initially recorded my test, it skipped my simulated Home button press, but it was recording as I swiped over to the first home screen and navigated into the Health app to show the data points.

I searched for how to press the Home button, and found this (which works):

XCUIDevice.shared.press(.home)

However, none of the other calls it recorded actually work for navigation outside of the app. The recorded code for swiping on the home screen obviously looks wrong, and also doesn't work when I replace tap() with a swipeRight() or swipeLeft():

app.childrenMatchingType(.Window).elementBoundByIndex(1).childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).elementBoundByIndex(0).childrenMatchingType(.ScrollView).element.tap()

The next couple of lines, for launching an app on the home screen, don't even work for an app icon that's on the currently visible page:

let elementsQuery = app.scrollViews.otherElements
elementsQuery.icons["Health"].tap()

Is there any way to achieve what I'm trying to do, or will I need to wait to verify end-to-end testing until I add the ability to read from HealthKit to my app?

Monamonachal answered 15/1, 2016 at 21:33 Comment(0)
C
11

Xcode 9

Here's the solution using Xcode 9

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

You can find a list of bundle identifier for the system apps in this post

Xcode 8

For Xcode 8 it's a little bit more complicated In order to launch an application from the Springboard you need to import the following headers

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

Then use the following (for example with 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

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")
        }
    }
}
Connor answered 14/4, 2017 at 15:37 Comment(0)
N
3

Swift 4

let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
Namedropper answered 30/10, 2017 at 8:9 Comment(0)
J
0

you bounded your UI tests with your application and the moment you press home button and move out of the application UI you can not perform UI operations as app variable in your code is pointing to your application.

you may be having code like

let app = XCUIApplication()

So you should modify that XCUIApplication() line.

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

now you can move out of application.

According to my knowledge it is good to have and independent UITestAgent app and initiate its UiTestcases with springboard bundle id so you can test any application with the help of that app not like i coded some test case inside product XYZ code base and for next product ABC I will write tests inside ABC product's code base!

Jerz answered 12/4, 2017 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.