-
-
Save Jwood97/e1d9b66b02855f428340bb813ae988e4 to your computer and use it in GitHub Desktop.
Helper functions examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension XCUIElement { | |
@discardableResult | |
func waitForExistence(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { self.exists }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for existence") | |
} | |
@discardableResult | |
func waitForNonExistence(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { !self.exists }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for non-existence") | |
} | |
@discardableResult | |
func waitForHittable(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { self.exists && self.isHittable }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for hittability") | |
} | |
@discardableResult | |
func waitForVisible(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { self.exists && self.isVisible }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for visibility") | |
} | |
@discardableResult | |
func waitForNotVisible(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { !self.isVisible }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for invisibility") | |
} | |
@discardableResult | |
func waitForEnabled(timeout: Timeouts = .medium, hardAssertion: Bool = true, description: String? = nil) -> Bool { | |
return XCUIElement.wait(for: { self.isEnabled }, timeout: timeout, hardAssertion: hardAssertion, description: description ?? "Waiting for enabled state") | |
} | |
func tapWhenHittable(timeout: Timeouts = .medium, | |
file: StaticString = #file, | |
line: UInt = #line, | |
hardAssertion: Bool = true, | |
description: String? = nil) { | |
let failMessage = "Element was not hittable within \(timeout.rawValue)s: \(self)" | |
if self.waitForHittable(timeout: timeout, hardAssertion: hardAssertion, description: description) { | |
self.tap() | |
} else if hardAssertion { | |
XCTFail(failMessage) | |
} | |
} | |
} | |
// Example usage: | |
self.testApp.textFields["Email"].waitForExistence() | |
self.testApp.textFields["Email"].typeText("...") | |
self.testApp.textFields["Password"].waitForExistence() | |
self.testApp.textFields["Password"].waitForEnabled() | |
self.testApp.textFields["Password"].typeText("...") | |
self.testApp.buttons["Login"].tapWhenHittable() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment