【问题标题】:XCUITest; how to find StatusBarXCUITest;如何找到状态栏
【发布时间】:2021-10-26 06:56:33
【问题描述】:

我正在尝试创建一个 XCUITest,我想在其中验证 iOS 的状态栏是:

  • 隐藏;或
  • 可见。

但我无法“找到”它...例如:

func testExample() throws {

    let app = XCUIApplication()
    app.launch()

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

po statusBar1 在控制台中什么时候得到一个空结果:

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

有什么线索可以找到吗?

谢谢!

【问题讨论】:

    标签: ios xcode ipad xcuitest


    【解决方案1】:

    从 iOS 12 开始,状态栏不再可以通过 XCTest 访问,因为它是系统应用程序的一部分。
    如果您签入 iOS 11 及更低版本,app.descendants(matching: .statusBar).element 应该会有预期的结果。

    换句话说,您只能访问那些位于您自己的应用程序窗口中的XCUIElements。


    Credits*适用于XCTest

    【讨论】:

      【解决方案2】:

      现在可以作为跳板后代访问:

      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)
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 2016-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多