【问题标题】:Xcode 7, UI-Testing: working with UITableViewXcode 7,UI 测试:使用 UITableView
【发布时间】:2015-12-10 08:04:12
【问题描述】:

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

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

提前致谢!

【问题讨论】:

  • 如果我的回答解决了你的问题,请采纳,谢谢

标签: ios xcode7 ui-testing xcode-ui-testing


【解决方案1】:

这对我有用:

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 查看我对 Swift 等价物的回答。
  • let app = XCUIApplication() let tablesQuery = app.tables let cellQuery = tablesQuery.cells. contains(.staticText, identifier: "Work") let cell = cellQuery.children(matching: .staticText)让 cellElement = cell.element cellElement.tap()
【解决方案2】:

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

例如:-

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

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

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

我希望这会有所帮助!

【讨论】:

  • 感谢回复,但是不行,tap()之后什么都没有:/
【解决方案3】:

@Eugenezhenya 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 中编译
  • @delta2flat 立即尝试
【解决方案4】:

我也遇到了同样的问题!

起初,我只是通过.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];

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

希望有帮助!

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多