Search code examples
iosswiftuitest

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


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?


Solution

  • There is a magic part of XCode 7- UIRecording

    First selected the testCase,and start recording like the Gifenter image description here

    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