Search code examples
swiftuitableviewxcode-ui-testingxctestcase

How can I verify a cell is visible in my UI Test


I am trying to assert in a UITest that double tapping the home icon scrolls my table view back to the top.

I assume the pattern for this, is loading the view, scrolling, asserting it cannot be seem, tapping home and asserting it can now.

I have something like this

   let username = "...."
    let password = "...."

    app.launch()

    let loginPage = app.otherElements["login page"]
    loginPage.waitForExistence()

    let usernameInputField = app/*@START_MENU_TOKEN@*/.webViews.textFields["Email address"]/*[[".otherElements[\"bfx_login\"].webViews",".otherElements[\"Log in to OneHub | Recognition\"].textFields[\"Email address\"]",".textFields[\"Email address\"]",".webViews"],[[[-1,3,1],[-1,0,1]],[[-1,2],[-1,1]]],[0,0]]@END_MENU_TOKEN@*/
    usernameInputField.tap()
    usernameInputField.typeText(username)

    let passwordInputField = app/*@START_MENU_TOKEN@*/.webViews.secureTextFields["Password"]/*[[".otherElements[\"bfx_login\"].webViews",".otherElements[\"Log in to OneHub | Recognition\"].secureTextFields[\"Password\"]",".secureTextFields[\"Password\"]",".webViews"],[[[-1,3,1],[-1,0,1]],[[-1,2],[-1,1]]],[0,0]]@END_MENU_TOKEN@*/
    passwordInputField.tap()
    passwordInputField.typeText(password)

    loginPage.buttons["Log In"].tap()

    let tabBarController = app.tabBars["home_tabBarController"]
    tabBarController.waitForExistence()

    let activityFeed = app.tables["Activity Feed"]
    activityFeed.waitForExistence()

    app.swipeUp()

    XCTAssertFalse(activityFeed.cells.element(boundBy: 0).isHittable)

    let homeButton = tabBarController.children(matching: .button).element(boundBy: 0)
    homeButton.waitForExistence()
    homeButton.doubleTap()

    XCTAssertTrue(activityFeed.cells.element(boundBy: 0).isHittable)

Everything works apart from being able to assert against the cell. I do not believe

    XCTAssertFalse(activityFeed.cells.element(boundBy: 0).isHittable)

and

    XCTAssertTrue(activityFeed.cells.element(boundBy: 0).isHittable)

are working as I thought they would.

How can I assert a cell in a tableview is visible?


Solution

  • As far as checking whether something is visible goes, you want to use isHittable in conjunction with exists. See also here. Like so:

    element.exists && element.isHittable

    You have said isHittable does not work for you, can you explain what it does not do?