简体   繁体   English

Xcode UI 测试 - UITableView 部分

[英]Xcode UI Testing - UITableView sections

I want to use Xcode's UI Testing to count the number of sections in a tableview and the number of cells in each section.我想使用 Xcode 的 UI 测试来计算 tableview 中的部分数量和每个部分中的单元格数量。 How can I do that?我怎样才能做到这一点?

As of Xcode 7, table view headers show as Other elements.从 Xcode 7 开始,表格视图标题显示为Other元素。

Here's what I did for a (grouped) table view in my app:这是我在我的应用程序中为(分组的)表格视图所做的:

extension TableLayoutTests {

    func testHasMessagesGroup() {
        XCTAssert(app.tables.otherElements["MESSAGES"].exists)
    }

    func testHasMessageCell() {
        let header = app.tables.otherElements["MESSAGES"]
        let cell = app.tables.cells.elementBoundByIndex(1)
        XCTAssertGreaterThanOrEqual(cell.accessibilityFrame.minY, header.accessibilityFrame.maxY)
    }

    func testHasOtherMessageCell() {
        let header = app.tables.otherElements["MESSAGES"]
        let cell = app.tables.cells.elementBoundByIndex(2)
        XCTAssertGreaterThanOrEqual(cell.accessibilityFrame.minY, header.accessibilityFrame.maxY)
    }

}

Joe Masilotti is entirely correct in that application code cannot be accessed directly from the UI Test, though you can backchannel if you want to be out-of-style. Joe Masilotti 是完全正确的,因为应用程序代码不能直接从 UI 测试访问,但如果你想不合时宜,你可以反向通道。 Let's assume you don't.让我们假设你没有。

Xcode's UI Testing framework has access to all of the Swift language along with the UI hierarchy of your app, but not access to the data layer; Xcode 的 UI 测试框架可以访问所有 Swift 语言以及应用程序的 UI 层次结构,但不能访问数据层; so you can do the following:因此您可以执行以下操作:

  • Give the headers or footers of the tableview sections accessibilityIdentifiers that include an index component (eg, photos.headers_0 , etc)给出包含索引组件(例如,photos.headers_0 等)的 tableview 部分的页眉或页脚accessibilityIdentifiers
  • Having done that, use conditionals to determine the number of sections完成后,使用条件来确定部分的数量
  • Similarly, photos.section_0.cell_0 can be assigned in your data source, and an iterator + conditional can store the number of cells in section 0 or any other section.类似地,photos.section_0.cell_0 可以在你的数据源中赋值,迭代器+条件可以存储第0节或任何其他节中的单元格数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM