I can find a solution but it is not very nice. It seems there is no API to trigger the statusbar directly.
The following is suggested by ChatGPT
import XCTest
import CoreGraphics
func moveMouseToTop() {
let screenHeight = NSScreen.main?.frame.height ?? 900
let topPoint = CGPoint(x: 100, y: 2) // Move near the top edge
let move = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: topPoint, mouseButton: .left)
move?.post(tap: .cghidEventTap)
}
class StatusBarTests: XCTestCase {
func testClickStatusBarIcon() {
let app = XCUIApplication(bundleIdentifier: "your.app.bundle.identifier")
app.launch()
moveMouseToTop() // Ensure status bar is visible
let statusBar = XCUIApplication(bundleIdentifier: "com.apple.systemuiserver")
let statusBarItem = statusBar.menuBars.children(matching: .button).element(boundBy: 0) // Adjust index for your icon
XCTAssertTrue(statusBarItem.exists)
statusBarItem.click()
}
}