2

I am trying to create a XCUITest where I like to verify that iOS' Status Bar is either:

  • hidden; or
  • visible.

But I cannot 'find' it... for example:

func testExample() throws {

    let app = XCUIApplication()
    app.launch()

    let statusBar1 = app.descendants(matching: .statusBar)
    let statusBar2 = app.statusBars.element(boundBy: 0)
}

When do po statusBar1 in the console I get an empty result:

Find: Target Application 'com.domain.TestStatusBar'
  ↪︎Find: Descendants matching type StatusBar

Any clue of how to find it?

Thanks!

Jens Schwarzer
  • 2,840
  • 1
  • 22
  • 35

2 Answers2

3

It's now accessible as a springboard descendant:

class Springboard {

    static let shared = XCUIApplication(bundleIdentifier: "com.apple.springboard")

    static var statusBar: XCUIElement {
        // There are always two statusBars, 
        // but only one of them is accessible when we need to tap it
        if XCUIDevice.shared.orientation.isPortrait {
            return Springboard.shared.statusBars.firstMatch
        }
        else {
            return Springboard.shared.statusBars.element(boundBy: 1)
        }
    }
}
ilonska
  • 121
  • 2
1

As of iOS 12, the status bar is no longer accessible through XCTest as it is a part of the system application.
If you check in iOS 11 & below, app.descendants(matching: .statusBar).element should have the expected result.

In other words, you can access only those XCUIElements that live within your own application window.


Credits*Applies to XCTest

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98