From f7d5434de6602ad2eec14cb0f76f4c493f417196 Mon Sep 17 00:00:00 2001 From: shamanec Date: Sun, 6 Aug 2023 16:08:34 +0300 Subject: [PATCH] small rework and cleanup --- .../Foundations/BaseTest.swift | 46 ---------- .../Helpers/Alerts.swift | 6 +- xcuitest-sample-projUITests/Helpers/App.swift | 92 +++++++++++++++++++ .../Helpers/Interactions.swift | 5 + .../Props/TestConstants.swift | 1 + .../SampleAppUITests.swift | 12 +++ 6 files changed, 113 insertions(+), 49 deletions(-) create mode 100644 xcuitest-sample-projUITests/Helpers/App.swift diff --git a/xcuitest-sample-projUITests/Foundations/BaseTest.swift b/xcuitest-sample-projUITests/Foundations/BaseTest.swift index e8154c8..ad824aa 100644 --- a/xcuitest-sample-projUITests/Foundations/BaseTest.swift +++ b/xcuitest-sample-projUITests/Foundations/BaseTest.swift @@ -111,52 +111,6 @@ class BaseTest: XCTestCase { handleAlert(title: "", button: button) } - func pressHomeButton() { - device.press(.home) - } - - func terminateApp() { - app.terminate() - XCTAssert(app.wait(for: .notRunning, timeout: 2), "App was not successfully terminated") - } - - /// This function allows to delete the app between tests - to trigger permission alerts again for example - func deleteApp() { - let appName = "SampleApp" - Logger.log("Deleting application '\(appName)'") - let appIcon = springboard.icons[appName] - - // Attempt killing the app just in case - terminateApp() - // Go to the initial springboard screen - pressHomeButton() - - // Wait for the Settings app icon to appear before swiping to search for the AUT - let settingsIcon = springboard.icons["Settings"] - if Elements.waitForElement(settingsIcon, 2) { - // Sleep for half a second after finding the settings button because sometimes it swipes before finishing transition to initial springboard screen - usleep(500_000) // 0.5 seconds - Interactions.performSwipeUntil(springboard, .left, 2, until: appIcon.exists && appIcon.isHittable) - } - appIcon.press(forDuration: TestConstants.Timeout.veryShort) - - let editHomeScreenAlert = springboard.alerts["Edit Home Screens"] - if Elements.waitForElement(editHomeScreenAlert, 1) { - Alerts.handleSystemAlert(editHomeScreenAlert, "OK") - } - - let deleteAppButton = springboard.icons[appName].buttons["DeleteButton"] - if Elements.waitForElement(deleteAppButton, 5) { - deleteAppButton.tap() - } else { - XCTFail("Delete application button did not appear in 5 seconds") - } - - Alerts.handleSystemAlert("Remove “\(appName)”?", "Delete App") - Alerts.handleSystemAlert("Delete “\(appName)”?", "Delete") - pressHomeButton() - } - /// private var cleanedTestName: String? { let testName = self.name diff --git a/xcuitest-sample-projUITests/Helpers/Alerts.swift b/xcuitest-sample-projUITests/Helpers/Alerts.swift index ccce789..3f0e1c5 100644 --- a/xcuitest-sample-projUITests/Helpers/Alerts.swift +++ b/xcuitest-sample-projUITests/Helpers/Alerts.swift @@ -45,7 +45,7 @@ class Alerts { XCTAssertTrue(alertButton.exists, "No button with identifier: `\(button)` was found in the presented alert") } alertButton.tap() - Elements.waitUntilElementDisappears(alert, 2) + Elements.waitUntilElementDisappears(alert, TestConstants.Timeout.veryShort) } // MARK: - System alerts handling @@ -75,7 +75,7 @@ class Alerts { /// Handle system alert by element and targetting specific button static func handleSystemAlert(_ alert: XCUIElement, _ button: String) { - Elements.waitForElementExistence(alert, 5) + Elements.waitForElementExistence(alert, TestConstants.Timeout.short) var alertButton: XCUIElement if button == "" { alertButton = alert.buttons.firstMatch @@ -85,6 +85,6 @@ class Alerts { XCTAssertTrue(alertButton.exists, "No button with identifier: `\(button)` was found in the presented alert") } alertButton.tap() - Elements.waitUntilElementDisappears(alertButton, 2) + Elements.waitUntilElementDisappears(alertButton, TestConstants.Timeout.veryShort) } } diff --git a/xcuitest-sample-projUITests/Helpers/App.swift b/xcuitest-sample-projUITests/Helpers/App.swift new file mode 100644 index 0000000..53fe1ad --- /dev/null +++ b/xcuitest-sample-projUITests/Helpers/App.swift @@ -0,0 +1,92 @@ +// +// App.swift +// xcuitest-sample-projUITests +// +// Created by Nikola Shabanov on 3.08.23. +// + +import XCTest + +class App { + private static let app = BaseTest().getApp() + private static let springboard = BaseTest().getSpringboardApp() + + /// Put the AUT in the background + static func background() { + Logger.log("Putting application in the background") + Interactions.pressHomeButton() + // This assert did not work, probably I haven't coded the sample app properly + // But it should work + //XCTAssert(app.wait(for: .runningBackground, timeout: TestConstants.Timeout.medium), "App was not put in the background") + } + + /// Put the AUT in the foreground + static func foreground() { + Logger.log("Putting application to the foreground") + app.activate() + XCTAssert(app.wait(for: .runningForeground, timeout: TestConstants.Timeout.short), "App was not put in foreground") + } + + /// Terminate the AUT + static func terminate() { + Logger.log("Terminating application") + app.terminate() + XCTAssert(app.wait(for: .notRunning, timeout: TestConstants.Timeout.short), "App was not successfully terminated") + } + + /// Launch the AUT + static func launch() { + Logger.log("Launching application") + app.launch() + XCTAssert(app.wait(for: .runningForeground, timeout: TestConstants.Timeout.short), "App was not launched") + } + + /// Restart the AUT + static func restart() { + Logger.log("Restarting application") + terminate() + launch() + } + + /// Delete and restart the AUT + static func deleteAndRestart() { + Logger.log("Deleting and relaunching application") + delete() + launch() + } + + static func delete(_ appName: String = "SampleApp") { + Logger.log("Deleting application '\(appName)'") + let appIcon = springboard.icons[appName] + + // Attempt killing the app just in case + terminate() + // Go to the initial springboard screen + Interactions.pressHomeButton() + + // Wait for the Settings app icon to appear before swiping to search for the AUT + let settingsIcon = springboard.icons["Settings"] + if Elements.waitForElement(settingsIcon, 2) { + // Sleep for half a second after finding the settings button because sometimes it swipes before finishing transition to initial springboard screen + usleep(500_000) // 0.5 seconds + Interactions.performSwipeUntil(springboard, .left, 2, until: appIcon.exists && appIcon.isHittable) + } + appIcon.press(forDuration: TestConstants.Timeout.veryShort) + + let editHomeScreenAlert = springboard.alerts["Edit Home Screens"] + if Elements.waitForElement(editHomeScreenAlert, TestConstants.Timeout.second) { + Alerts.handleSystemAlert(editHomeScreenAlert, "OK") + } + + let deleteAppButton = springboard.icons[appName].buttons["DeleteButton"] + if Elements.waitForElement(deleteAppButton, TestConstants.Timeout.short) { + deleteAppButton.tap() + } else { + XCTFail("Delete application button did not appear in 5 seconds") + } + + Alerts.handleSystemAlert("Remove “\(appName)”?", "Delete App") + Alerts.handleSystemAlert("Delete “\(appName)”?", "Delete") + Interactions.pressHomeButton() + } +} diff --git a/xcuitest-sample-projUITests/Helpers/Interactions.swift b/xcuitest-sample-projUITests/Helpers/Interactions.swift index f2b6b20..c7d1370 100644 --- a/xcuitest-sample-projUITests/Helpers/Interactions.swift +++ b/xcuitest-sample-projUITests/Helpers/Interactions.swift @@ -8,6 +8,11 @@ import XCTest class Interactions { + /// Press the device home button + static func pressHomeButton() { + XCUIDevice.shared.press(.home) + } + /// Performs drag and drop actions on non-hittable elements /// /// - Parameters: diff --git a/xcuitest-sample-projUITests/Props/TestConstants.swift b/xcuitest-sample-projUITests/Props/TestConstants.swift index afb8808..35c34b9 100644 --- a/xcuitest-sample-projUITests/Props/TestConstants.swift +++ b/xcuitest-sample-projUITests/Props/TestConstants.swift @@ -21,6 +21,7 @@ struct TestConstants { - longExtran = 50.0 */ enum Timeout { + static let second = 1.0 static let veryShort = 3.0 static let short = 5.0 static let medium = 10.0 diff --git a/xcuitest-sample-projUITests/SampleAppUITests.swift b/xcuitest-sample-projUITests/SampleAppUITests.swift index 4567a9f..5f51c85 100644 --- a/xcuitest-sample-projUITests/SampleAppUITests.swift +++ b/xcuitest-sample-projUITests/SampleAppUITests.swift @@ -156,4 +156,16 @@ final class SampleAppUITests: BaseTest { getApp().swipeUp() print(firstPage.triggerAlertButton.isFullyVisible()) } + + func testAppActions() { + App.background() + sleep(1) + App.foreground() + sleep(1) + App.terminate() + sleep(1) + App.restart() + sleep(1) + App.deleteAndRestart() + } }