xcode 给定 XCTest UI 测试中的索引,如何验证表视图行中是否存在文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32365327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:45:56  来源:igfitidea点击:

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

xcodeswiftxctestxcode-ui-testing

提问by Evgenii

I am using XCTest UI Test framework to check rows in a table view. In my test I need to verify the existence of text in a table view row given its index.

我正在使用 XCTest UI 测试框架来检查表视图中的行。在我的测试中,我需要验证给定索引的表视图行中是否存在文本。

I was thinking of using tableRows.elementBoundByIndexmethod, but it did not return anything. The number of rows returned by calling tableRows.countis zero. The table is present and filled because I can query the text inside the rows successfully:

我正在考虑使用tableRows.elementBoundByIndex方法,但它没有返回任何东西。调用返回的行数tableRows.count为零。该表存在并填充,因为我可以成功查询行内的文本:

XCTAssertEqual(app.tables.staticTexts["Cute Alpaca"].exists)

How can I verify existence of a text in a row given its index in an XCTest UI test?

给定 XCTest UI 测试中的索引,如何验证一行中文本的存在?

回答by Kevin

It turns out that tableRowsdoesn't actually detect rows in the UITableView. Use cellsinstead.

事实证明,tableRows实际上并没有检测到UITableView. 使用cells来代替。

I have a feeling that tableRowsand tableColumnsare used to do testing on Mac OS X where you have actual tables while on iOS we have UITableViewCelland UICollectionViewCell. Or this could be a bug as we are still in the beta period.

我有一种感觉,tableRows并且tableColumns习惯于在 Mac OS X 上进行测试,在那里你有实际的表,而在 iOS 上我们有UITableViewCellUICollectionViewCell. 或者这可能是一个错误,因为我们仍处于测试阶段。

let table = app.tables.element
XCTAssertTrue(table.exists)

let cell = table.cells.elementBoundByIndex(2)
XCTAssertTrue(cell.exists)
let indexedText = cell.staticTexts.element
XCTAssertTrue(indexedText.exists)

or the one liner

或一个班轮

XCTAssertTrue(app.tables.element.cells.elementBoundByIndex(2).exists)

回答by Canh Tran

For your case, I think this code will help you.

对于您的情况,我认为此代码会对您有所帮助。

let tableCellContainer = app.tables["HomeTableView"].cells.element(boundBy: 1) // Get the first Cell
let cell = tableCellContainer.staticTexts["Brazil"]
XCTAssert(cell.exists)

Regards,

问候,