问题
There is an UITableView
in my storyboard. It contains 5 Table view cells.
When I click the first cell, it will go to the second UIView and show the corresponding images. How could I code the UITest part to test whether the image show up when I click the first cell?
回答1:
There is a magic part of XCode 7- UIRecording
First selected the testCase,and start recording like the Gif
Then you will see,UIRecording create UITest code for you,In my case,I get
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()
Then,I just need a little change
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)
So,the there are mainly two step
- Let UIRecording Create codes for you
- Make a little changes,such as add Assert,query by index,and so on.
Then run
来源:https://stackoverflow.com/questions/33643739/how-to-write-ui-test-to-test-whether-image-existing-when-click-the-first-cell-in