Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在XCUITest中获得给定文本的单元格索引?_Swift_Xcode_Xctest_Xcuitest - Fatal编程技术网

Swift 如何在XCUITest中获得给定文本的单元格索引?

Swift 如何在XCUITest中获得给定文本的单元格索引?,swift,xcode,xctest,xcuitest,Swift,Xcode,Xctest,Xcuitest,我正在测试一个tableview,在xguitest中查看单元格内容。在我的情况下,我不知道单元格文本的顺序,也不允许我为文本设置可访问性id。给定单元格内的文本,如何获取单元格的索引 例如,如果我想获取包含文本“cell 2 text”的单元格的索引,我会尝试以下方法: func testSample() { let app = XCUIApplication() let table = app.tables let cells = table.cells

我正在测试一个tableview,在xguitest中查看单元格内容。在我的情况下,我不知道单元格文本的顺序,也不允许我为文本设置可访问性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)
}
我觉得我很接近,但我不确定。有人能提出解决办法吗

如果以前有人问过这个问题,我很抱歉。我找不到关于这件事的任何具体信息

我之前访问过的参考资料:


最可靠的方法实际上是将索引添加到可访问性标识符中。但是,你不能。能否更改单元格的可访问性标识符而不是文本

无论如何,如果不滚动表视图,可以这样处理:

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

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

最可靠的方法实际上是将索引添加到可访问性标识符中。但是,你不能。能否更改单元格的可访问性标识符而不是文本

for index in 0..<table.cells.count {
    if table.cells.element(boundBy: index).staticTexts["Your Text"].exists {
        return index
   }
}
无论如何,如果不滚动表视图,可以这样处理:

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

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

对于索引0处的索引。
对于索引0处的索引。。我认为滚动表格在这里没有任何效果。这是特殊情况吗?我想滚动表格在这里不会有任何效果。有什么特殊情况吗?
for index in 0..<table.cells.count {
    if table.cells.element(boundBy: index).staticTexts["Your Text"].exists {
        return index
   }
}