Created
July 9, 2024 17:14
-
-
Save phranck/b811c4f73d28ce5f2e8a106814f5a02e to your computer and use it in GitHub Desktop.
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 { | |
func changeText(ofTextField accessibilityId: AccessibilityIdentifier, to newText: String) { | |
let textField = textField(withId: accessibilityId) | |
textField.tap() | |
textField.clearText() | |
textField.typeTextIndividually(newText) | |
} | |
func cleanup() { | |
#if os(iOS) | |
#elseif os(macOS) | |
windows.allElementsBoundByIndex.forEach { window in | |
if window.exists && window.isHittable { | |
if window.buttons[XCUIIdentifierCloseWindow].exists && window.buttons[XCUIIdentifierCloseWindow].isHittable { | |
window.buttons[XCUIIdentifierCloseWindow].click() | |
} | |
} | |
} | |
#endif | |
} | |
@discardableResult | |
func clearText() -> XCUIElement { | |
#if os(iOS) | |
let stringValue = label | |
#elseif os(macOS) | |
guard let stringValue = value as? String else { return self } | |
#endif | |
// Tap the delete key for each character in the text field | |
let deleteString = stringValue.map { _ in XCUIKeyboardKey.delete.rawValue }.joined(separator: "") | |
typeText(deleteString) | |
return self | |
} | |
@discardableResult | |
func doubleTap(textWithId accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
let text = staticTexts[accessibilityId] | |
if text.waitForExistence(timeout: 0.75) == false { | |
XCTFail("Could not find textfield with id: \(accessibilityId)") | |
} | |
text.doubleTap() | |
return self | |
} | |
@discardableResult | |
func navigateInMenu(toColumnType columnType: CBShotListColumnType) -> XCUIElement { | |
guard let index = CBShotListColumnType.genericTypes.firstIndex(of: columnType) else { | |
XCTFail("Could not find requested column type: \(columnType.title)") | |
return self | |
} | |
#if os(iOS) | |
collectionViews.buttons[columnType.title].tap() | |
#elseif os(macOS) | |
for _ in 0...index { typeKey(.downArrow, modifierFlags:.function) } | |
typeKey(.return, modifierFlags:.function) | |
#endif | |
return self | |
} | |
@discardableResult | |
func navigateTo(menuItem accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
let menuItem = groups.menuItems[accessibilityId] | |
if menuItem.waitForExistence(timeout: 1) == false { | |
XCTFail("Could not find menu item with id: \(accessibilityId)") | |
} | |
menuItem.tap() | |
return self | |
} | |
@discardableResult | |
func tap(button accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
let button = buttons[accessibilityId] | |
if button.waitForExistence(timeout: 1) == false { | |
XCTFail("Could not find button: \(accessibilityId)") | |
} | |
button.tap() | |
return self | |
} | |
@discardableResult | |
func tap(buttonWithTitle title: String) -> XCUIElement { | |
let button = buttons[title] | |
if button.waitForExistence(timeout: 0.75) == false { | |
XCTFail("Could not find button with title: \(title)") | |
} | |
button.tap() | |
return self | |
} | |
@discardableResult | |
func tap(slider accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
#if os(iOS) | |
let slider = sliders[accessibilityId] | |
#elseif os(macOS) | |
let slider = sliders[accessibilityId].children(matching: .valueIndicator).element | |
#endif | |
if slider.waitForExistence(timeout: 0.75) == false { | |
XCTFail("Could not find slider: \(accessibilityId)") | |
} | |
slider.tap() | |
return self | |
} | |
@discardableResult | |
func tap(textFieldWithId accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
textField(withId: accessibilityId).tap() | |
return self | |
} | |
@discardableResult | |
func tapEntryInSidebar(withtext text: String) -> XCUIElement { | |
XCTAssertTrue(sideBar.staticTexts[text].exists, "Expected to find an entry named '\(text)' in sidebar, but did not!") | |
sideBar.staticTexts[text].tap() | |
return self | |
} | |
func textField(withId accessibilityId: AccessibilityIdentifier) -> XCUIElement { | |
let textField = textFields[accessibilityId] | |
if textField.waitForExistence(timeout: 0.75) == false { | |
XCTFail("Could not find textfield with id: \(accessibilityId)") | |
} | |
return textField | |
} | |
@discardableResult | |
func typeTextIndividually(_ text: String) -> XCUIElement { | |
if waitForExistence(timeout: 0.75) == false { | |
XCTFail("Could not find element with id: \(self.identifier)") | |
} | |
text.forEach { typeText(String($0)) } | |
return self | |
} | |
@discardableResult | |
func tap(staticText text: String) -> XCUIElement { | |
staticTexts[text].tap() | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment