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

我的故事板中有一个UITableView 。 它包含5个表视图单元格。 当我单击第一个单元格时,它将转到第二个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,按索引查询等。

然后跑