【问题标题】:UI testing a tab bar controllerUI 测试标签栏控制器
【发布时间】:2016-10-25 02:09:54
【问题描述】:

我构建了一个带有 3 个标签的简单标签栏。

我想运行一个 UI 测试以确保如果用户单击选项卡栏项目,则显示正确的视图控制器。我该怎么做呢?下面是我要开始的代码,只是不知道如何编写我的断言。

func testTabBarMyProfileButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["My Profile"].tap()
}

func testTabBarGraphsButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["Graphs"].tap()
}

func testTabBarAboutButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["About"].tap()
}

【问题讨论】:

    标签: ios swift xcode xcode-ui-testing


    【解决方案1】:

    您可以通过它的位置访问标签栏按钮:

    app.tabBars.buttons.element(boundBy: 2).tap()
    

    【讨论】:

    • 这对我有帮助。一个附加信息:tabBars.buttons.element 从索引 0 开始。所以第一个标签栏项目是 0,第二个是 1 等等。
    【解决方案2】:

    如果您在每个选项卡栏上显示的每个视图控制器中都有不同的控件,您可以断言它们是否存在(这是预期的)。 例如,如果第一个标签栏的 UILabel 名为“First name”,您可以通过编写来断言它是否存在

    Let theLabel = app.staticTexts["myValue"]
    XCTAssert(theLabel.exists).to(beTrue)
    

    在其他屏幕上对不同的控件执行相同的操作。

    【讨论】:

      【解决方案3】:

      如果有人发现这需要 UI 测试另一个应用程序的内容,我刚刚找到了一个解决方案..

      标签栏项目是一个惰性变量,需要先触摸才能按值引用标签栏按钮。添加这一行:

      tabBarItem.accessibilityIdentifier = "my-snazzy-identifier"
      

      到 viewDidLoad 方法,你应该能够在你的 UI 测试中这样做:

      app.tabBars.buttons["Button Title"].tap()
      

      【讨论】:

        【解决方案4】:

        您可以测试导航栏的标题。

        XCTAssert(app.navigationBars["Graphs"].exists)
        

        my GitHub repo for a more detailed UI Testing example

        【讨论】:

        • 我不打算使用导航栏。还有其他方法吗?
        • 您必须在每个屏幕中声明一些独特的东西。这可以是按钮、文本标签或 UI 控件中的任何内容。
        猜你喜欢
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多