Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Aug 2, 2023
1 parent ca5aeb5 commit 37b2b99
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
4 changes: 4 additions & 0 deletions xcuitest-sample-proj.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FF86799E2A696BDC00C4A1A7 /* CarouselItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF86799D2A696BDC00C4A1A7 /* CarouselItemView.swift */; };
FFED40FE2A714284005B0940 /* Sliders in Frameworks */ = {isa = PBXBuildFile; productRef = FFED40FD2A714284005B0940 /* Sliders */; };
FFEDF6402A7A7E1A0065A5E4 /* Alerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEDF63F2A7A7E1A0065A5E4 /* Alerts.swift */; };
FFEDF6422A7A965C0065A5E4 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEDF6412A7A965C0065A5E4 /* Logger.swift */; };
FFF0348F2A6CFA6500A33E97 /* FirstPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFF0348E2A6CFA6500A33E97 /* FirstPage.swift */; };
FFF034912A6CFA8B00A33E97 /* BasePage.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFF034902A6CFA8B00A33E97 /* BasePage.swift */; };
FFF034932A6CFEA200A33E97 /* SecondPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFF034922A6CFEA200A33E97 /* SecondPage.swift */; };
Expand Down Expand Up @@ -66,6 +67,7 @@
FF86799A2A69699D00C4A1A7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
FF86799D2A696BDC00C4A1A7 /* CarouselItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarouselItemView.swift; sourceTree = "<group>"; };
FFEDF63F2A7A7E1A0065A5E4 /* Alerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alerts.swift; sourceTree = "<group>"; };
FFEDF6412A7A965C0065A5E4 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
FFF0348E2A6CFA6500A33E97 /* FirstPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstPage.swift; sourceTree = "<group>"; };
FFF034902A6CFA8B00A33E97 /* BasePage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasePage.swift; sourceTree = "<group>"; };
FFF034922A6CFEA200A33E97 /* SecondPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondPage.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -191,6 +193,7 @@
FF79C2882A66F9F500BBC60A /* Interactions.swift */,
FF4D5CAF2A68033C00B52624 /* Elements.swift */,
FFEDF63F2A7A7E1A0065A5E4 /* Alerts.swift */,
FFEDF6412A7A965C0065A5E4 /* Logger.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -376,6 +379,7 @@
FFF034952A6D027B00A33E97 /* TabBar.swift in Sources */,
FF79C2892A66F9F500BBC60A /* Interactions.swift in Sources */,
FF4D5CB02A68033C00B52624 /* Elements.swift in Sources */,
FFEDF6422A7A965C0065A5E4 /* Logger.swift in Sources */,
FFF0349B2A6D227100A33E97 /* XCUIApplication+Extensions.swift in Sources */,
FFF0348F2A6CFA6500A33E97 /* FirstPage.swift in Sources */,
FF79C2862A66F72200BBC60A /* TestConstants.swift in Sources */,
Expand Down
47 changes: 26 additions & 21 deletions xcuitest-sample-projUITests/Foundations/BaseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class BaseTest: XCTestCase {
private let device = XCUIDevice.shared

override func setUp() {
// Fail-fast tests
continueAfterFailure = false

guard let testName = cleanedTestName else {
XCTFail("Could not clean current test name")
return
Expand All @@ -23,8 +26,6 @@ class BaseTest: XCTestCase {
deleteApp()
}

// Fail-fast tests
continueAfterFailure = false
// Start the AUT
app.launchArguments = launchArguments()
app.launch()
Expand Down Expand Up @@ -83,41 +84,45 @@ class BaseTest: XCTestCase {
device.press(.home)
}

func terminateApp() {
app.terminate()
XCTAssertEqual(app.state, .notRunning, "App was not successfully terminated")
}

/// This function allows to delete the app between tests - to trigger permission alerts again for example
func deleteApp() {
app.terminate()
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
springboard.swipeLeft()
Interactions.performSwipeUntil(springboard, .left, 2, until: appIcon.exists && appIcon.isHittable)
}

let appName = "SampleApp"
let icon = springboard.icons[appName]
let iconExists = Elements.waitForElement(icon, TestConstants.Timeout.short)

guard iconExists else { return }

icon.press(forDuration: TestConstants.Timeout.short)
appIcon.press(forDuration: TestConstants.Timeout.veryShort)

let editHomeScreenAlert = springboard.alerts["Edit Home Screens"]
if Elements.waitForElement(editHomeScreenAlert, 1) {
let editHomeScreenAlertButton = editHomeScreenAlert.buttons["OK"]
editHomeScreenAlertButton.tap()
Alerts.handleSystemAlert(editHomeScreenAlert, "OK")
}

let deleteAppButton = springboard.icons[appName].buttons["DeleteButton"]
deleteAppButton.tap()

let additional_confirmation = springboard.alerts["Remove “\(appName)”?"].buttons["Delete App"]
additional_confirmation.tap()
if Elements.waitForElement(deleteAppButton, 5) {
deleteAppButton.tap()
} else {
XCTFail("Delete application button did not appear in 5 seconds")
}

let deleteConfirmation = springboard.alerts["Delete\(appName)”?"].buttons["Delete"]
deleteConfirmation.tap()
Alerts.handleSystemAlert("Remove\(appName)”?", "Delete App")
Alerts.handleSystemAlert("Delete “\(appName)”?", "Delete")
pressHomeButton()
}

Expand Down
13 changes: 8 additions & 5 deletions xcuitest-sample-projUITests/Helpers/Alerts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ class Alerts {

/// Handle system alert by text displayed in title or description and also specific button label
static func handleSystemAlert(_ text: String, _ button: String) {
let alert = BaseTest().getSpringboardApp().alerts.firstMatch
if alert.staticTexts.element(withLabelContaining: text).exists {
handleSystemAlert(alert, button)
return
let alerts = BaseTest().getSpringboardApp().alerts
for i in 0...alerts.count {
let currentAlert = alerts.element(boundBy: i)
if currentAlert.staticTexts.element(withLabelContaining: text).exists {
handleSystemAlert(currentAlert, button)
return
}
}
XCTFail("There was no alert found that contains text: \(text)")
}
Expand All @@ -79,6 +82,6 @@ class Alerts {
XCTAssertTrue(alertButton.exists, "No button with identifier: `\(button)` was found in the presented alert")
}
alertButton.tap()
Elements.waitUntilElementDisappears(alert, 2)
Elements.waitUntilElementDisappears(alertButton, 2)
}
}
14 changes: 14 additions & 0 deletions xcuitest-sample-projUITests/Helpers/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Logger.swift
// xcuitest-sample-projUITests
//
// Created by Nikola Shabanov on 2.08.23.
//

import Foundation

class Logger{
static func log(_ mlog: String){
NSLog("[DEBUG] " + mlog)
}
}

0 comments on commit 37b2b99

Please sign in to comment.