如何编写 UI 测试来测试单击 UITableView 中的第一个单元格时图像是否存在?

how to write UI test to test whether image existing when click the first cell in an UITableView?

我的故事板中有一个 UITableView。它包含 5 Table 个视图单元格。 当我单击第一个单元格时,它将转到第二个 UIView 并显示相应的图像。我如何编写 UITest 部分以测试当我单击第一个单元格时图像是否显示?

有一个神奇的部分XCode 7- UIRecording

先选中testCase,开始像Gif那样录制

然后你会看到,UIRecording 为你创建 UITest 代码,在我的例子中,我得到

  let app = XCUIApplication()
  app.tables/*@START_MENU_TOKEN@*/.staticTexts["Hello"]/*[[".cells.staticTexts[\"Hello\"]",".staticTexts[\"Hello\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()
  app.otherElements.containingType(.NavigationBar, identifier:"UIView").childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Image).element.tap()

那么,我只需要一点零钱

    let app = XCUIApplication()
    app.tables.cells.elementBoundByIndex(0).tap()
    let imageView =  app.otherElements.containingType(.NavigationBar, identifier:"UIView").childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Image)
    let count = imageView.images.count
    XCTAssert(count != 0)

所以,主要有两步

  • 让UIRecording为你创造代码
  • 稍微改动一下,比如添加Assert,按索引查询等。

然后运行