Xcode7、UI-Testing:使用UITableView

Xcode 7, UI-Testing: working with UITableView

我在使用 Apple 在 WWDC 2015 上推出的 xCode 的 UITesting 框架时遇到了一个问题。 我有一个 UITableView,这个 table 包含很多单元格。我还有一个带有单元格标题的 NSArray - 如果单元格标题包含在 NSArray 中,则应点击此单元格。 我的问题是我无法为特定单元格滚动 table 视图,因为框架不包含使用 table 视图的方法,仅包含滑动手势(向下、向上)。

也许有人知道如何点击 table 视图中的特定单元格?或者如何为特定单元格滚动 table 视图? 因为当我为屏幕上不可见的单元格调用 tap() 方法时 - 没有任何反应。

提前致谢!

我最近遇到了类似的问题。 您可以尝试的一个选项是在 table 中查找单元格的特定标题,然后点击它,

例如:-

XCUIApplication().tables.staticTexts["identifier"].tap()

XCUIApplication().tables.cells.staticTexts["identifier"].tap()

其中 "identifier" 是您尝试点击的单元格的标题。

希望对您有所帮助!

这对我有用:

XCUIElementQuery *tablesQuery = self.app.tables;

XCUIElementQuery *cellQuery = [tablesQuery.cells containingType:XCUIElementTypeStaticText
                                                     identifier:@"Work"];

XCUIElementQuery* cell = [cellQuery childrenMatchingType:XCUIElementTypeStaticText];

XCUIElement* cellElement = cell.element;

[cellElement tap];

要滚动 table 使用:

XCUIElementQuery *tablesQuery = self.app.tables;
XCUIElement* table = tablesQuery.element;
[table swipeUp];

Swift @Eugene Zhenya Gordin 的回答版本: 更新 Swift 4

    let tablesQuery = app.tables
    let cellQuery = tablesQuery.cells.containing(.staticText, identifier: "MyCell")
    let cell = cellQuery.children(matching: .staticText)
    let cellElement = cell.element
    cellElement.tap()

    let tableElement = tablesQuery.element
    tableElement.swipeUp()

我遇到了同样的问题!

起初,我只是通过 .staticTexts 查询点击单元格的标签。这是另一种对我有用的方法:

cellForRowAtIndexPath 中实例化单元格的地方,根据它们的 titleLabel 或另一个 属性 为每个单元格赋予一个唯一标识符。

示例:

cell.accessibilityIdentifier = [NSString stringWithFormat:@"myCellType%@", cell.colorIdentifier];

然后,回到测试 class,试试这个:(故意有点冗长,因为它帮助我更好地理解)

XCUIApplication *application = [XCUIApplication new];
XCUIElementQuery *tablesQuery = application.tables;
XCUIElementQuery *allCellsQuery = tablesQuery.cells;
XCUIElementQuery *specificCellQuery = [cellQuery matchingIdentifier:@"myCellTypeSaffron"];
XCUIElement *cell = specificCellQuery.element;

[cell tap];

特定单元格在与之交互时解析。

希望对您有所帮助!