XCUITest: tap() method doesn't work properly with UITableVIews.

Hi everyone!


Is it possible that the use of the tap() method doesn't work on UITableViews when the cell is not visible? Official documentation says that the method should make the cell with the correspective identifier visible and then tap on it.


But if I try this code:

let app = XCUIApplication()

let myTableView = app.tables.element(matching: .table, identifier: "tableView01")
let myCell = myTableView.cells.element(matching: .cell, identifier: "tappableCell")

myCell.tap()

I get this error: << UITests.testP() failed: Computed invalid hit point (-1.2, -0.5) for Cell, 0x17419c7d0, traits: 8589934593, identifier: 'tappableCell' >>


And if I change the code into this:

let app = XCUIApplication()

let myTableView = app.tables.element(matching: .table, identifier: "tableView01")
myTableView.swipeUp(). //I swipe so that the cell becomes visible...

let myCell = myTableView.cells.element(matching: .cell, identifier: "tappableCell")
myCell.tap()

Nothing happens! On the command line I get the log as the action should've been computed but on the screen nothing happens!


Then I tried positioning a breakpoint just before the tap() method and giving the tap command manually from the command line writing 'po myCell.tap()' and it worked indeed! What's happening?! I have the same strange bug with both Xcode 8.3.3 and Xcode 9 - Beta 4.

Do you know any possible solution? Am I wrong in something? It could be related with my usage of accessibility identifiers to run the query?



Thank you for your support!

Andrea

Answered by Martijn in 274419022

We found a solution that doesn't require any change of your UITest script.

It appears that in Xcode 9 or iOS 11 the property "isAccessibilityElement" is disabled by default and hence isHittable always returns false.

You can enable this property in the Class file of your custom UICollectionViewCell.


See my answer on topic: https://forums.developer.apple.com/thread/90056#274418

I have had success with switching to doubleTap() instead of just tap()

I also include a swipe down. In a view where I know what the name is that I am looking to tap on the code looks something like this:

    func tapItem(itemName: String) {
        var tapped = false
        var swipes = 10
        while !tapped && swipes > 0{
            if !getTableView().cells[itemName].staticTexts[itemName].isHittable {
                getTableView().swipeUp()
            } else {
                getTableView().cells[itemName].staticTexts[itemName].doubleTap()
                tapped = true
            }
            swipes = swipes-1
        }
        if !tapped {
            XCTAssert(false, "item was never tapped")
        }
    }


Hope this helps

Yeah, I came to the same conclusion after various attempts. ℹ

However I found nice your piece of code, thank you! ➕ ✅


I will leave the topic open just a little more to see if someone from Apple could give us a better explanation! 😐

Accepted Answer

We found a solution that doesn't require any change of your UITest script.

It appears that in Xcode 9 or iOS 11 the property "isAccessibilityElement" is disabled by default and hence isHittable always returns false.

You can enable this property in the Class file of your custom UICollectionViewCell.


See my answer on topic: https://forums.developer.apple.com/thread/90056#274418

That helped, thanks.

But why doubleTap()? How it works? Who can told about this?

I have a similar problem, tap() fails only on a pipeline with circleci. we changes to forcetap() and the problem is gone. Any ideas why this fix the problem?

XCUITest: tap() method doesn't work properly with UITableVIews.
 
 
Q