Skip to content

Commit

Permalink
small rework and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Aug 6, 2023
1 parent 2d27aa0 commit f7d5434
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 49 deletions.
46 changes: 0 additions & 46 deletions xcuitest-sample-projUITests/Foundations/BaseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions xcuitest-sample-projUITests/Helpers/Alerts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
92 changes: 92 additions & 0 deletions xcuitest-sample-projUITests/Helpers/App.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
5 changes: 5 additions & 0 deletions xcuitest-sample-projUITests/Helpers/Interactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions xcuitest-sample-projUITests/Props/TestConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions xcuitest-sample-projUITests/SampleAppUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit f7d5434

Please sign in to comment.