【问题标题】:How to get the index of a cell given the text inside in XCUITest?给定 XCUITest 中的文本,如何获取单元格的索引?
【发布时间】:2019-06-17 18:43:24
【问题描述】:

我正在测试 XCUItest 中的单元格内容的表格视图。就我而言,我不知道单元格文本的顺序,也不允许我为文本设置可访问性 ID。给定内部文本,如何获取单元格的索引?

例如,如果我想获取包含文本“Cell 2 Text”的单元格的索引,我会尝试这样的操作:

func testSample() {
    let app = XCUIApplication()
    let table = app.tables
    let cells = table.cells

    let indexOfCell2Text = cells.containing(.staticText, identifier: "Cell 2 Text").element.index(ofAccessibilityElement: "I dunno")
    print(indexOfCell2Text)
}

我觉得我很接近,但我不确定。任何人都可以提出解决方案吗?

如果之前有人问过这个问题,我深表歉意。我找不到任何关于此的具体内容。

我之前访问过的参考资料: https://developer.apple.com/documentation/xctest/xcuielementquery/1500842-element

How can I verify existence of text inside a table view row given its index in an XCTest UI Test?

iOS UI Testing tap on first index of the table

【问题讨论】:

    标签: swift xcode xctest xcuitest


    【解决方案1】:

    真正最可靠的方法是将索引添加到可访问性标识符中。但是,你不能。您可以更改单元格的可访问性标识符而不是文本吗?

    不管怎样,如果你不滚动你的表格视图,你可以这样处理:

    let idx = 0
    for cell in table.cells.allElementsBoundByIndex {
        if cell.staticTexts["Text you are looking for"].exists {
            return idx
        }
        idx = idx + 1
    }
    

    否则,您将使用的索引与屏幕上显示的单元格相关。因此,滚动后,新的第一个可见单元格将成为索引 0 处的单元格,并且会搞砸您的搜索。

    【讨论】:

    • 我认为滚动表格在这里不会有任何效果。有什么特殊情况吗?
    【解决方案2】:
    for index in 0..<table.cells.count {
        if table.cells.element(boundBy: index).staticTexts["Your Text"].exists {
            return index
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 2018-09-17
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多