XCTest UI 测试:无法在 table 单元格中找到 collectionView

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

有一个单元格包含一个集合视图。我已使用

在集合视图中设置单元格
cell.accessibilityLabel = @"daren_shangjia_0001_add_new_photo_album";

然后在 XCTest 中,我做

print(app.debugDescription);

我没有看到我提供的 accessibilityLabel。

Element subtree:
 →Application 0x608000367200: {{0.0, 0.0}, {414.0, 736.0}}, label: '纳豆行'
    Window 0x608000365e80: Main Window, {{0.0, 0.0}, {414.0, 736.0}}
      Other 0x608000365880: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
        Other 0x608000365b80: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
          Other 0x608000365700: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
            Other 0x608000364d40: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
              Other 0x608000364680: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                Other 0x608000364740: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                  Other 0x608000364800: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                    NavigationBar 0x6080003648c0: traits: 35192962023424, {{0.0, 20.0}, {414.0, 44.0}}, identifier: '140'
                      Button 0x608000364980: traits: 8589934593, {{20.0, 31.0}, {20.7, 20.5}}, label: 'da ren list backBtn'
                      StaticText 0x608000365f40: traits: 8590000192, {{192.0, 28.0}, {30.0, 27.0}}, label: '140'
                      Button 0x608000366000: traits: 8589934593, {{360.7, 25.0}, {33.3, 33.1}}, label: 'denglu 0010 you ke register'
                    Other 0x608000366240: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                      Other 0x6000003660c0: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                        Other 0x600000365e80: traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
                          Table 0x600000365dc0: traits: 35192962023424, {{0.0, 0.0}, {414.0, 680.8}}
                            Image 0x608000366a80: traits: 8589934596, {{0.0, 64.0}, {414.0, 219.6}}
                              Image 0x608000366cc0: traits: 8589934596, {{168.4, 135.2}, {77.3, 77.3}}
                                Button 0x608000367080: traits: 8589934593, {{151.8, 118.6}, {110.4, 110.4}}
                            Image 0x6080003672c0: traits: 8589934596, {{245.2, 195.3}, {16.6, 16.6}}, identifier: 'da_ren_list_girl'

要访问容器视图中的视图,必须确保容器视图不可访问。对于您的情况,我认为这是 table 视图单元格。

let cell: UITableViewCell!
cell.isAccessibilityElement = false

为了降低辅助功能用户的复杂性,屏幕的任何部分最多只能有一个辅助视图。单元格通常包含可以概括为单个实体的信息,例如,代表一个项目,其中包括文本和带有选项 select 的图片。由于您决定拥有一个相当复杂的单元格,因此您需要更改它的默认行为以允许辅助功能用户更精细地访问单元格的内容。无法再在 table 视图单元格级别进行汇总。因此,解释屏幕上的内容的工作必须传递给视图层次结构中较低的元素。

要进一步理解这个概念,请尝试在打开 VoiceOver 的情况下使用您的应用,并使用三次 three-finger 点击来切换屏幕窗帘。

我遇到了和你一样的问题。希望这段代码能帮到你。

// 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 辅助功能标识符

"tableViewCell" - 包含 CollectionView 的 TableViewCell

"daren_shangjia_0001_add_new_photo_album" - 集合视图单元格可访问性标识符

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()