XCTest UI tests: can not find collectionView within a table cell

懵懂的女人 提交于 2019-12-02 08:13:27

To access a view within a container view, you must ensure that the container view is not accessible. In your case, I think this is the table view cell.

let cell = UITableViewCell!
cell.isAccessibilityElement = false

To reduce complexity for Accessibility users, there will be a maximum of one accessible view on any part of the screen. A cell is usually something which contains information which can be summarised as a single entity, for example, representing an item which includes text and a picture with the option to select it. Since you have decided to have quite a complex cell, you need to change the default behaviour of it to allow Accessibility users more granular access to the contents of the cell. It can no longer be summarised at the table view cell level. So the job of explaining what is on the screen must be passed to elements lower in the view hierarchy.

To further understand this concept, try using your app with VoiceOver turned on and use a triple three-finger tap to toggle the screen curtain.

Canh Tran

I've had the same issue as you. Hope this code will help you.

// Get the first Cell and don't forget to set id for your tableview
let tableCellContainer = app.tables["HomeTableView"].cells.element(boundBy: 1)

// Get collection cell which is has the text label inside is 'Brazil'
let cell = tableCellContainer.staticTexts["Brazil"] 

cell.tap()                          
XCTAssert(cell.exists)

"tableView" - TableView Accessibility Identifier

"tableViewCell" - TableViewCell Containing CollectionView

"daren_shangjia_0001_add_new_photo_album" - Collection View Cell accessibility Identifier

let myTable = app.tables.matching(identifier: "tableView")
        let cell = myTable.cells.element(matching: .cell, identifier: "tableViewCell").firstMatch
        let element = cell.children(matching: .any).matching(identifier: "daren_shangjia_0001_add_new_photo_album")
        element.element.tap()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!