From 16bbd962beefcb880e45157f232260fee66c65a3 Mon Sep 17 00:00:00 2001 From: shamanec Date: Thu, 10 Aug 2023 16:19:56 +0300 Subject: [PATCH] cleanup and comment improvements --- xcuitest-sample-projUITests/Helpers/App.swift | 4 +- .../Helpers/Elements.swift | 46 +++++++++---------- .../Helpers/Interactions.swift | 8 ++-- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/xcuitest-sample-projUITests/Helpers/App.swift b/xcuitest-sample-projUITests/Helpers/App.swift index 53fe1ad..16c0a76 100644 --- a/xcuitest-sample-projUITests/Helpers/App.swift +++ b/xcuitest-sample-projUITests/Helpers/App.swift @@ -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") } @@ -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() diff --git a/xcuitest-sample-projUITests/Helpers/Elements.swift b/xcuitest-sample-projUITests/Helpers/Elements.swift index b42a506..7d0f8eb 100644 --- a/xcuitest-sample-projUITests/Helpers/Elements.swift +++ b/xcuitest-sample-projUITests/Helpers/Elements.swift @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 @@ -135,7 +135,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 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 @@ -143,13 +143,13 @@ class Elements { 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") } } diff --git a/xcuitest-sample-projUITests/Helpers/Interactions.swift b/xcuitest-sample-projUITests/Helpers/Interactions.swift index c7d1370..9783176 100644 --- a/xcuitest-sample-projUITests/Helpers/Interactions.swift +++ b/xcuitest-sample-projUITests/Helpers/Interactions.swift @@ -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) @@ -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** /// ``` @@ -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:** /// ```