Skip to content

Instantly share code, notes, and snippets.

override func setUpWithError() throws {
try super.setUpWithError()
continueAfterFailure = false
self.app = XCUIApplication()
let path = Bundle.allBundles.compactMap { $0.path(forResource: "state", ofType: "json") }.first!
self.app.launchArguments = [
"-initialScreen", "home_screen",
"-state", path,
"-reset", "true"
]
func testStartingFromHomeScreen() {
self.app.launch()
let homeView = self.app.otherElements[HomeView.AccessibilityIdentifiers.homeView.rawValue]
XCTAssertTrue(homeView.exists)
XCTAssertEqual(self.app.staticTexts[HomeView.AccessibilityIdentifiers.name.rawValue].label, "Riccardo")
XCTAssertEqual(self.app.staticTexts[HomeView.AccessibilityIdentifiers.surname.rawValue].label, "Cipolleschi")
XCTAssertEqual(self.app.staticTexts[HomeView.AccessibilityIdentifiers.age.rawValue].label, "32")
XCTAssertEqual(self.app.staticTexts[HomeView.AccessibilityIdentifiers.coins.rawValue].label, "0")
XCTAssertTrue(self.app.buttons[HomeView.AccessibilityIdentifiers.purchase.rawValue].exists)
// Entry point of the app
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let scene = (scene as? UIWindowScene) else { return }
func extractState() -> Models.User? {
guard
let state = UserDefaults.standard.string(forKey: "state"),
let fileContent = try? String(contentsOfFile: state),
let fileData = fileContent.data(using: .utf8)
else {
return nil
}
return try? JSONDecoder().decode(Models.User.self, from: fileData)
let path = Bundle
.allBundles
.compactMap { $0.path(forResource: "state", ofType: "json") }
.first!
{
"name": "Riccardo",
"surname": "Cipolleschi",
"age": 32,
"coins": 0
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
// ...
// create the view controller we need
class HomeTests: XCTestCase {
var app: XCUIApplication!
override func setUpWithError() throws {
try super.setUpWithError()
continueAfterFailure = false
self.app = XCUIApplication()
// We are passing the arguments here.
class LegalView: UIView {
func setup() {
// add elements to the view
#if UITESTING
self.setupAccessibilityIdentifiers()
#endif
// ...
}
}
class LegalTests: XCTestCase {
var app: XCUIApplication!
var tosButton: XCUIElement!
var privacyButton: XCUIElement!
var continueButton: XCUIElement!
var homeScreen: XCUIElement?
override func setUpWithError() throws {
try super.setUpWithError()