Xcode 7,UI-Testing:使用UITableView

Ale*_*ych 17 ui-testing ios xcode7 xcode-ui-testing

我正面临一个问题,使用UITestingApple在2015年WWDC上引入的xCode框架.我有一个UITableView,这个表包含很多单元格.我还有一个带有单元格标题的NSArray - 如果NSArray中包含单元格标题,则应该点击该单元格.我的问题是我无法滚动特定单元格的表格视图,因为框架不包含处理表格视图的方法,只有滑动手势(向下,向上).

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

提前致谢!

Eug*_*din 14

这对我有用:

XCUIElementQuery *tablesQuery = self.app.tables;

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

XCUIElementQuery* cell = [cellQuery childrenMatchingType:XCUIElementTypeStaticText];

XCUIElement* cellElement = cell.element;

[cellElement tap];
Run Code Online (Sandbox Code Playgroud)

要滚动表,请使用:

XCUIElementQuery *tablesQuery = self.app.tables;
XCUIElement* table = tablesQuery.element;
[table swipeUp];
Run Code Online (Sandbox Code Playgroud)


小智 5

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

例如:-

XCUIApplication().tables.staticTexts["identifier"].tap()
Run Code Online (Sandbox Code Playgroud)

或者

XCUIApplication().tables.cells.staticTexts["identifier"].tap()
Run Code Online (Sandbox Code Playgroud)

其中“标识符”是您尝试点击的单元格的标题。

我希望这有帮助!

  • 感谢您的回复,但它不起作用,tap() 后没有任何反应:/ (4认同)

San*_*osh 5

@Eugene的Swift版本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()
Run Code Online (Sandbox Code Playgroud)


Ari*_*lSD 5

我遇到了同样的问题!

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

cellForRowAtIndexPath实例化单元格的位置,根据它们titleLabel或其他属性为每个单元格指定一个唯一标识符。

例子:

cell.accessibilityIdentifier = [NSString stringWithFormat:@"myCellType%@", cell.colorIdentifier];
Run Code Online (Sandbox Code Playgroud)

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

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

[cell tap];
Run Code Online (Sandbox Code Playgroud)

当特定的细胞与其相互作用时就会分解。

希望有帮助!