Xcode 7,UI测试:如何使用UITableView

18

我在使用苹果在2015年WWDC推出的UITesting框架时遇到了一个问题。

我有一个UITableView,其中包含许多单元格。同时,我有一个包含单元格标题的NSArray - 如果单元格标题包含在NSArray中,则应该点击该单元格。

我的问题是,我无法滚动表格视图以查看特定单元格,因为该框架不包含用于处理表格视图的方法,只有滑动手势(向下,向上)。

也许有人知道如何在表格视图中点击特定单元格?或者如何滚动表格视图以便查看特定单元格?因为当我对屏幕上不可见的单元格调用tap()方法时,什么都不会发生。

提前致谢!


1
如果我的回答解决了您的问题,您能否接受它呢?谢谢。 - Eugene Gordin
4个回答

17

这个对我有用:

XCUIElementQuery *tablesQuery = self.app.tables;

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

XCUIElementQuery* cell = [cellQuery childrenMatchingType:XCUIElementTypeStaticText];

XCUIElement* cellElement = cell.element;

[cellElement tap];

要滚动表格,请使用:

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

有任何想法如何使用Swift实现这个? - cptdanko
1
@cptdanko 请查看我提供的Swift等效答案。 - Santosh
让app = XCUIApplication() 让tablesQuery = app.tables 让cellQuery = tablesQuery.cells.containing(.staticText, identifier: "Work") 让cell = cellQuery.children(matching: .staticText) 让cellElement = cell.element cellElement.tap() - adak

5
我最近遇到了类似的问题。你可以尝试的一个选项是在表格中查找单元格的特定标题,然后点击它,例如:
XCUIApplication().tables.staticTexts["identifier"].tap()

或者

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

“identifier”是您要点击的单元格的标题。

希望这可以帮助您!


4
谢谢回复,但它没有起作用,在tap()之后什么都没有发生:/ - Aleh Sarochych

5

我曾经遇到过完全相同的问题!

起初,我只是通过 .staticTexts 查询点击单元格标签。这里提供另一种方法:

cellForRowAtIndexPath 中,实例化单元格时,根据它们的 titleLabel 或其他属性为它们分别赋予唯一的识别符。

例如:

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

然后,在测试类中尝试这样做:(有点冗长是有意为之的,因为它能帮助我更好地理解)
XCUIApplication *application = [XCUIApplication new];
XCUIElementQuery *tablesQuery = application.tables;
XCUIElementQuery *allCellsQuery = tablesQuery.cells;
XCUIElementQuery *specificCellQuery = [cellQuery matchingIdentifier:@"myCellTypeSaffron"];
XCUIElement *cell = specificCellQuery.element;

[cell tap];

具体的单元格会在与之交互时解决。
希望这有所帮助!

5

@Eugene Zhenya Gordin的答案对应的Swift版本: 已更新至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()

这些在Swift 4中无法编译。 - Corbin Miller
@delta2flat 现在尝试一下。 - Santosh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接