Skip to content

Instantly share code, notes, and snippets.

@asam139
Forked from KoCMoHaBTa/- UITesting Utilities.md
Last active July 13, 2018 12:38
Show Gist options
  • Save asam139/7d772955958bab73ebdb03c092fd969f to your computer and use it in GitHub Desktop.
Save asam139/7d772955958bab73ebdb03c092fd969f to your computer and use it in GitHub Desktop.
UITesting Utilities
//
// Springboard.swift
// PriorityMatrixUITests
//
// Created by Saul Moreno Abril on 09/07/2018.
//
import Foundation
import XCTest
class Springboard {
static let shared = Springboard()
let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")!
let settings = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.Preferences")!
func deleteApp(withName name: String) {
XCUIApplication().terminate()
let springboard = self.springboard
springboard.resolve()
let predicate = NSPredicate(format: "label = %@", name);
var count = springboard.icons.matching(predicate).count;
while count > 0 {
let icon = springboard.icons.matching(predicate).firstMatch
if !icon.exists || !icon.isHittable {
break;
}
let iconFrame = icon.frame
let springboardFrame = springboard.frame
//Tap & Hold the app icon in order to go to edit mode
Thread.sleep(forTimeInterval: 0.5)
icon.press(forDuration: 2.0)
//Tap the little "X" button at approximately where it is. The X is not exposed directly
Thread.sleep(forTimeInterval: 0.5)
let vector = CGVector(dx: (iconFrame.minX + 5) / springboardFrame.maxX, dy: (iconFrame.minY + 5) / springboardFrame.maxY)
springboard.coordinate(withNormalizedOffset: vector).tap()
//tap the delete alert button if it appears (optional)
Thread.sleep(forTimeInterval: 0.5)
let deleteButton = springboard.alerts.buttons["Delete"];
if (deleteButton.exists) {
deleteButton.tap();
}
//Press home once make the icons stop wiggling
Thread.sleep(forTimeInterval: 0.5)
XCUIDevice.shared.press(.home);
Thread.sleep(forTimeInterval: 0.5)
count = springboard.icons.matching(predicate).count;
}
}
func resetLocationAndPrivacySettings() {
XCUIApplication().terminate()
let springboard = self.springboard
springboard.resolve()
let settingsIcon = springboard.icons["Settings"]
if settingsIcon.exists {
//Press home button twise slowly in order to go to the first page of the springboard
Thread.sleep(forTimeInterval: 0.5)
XCUIDevice.shared.press(.home);
//Press home button twise slowly in order to go to the first page of the springboard
Thread.sleep(forTimeInterval: 0.5)
XCUIDevice.shared.press(.home);
//tap the settings icon
Thread.sleep(forTimeInterval: 0.5)
settingsIcon.tap()
let settings = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.Preferences")!
//go to `General` -> `Reset` and tap `Reset Location & Privacy`
settings.tables.staticTexts["General"].tap()
settings.tables.staticTexts["Reset"].tap()
settings.tables.staticTexts["Reset Location & Privacy"].tap()
//tap the `Reset Warnings` button
Thread.sleep(forTimeInterval: 0.5)
settings.buttons["Reset Warnings"].tap()
settings.terminate()
}
}
}

UITesing Utilities

Contents

How to use

  • add the contents to your UITesting target
  • import XCTest-Private.h into your UITesting target bridging header
  • in your XCTestCase tearDown() method use the Springboard utility to delete the app and/or reset Location and Privacy Settings

Example tearDown()

  override func tearDown() {
    
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    Springboard.shared.deleteApp(withName: "YourAppName")
    Springboard.shared.resetLocationAndPrivacySettings()
    
    super.tearDown()
  }

References

http://stackoverflow.com/questions/33107731/is-there-a-way-to-reset-the-app-between-tests-in-swift-xctest-ui-in-xcode-7

Bugs fixed

  • Compatibility with iPad when the app icon appears as quick access in the dock
//
// XCTest-Private.h
// https://gist.github.com/KoCMoHaBTa/5d2cecfc17db5f3944bc98bcd6fcde55
//
// Created by Milen Halachev on 2/15/17.
// Copyright © 2017 Milen Halachev. All rights reserved.
//
#ifndef XCTest_Private_h
#define XCTest_Private_h
#import <XCTest/XCUIApplication.h>
@interface XCUIApplication (Private)
- (id)initPrivateWithPath:(id)arg1 bundleID:(id)arg2;
@end
@interface XCUIElement (Private)
- (void)resolve;
@end
#endif /* XCTest_Private_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment