Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 929
Dov Avatar asked Jan 15 '16 21:01

Dov


People also ask

How do you write UI test cases in Swift?

To add it in an existing project go to the build folder and on the bottom left you will see the plus button. Selecting that will give you a dropdown menu, where you can then select “UI Testing Bundle”. Then go to the UITests. swift file in you file hierarchy.


2 Answers

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")
        }
    }
}
like image 103
Titouan de Bailleul Avatar answered Sep 18 '22 20:09

Titouan de Bailleul


Swift 4

let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
like image 25
ScottyBlades Avatar answered Sep 17 '22 20:09

ScottyBlades