【问题标题】:swift in UI test(test the num of cells in UITable View)swift in UI test(测试UITableView中的单元格数)
【发布时间】:2015-11-11 02:45:48
【问题描述】:

我对 UI 测试很陌生。我的故事板中有一个UITableView,它包含一些单元格。 更新: 我想断言应用程序启动时UITableView 中的单元格数量将超过0。但我不知道如何编写这部分代码。使用NSPredicate?还是其他人?

 func testCellsNum()
{
        let app = XCUIApplication()
        let tableCell = app.tableRows.count
    //Then what should I do ?
        XCTAssertGreaterThan(tableCell, 0, "should greater than 0")//this line doesn't work


}

【问题讨论】:

    标签: ios swift uitest


    【解决方案1】:

    如果你只有一张桌子:

    func testExample() {
        let app = XCUIApplication()
        let tablesQuery = app.tables
        let count = tablesQuery.cells.count
        XCTAssert(count > 0)
    }
    

    如果你有多个表,使用它来获取第一个或任何你想要的索引

     tablesQuery.elementAtIndex(0).cells
    

    【讨论】:

    • 我有两个场景。第一个有 UITableView。当我单击其中的第一个单元格时,它将进入第二个场景。第二个场景有 ImageCells。如何测试第二个场景中的图像单元数?
    • 看我这里回复的帖子stackoverflow.com/a/33643956/3940672,you可以使用recoding生成原始代码,然后对我发布的代码稍作修改
    • 你也可以像myTableView.accessibilityIdentifier = “myUniqueTableViewIdentifier”一样向你的tableview添加一个accessibilityIdentifier,一旦完成,你就可以在你的测试用例中访问tableView,比如:let myTableView = app.tables.matching(identifier: "myUniqueTableViewIdentifier")
    • 谁能确认这在 iOS 11、Xcode 9 中有效?断言对我来说总是失败,即使是静态单元格。
    • @damjandd 我遇到了同样的问题,可能与:stackoverflow.com/questions/33679817/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多