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

血红的双手。 提交于 2020-01-11 11:42:09

问题


There is a cell contains a Collection View. I have set the cells within a Collection View using

cell.accessibilityLabel = @"daren_shangjia_0001_add_new_photo_album";

and then in XCTest, I do

print(app.debugDescription);

I didn't see the accessibilityLabel that I have provided.

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'

回答1:


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.




回答2:


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)



回答3:


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


来源:https://stackoverflow.com/questions/41845309/xctest-ui-tests-can-not-find-collectionview-within-a-table-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!