How to Reset the App Between Tests in Swift Xctest Ui

How to delete/reset an app from iOS 13 with XCTest?

Try to press the app icon a little longer than in previous iOS versions.

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

func deleteMyApp() {
XCUIApplication().terminate()

let icon = springboard.icons["YourAppName"]
if icon.exists {
let iconFrame = icon.frame
let springboardFrame = springboard.frame
icon.press(forDuration: 5)

// Tap the little "X" button at approximately where it is. The X is not exposed directly
springboard.coordinate(withNormalizedOffset: CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX, dy: (iconFrame.minY + 3) / springboardFrame.maxY)).tap()

springboard.alerts.buttons["Delete"].tap()
}
}

How do I reset the application data after each test with Xcode 7 UI Testing?

Not in a straight forward manner. But there are some workarounds.

The XCUIApplication can set command line arguments and environment variables that can alter your application’s behavior.

A simple example of your main.m file:

int main(int argc, char * argv[]) {
#if DEBUG
// Reset all data for UI Testing
@autoreleasepool {
for (int i = 1; i < argc; ++i) {
if (0 == strcmp("--reset-container", argv[i])) {
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSFileManager *fm = [[NSFileManager alloc] init];
for (NSString *path in folders) {
[fm removeItemAtPath:path error:nil];
}
// Also remove documents folder if necessary...
}
}
}
#endif
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}

And in -[XCTestCase setUp] add:

XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchArguments = @[@"--reset-container"];
[app launch];

I can't delete my application during the teardown phase of my tests

You can reset application's permissions with Xcode 11.4 API
https://developer.apple.com/documentation/xctest/xcuiapplication/3526066-resetauthorizationstatus


If you still want to delete the app on iOS 13, check my answer here
How to delete/reset an app from iOS 13 with XCTest?

How do I keep the app open between UITests in Xcode

In your setUp() method, remove [[[XCUIApplication alloc] init] launch]; and put it into the first test you will be performing.

For eg.,

If you have tests: testUI(), testUIPart2(), testUIPart3(), etc., and it runs in this order, put [[[XCUIApplication alloc] init] launch]; into the first line of testUI() and no where else.

XCTest UI Testing - How to close and open an app without relaunch?

As of Xcode 9 and iOS 11, XCUIApplication() has an activate() method that you can use to relaunch the app.

As brandenbyers suggested, you can "press" the home button to background your app, and then activate it again like this to avoid using Siri:

XCUIDevice.shared.press(.home)
XCUIApplication().activate()

Note that this only works with targets built using XCUITest, not XCTest. If you try this within a target built from XCTest, all XCUIApplication operations will crash.



Related Topics



Leave a reply



Submit