Skip to content

Commit

Permalink
cleanup and comment improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec committed Aug 10, 2023
1 parent e3ef0fa commit 16bbd96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
4 changes: 1 addition & 3 deletions xcuitest-sample-projUITests/Helpers/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class App {
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
// But theoretically should work
//XCTAssert(app.wait(for: .runningBackground, timeout: TestConstants.Timeout.medium), "App was not put in the background")
}

Expand Down Expand Up @@ -59,8 +59,6 @@ class App {
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()

Expand Down
46 changes: 23 additions & 23 deletions xcuitest-sample-projUITests/Helpers/Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ class Elements {

// MARK: - Wait functions

/// Wait for element to exist with XCUITest supplied `waitForExistence()`
static func waitForElement(_ element: XCUIElement, _ timeoutValue: Double) -> Bool {
return element.waitForExistence(timeout: timeoutValue)
}

/// Wait until an XCUIElementQuery has at least X number of elements - polls each 300ms for the supplied timeout duration
///
/// - Parameters:
Expand All @@ -77,9 +72,14 @@ class Elements {
XCTAssertTrue(result, "XCUIElementQuery was not filled with \(elementsCount) elements in \(timeoutValue) seconds")
}

// Can be faster than other approaches because XCUITest supplied `waitForExistence()` and `XCTWaiter` first wait a second and then perform a check
/// Wait for element to exist polling for the supplied timeout duration.
static func waitForElementExistence(_ element: XCUIElement, _ timeoutValue: Double, _ exists: Bool, _ pollInterval: UInt32 = 200_000) {
/// Wait for element to exist with XCUITest supplied `waitForExistence()`
static func waitForElement(_ element: XCUIElement, _ timeoutValue: Double = TestConstants.Timeout.medium) -> Bool {
return element.waitForExistence(timeout: timeoutValue)
}

// Can be faster than other approaches because XCUITest supplied `waitForExistence()` and `XCTWaiter` first wait a second and then perform a check, based on my testing, not that I've found that information somewhere
/// Custom wait for element to exist polling for the supplied timeout duration.
static func waitForElementExistence(_ element: XCUIElement, _ timeoutValue: Double = TestConstants.Timeout.medium, _ exists: Bool = true, _ pollInterval: UInt32 = 200_000) {
var result = false
let startTime = Date().timeIntervalSince1970

Expand All @@ -93,13 +93,22 @@ class Elements {
XCTAssertTrue(result, "Element \(exists ? "does not exist" : "exists") after \(timeoutValue) seconds")
}

/// Wait for element to exist
static func waitForElementExistenceAlt(_ element: XCUIElement, _ timeoutValue: Double, _ existence: Bool) {
let predicate = "exists == \(String(existence))"
let existsPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: existsPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertTrue(result == .completed, "Element does not exist after \(timeoutValue) seconds")
}

/// Wait for element to become hittable
static func waitForElementHittable(_ element: XCUIElement, _ timeoutValue: Double, _ hittable: Bool) {
let predicate = "hittable == \(String(hittable))"
let isDisplayedPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: isDisplayedPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element is hittable after \(timeoutValue) seconds or does not exist")
XCTAssertTrue(result == .completed, "Element is hittable after \(timeoutValue) seconds or does not exist")
}

/// Wait for element to become enabled
Expand All @@ -108,16 +117,7 @@ class Elements {
let isEnabledPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: isEnabledPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element is not enabled after \(timeoutValue) seconds or does not exist")
}

/// Wait for element to exist
static func waitForElementExistenceAlt(_ element: XCUIElement, _ timeoutValue: Double, _ existence: Bool) {
let predicate = "exists == \(String(existence))"
let existsPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: existsPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element does not exist after \(timeoutValue) seconds")
XCTAssertTrue(result == .completed, "Element is not enabled after \(timeoutValue) seconds or does not exist")
}

/// Wait for element label to equal exact string
Expand All @@ -126,7 +126,7 @@ class Elements {
let labelEqualsPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: labelEqualsPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element label value is not '\(label)' after \(timeoutValue) seconds or does not exist")
XCTAssertTrue(result == .completed, "Element label value is not '\(label)' after \(timeoutValue) seconds or does not exist")
}

/// Wait for element label to contain a string
Expand All @@ -135,21 +135,21 @@ class Elements {
let labelEqualsPredicate = NSPredicate(format: predicate)
let expectation = [XCTNSPredicateExpectation(predicate: labelEqualsPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element label value does not contain '\(label)' after \(timeoutValue) seconds or does not exist")
XCTAssertTrue(result == .completed, "Element label value does not contain '\(label)' after \(timeoutValue) seconds or does not exist")
}

/// Wait for element value to contain a specific string
static func waitForElementValueContains(_ element: XCUIElement, _ timeoutValue: Double, _ value: String) {
let valueContainsPredicate = NSPredicate(format: "value CONTAINS[c] %@", value)
let expectation = [XCTNSPredicateExpectation(predicate: valueContainsPredicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element label value does not contain '\(value)' after \(timeoutValue) seconds or does not exist")
XCTAssertTrue(result == .completed, "Element label value does not contain '\(value)' after \(timeoutValue) seconds or does not exist")
}

/// Wait for element to fulfill any provided predicate condition as string
static func waitForElementPredicate(_ element: XCUIElement, _ timeoutValue: Double, _ predicate: NSPredicate) {
let expectation = [XCTNSPredicateExpectation(predicate: predicate, object: element)]
let result = XCTWaiter().wait(for: expectation, timeout: timeoutValue)
XCTAssertEqual(result, .completed, "Element did not fulfil `\(predicate)` condition after \(timeoutValue) seconds")
XCTAssertTrue(result == .completed, "Element did not fulfil `\(predicate)` condition after \(timeoutValue) seconds")
}
}
8 changes: 4 additions & 4 deletions xcuitest-sample-projUITests/Helpers/Interactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Interactions {
XCUIDevice.shared.press(.home)
}

/// Performs drag and drop actions on non-hittable elements
/// Performs drag and drop action relative to two elements
///
/// - Parameters:
/// - firstElement: The element that will be dragged
/// - secondElement: The element which will be used as an end coordinate to drag the first element to
/// - pressDuration: How long to press the element to activate the drag and drop functionality before moving it
static func dragAndDrop(_ firstElement: XCUIElement, _ secondElement: XCUIElement, _ pressDuration: TimeInterval) {
static func dragAndDrop(_ firstElement: XCUIElement, _ secondElement: XCUIElement, _ pressDuration: TimeInterval = 0.5) {
let startCoordinate = firstElement.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
let endCoordinate = secondElement.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
startCoordinate.press(forDuration: pressDuration, thenDragTo: endCoordinate)
Expand Down Expand Up @@ -84,7 +84,7 @@ class Interactions {
}
}

/// Swipe until a condition is met, allows to swipe in a specific element
/// Gently swipe until a condition is met, allows to swipe in a specific element
///
/// **Examples**
/// ```
Expand All @@ -110,7 +110,7 @@ class Interactions {
XCTAssertTrue(success, "Condition was not satisfied swiping \(maxNumberOfSwipes) times in \(self) with swipe adjustment \(swipeAdjustment)")
}

/// Swipes in app or container until a condition is met
/// Swipes in app/element until a condition is met
///
/// **Examples:**
/// ```
Expand Down

0 comments on commit 16bbd96

Please sign in to comment.