How to run system applications in an IOS Xcode UI test case

I have an application whose main goal is to enter data into HealthKit. I would like to write some tests of the Xcode interface to verify that it successfully writes this data, but I find it difficult to verify the data in the Health application.

When I originally recorded my test, it skipped my click on the “Modified Home” button, but it was recorded when I moved to the first home screen and went to the Health app to display data points.

I searched how to click the Home button and found this (which works):

XCUIDevice.sharedDevice().pressButton(.Home) 

However, none of the other calls he recorded actually worked for navigation outside the application. The recorded code for scrolling on the main screen clearly looks wrong, and also does not work when I replace tap() with 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 following two lines, to launch the application on the main screen, do not even work for the application icon on the currently visible page:

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

Is there a way to achieve what I'm trying to do, or do I need to wait to check end-to-end testing until I add the ability to read from HealthKit to my application?

+6
source share
3 answers

Xcode 9

Here is the solution using Xcode 9

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

You can find a list of package identifiers for system applications in this post.

Xcode 8

For Xcode 8, it's a little trickier. To run the application with 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") } } } 
+3
source

Swift 4

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

you have limited your user interface tests to your application, and once you press the home button and exit the application user interface, you will not be able to perform user interface operations because the application variable in your code points to your application.

you may have a code like

 let app = XCUIApplication() 

So you should change this line to XCUIApplication ().

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

Now you can exit the application.

To my knowledge, it’s good to have an independent UITestAgent application and initiate its UiTestcases with a pattern identifier so that you can test any application using this application, not as I encoded some test case inside the XYZ code base of the product and for the next ABC product I will write tests inside the ABC product code base!

0
source

All Articles