iOS UITest passes locally but fails on Bitrise

52 views Asked by At

I have a supposedly straightforward UITest that I want to write.

I have a collection view and I want to scroll to the end of the collection view.

Along the way, I want to count specific elements / cells which are advertisement cells.

Locally (on XCode and Fastlane), this test passes, however, on Bitrise this test fails.

When I look at the snapshot of where the test ended on Bitrise, I notice that for some reason on Bitrise, the test is ending prematurely and does not scroll to the end of the collection view which might the reason it does not count 2 advertisements as expected.

This is the failure message on Bitrise: XCTAssertEqual failed: ("1") is not equal to ("2") - Expected 2 adverts, but found 1.

This is the code of my test

func testAdverts() {
    var previousMaxIndexPath: IndexPath = IndexPath(row: 0, section: 0)
    var advertIdentifiers = Set<String>()
    var noNewIndexSwipeCount = 0

    while true {
        app.swipeUp()

        if let currentMax = currentMaxIndexPath, currentMax > previousMaxIndexPath {
            previousMaxIndexPath = currentMax
            advertIdentifiers.formUnion(visibleAdvertIdentifiers)
            noNewIndexSwipeCount = 0
        } else if noNewIndexSwipeCount >= 2 {
            break
        } else {
            noNewIndexSwipeCount += 1
        }
    }

    XCTAssertEqual(advertIdentifiers.count, 2, "Expected 2 adverts, but found \(advertIdentifiers.count).")
}

Here are some of the other functions that get called within the test for more context.

var visibleCellIdentifiers: [String] {
        return elementQuery(type: .cell).allElementsBoundByIndex.map { $0.identifier }
    }

    var currentMaxIndexPath: IndexPath? {
        return visibleCellIdentifiers.compactMap { extractIndexPath(from: $0) }.max()
    }

    var visibleAdvertIdentifiers: [String] {
        return visibleCellIdentifiers.filter { $0.contains("AdvertStoryTileCollectionViewCell") }
    }

    func extractIndexPath(from identifier: String) -> IndexPath? {
        guard let range = identifier.range(of: "indexPath_\\d+_\\d+", options: .regularExpression) else {
            return nil
        }

        let matchedSubstring = identifier[range]
        let components = matchedSubstring.split(separator: "_")
        guard let section = Int(components[1]), let row = Int(components[2]) else {
            return nil
        }

        return IndexPath(row: row, section: section)
    }

As I mentioned before, the test seems to pass locally on XCode and on FastLane.

I've also tried other approaches to scroll to the end of the collection view like this one but this doesn't work for me as I have cells repeating.

0

There are 0 answers