在 Xcode ui 中查找表格单元格测试 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44431136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find table cell in Xcode ui testing Swift
提问by Qandil Tariq
I am new on Xcode + ios testing. I am going to automate ui of an existing ios project in swift. The problem is that I am unable to find table view of a view. Here is my code which i used to finding a cell from table view but this is not working in this case:
我是 Xcode + ios 测试的新手。我将快速自动化现有 ios 项目的 ui。问题是我无法找到视图的表视图。这是我用来从表格视图中查找单元格的代码,但在这种情况下不起作用:
XCUIApplication().tables.cells.allElementsBoundByIndex[1].tap()
I am attaching screenshot of the view flow. The ios code of view is not written by me.
回答by Kushal Ashok
So you want to find a tableView's cell and then tap on it.
所以你想找到一个 tableView 的单元格,然后点击它。
This solution is posted after testing in Xcode 9.0 beta 6, using Swift 4
此解决方案是在 Xcode 9.0 beta 6 中测试后发布的,使用 Swift 4
As mentioned by Oletha in a comment, it's a good idea to understand the hierarchy of your view by printing the debugDescription:
正如 Oletha 在评论中提到的,通过打印 debugDescription 来了解视图的层次结构是一个好主意:
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launch()
print(app.debugDescription)
}
The print statement will give you a detailed list of views and their hierarchy.
打印语句将为您提供视图及其层次结构的详细列表。
Next, you should add an accessibilityIdentifierto your tableview and cell as follows:
接下来,您应该向 tableview 和单元格添加一个accessibilityIdentifier,如下所示:
a) Inside your viewDidLoad()or any other relevant function of the controller-
a) 在您的viewDidLoad()或控制器的任何其他相关功能中 -
myTableView.accessibilityIdentifier = “myUniqueTableViewIdentifier”
b) Inside your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)function-
b) 在您的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)函数中-
cell.accessibilityIdentifier = "myCell_\(indexPath.row)"
Once done, you can write the following code in your test case to get the desired results:
完成后,您可以在测试用例中编写以下代码以获得所需的结果:
let myTable = app.tables.matching(identifier: "myUniqueTableViewIdentifier")
let cell = myTable.cells.element(matching: .cell, identifier: "myCell_0")
cell.tap()
Note that this will simply help you find and tap the required cell, you can add some XCTAssert checks inside your test case to make it more useful.
请注意,这只会帮助您找到并点击所需的单元格,您可以在测试用例中添加一些 XCTAssert 检查以使其更有用。
P.S.: There are many guides online which can help you learn the UI testing with Xcode. e.g.: https://blog.metova.com/guide-xcode-ui-test/
PS:网上有很多教程可以帮助你学习使用Xcode进行UI测试。例如:https: //blog.metova.com/guide-xcode-ui-test/
回答by CSawy
Objective-C version of Kushal's answer. Last step is more simplified, though.
Kushal 答案的 Objective-C 版本。不过,最后一步更简单。
A- Where you define your table (usually in the viewDidLoad
of its viewController), define its accessibilityIdentifier
:
A- 在你定义你的表的地方(通常在viewDidLoad
它的 viewController 中),定义它的accessibilityIdentifier
:
_myTable.accessibilityIdentifier = @"MyOptionsTable";
B- In cellForRowAtIndexPath
, give each cell an accessibilityIdentifier
:
B- In cellForRowAtIndexPath
,给每个单元格一个accessibilityIdentifier
:
cell.accessibilityIdentifier = @"MySpecialCell";
C- In your test case, get the table, then tap on the cell:
C- 在您的测试用例中,获取表格,然后点击单元格:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *table = app.tables[@"MyOptionsTable"];
XCUIElement *cell = table.cells[@"MySpecialCell"];
[cell tap];