1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-28 23:00:07 +01:00
Files
app/tests/ui/UITestCase.swift

98 lines
3.1 KiB
Swift

//
// UITestCase.swift
// UI Tests
//
// Created by Nico Verbruggen on 15/10/2022.
// Copyright © 2025 Nico Verbruggen. All rights reserved.
//
import XCTest
class UITestCase: XCTestCase {
/**
Launches the app and opens the menu.
Defaults to waiting for the app to finish initialization.
- Parameter waitForInitialization: Waits for the PHP Monitor to pass the environment checks (startup).
- Parameter openMenu: Attempts to open the status menu when ready; requires passing environment checks.
- Parameter configuration: The TestableConfiguration to include when launching PHP Monitor.
*/
public func launch(
waitForInitialization: Bool = true,
openMenu: Bool = false,
with configuration: TestableConfiguration? = nil,
) -> XCPMApplication {
let app = XCPMApplication()
let config = configuration ?? TestableConfigurations.working
app.withConfiguration(config)
app.launch()
if waitForInitialization || openMenu {
let statusItem = app.statusItems.firstMatch
let isEnabled = NSPredicate(format: "isEnabled == true")
let expectation = expectation(for: isEnabled, evaluatedWith: statusItem, handler: nil)
let result = XCTWaiter().wait(for: [expectation], timeout: 15)
if result == .timedOut {
XCTFail("PHP Monitor did not initialize with an available UI element within 15 seconds.")
}
if openMenu {
statusItem.click()
}
}
// Note: If this fails here, make sure the menu bar item can be displayed
// If you use Bartender or something like this, this item may be hidden and tests will fail
return app
}
/** Checks if a single element exists. */
public func assertExists(_ element: XCUIElement, _ timeout: TimeInterval = 0.05) {
XCTAssertTrue(element.waitForExistence(timeout: timeout))
}
/** Checks if a single element fails to exist. */
public func assertNotExists(_ element: XCUIElement, _ timeout: TimeInterval = 0.05) {
XCTAssertFalse(element.waitForExistence(timeout: timeout))
}
/** Checks if all elements exist. */
public func assertAllExist(_ elements: [XCUIElement], _ timeout: TimeInterval = 0.05) {
for element in elements {
XCTAssert(element.waitForExistence(timeout: timeout))
}
}
/** Clicks on a given element. */
public func click(_ element: XCUIElement) {
element.click()
}
}
extension XCPMApplication {
/**
Opens a given menu item found in the menu bar's status item.
*/
public func mainMenuItem(withText text: String) -> XCUIElement {
self.statusItems.firstMatch.menuItems[text].firstMatch
}
}
extension XCUIElement {
/**
Clears all the text from a given element.
*/
func clearText() {
guard let stringValue = self.value as? String else {
return
}
var deleteString = String()
for _ in stringValue {
deleteString += XCUIKeyboardKey.delete.rawValue
}
typeText(deleteString)
}
}