Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode UITest scrolling to the bottom of an UITableView

I am writing an UI test case, in which I need to perform an action, and then on the current page, scroll the only UITableView to the bottom to check if specific text shows up inside the last cell in the UITableView.

Right now the only way I can think of is to scroll it using app.tables.cells.element(boundBy: 0).swipeUp(), but if there are too many cells, it doesn't scroll all the way to the bottom. And the number of cells in the UITableView is not always the same, I cannot swipe up more than once because there might be only one cell in the table.

like image 297
Amy L. Avatar asked Jan 22 '19 20:01

Amy L.


2 Answers

One way you could go about this is by getting the last cell from the tableView. Then, run a while loop that scrolls and checks to see if the cell isHittable between each scroll. Once it's determined that isHittable == true, the element can then be asserted against. https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable

It would look something like this (Swift answer):

  1. In your XCTestCase file, write a query to identify the table. Then, a subsequent query to identify the last cell.
let tableView = app.descendants(matching: .table).firstMatch
guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
  1. Use a while loop to determine whether or not the cell isHittable/is on screen. Note: isHittable relies on the cell's userInteractionEnabled property being set to true
//Add in a count, so that the loop can escape if it's scrolled too many times
let MAX_SCROLLS = 10
var count = 0
while lastCell.isHittable == false && count < MAX_SCROLLS {
    apps.swipeUp()
    count += 1
}
  1. Check the cell's text using the label property, and compare it against the expected text.
//If there is only one label within the cell
let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
like image 52
Blaine Avatar answered Dec 04 '22 16:12

Blaine


Blaines answer lead me to dig a little bit more into this topic and I found a different solution that worked for me:

func testTheTest() {
    let app = XCUIApplication()
    app.launch()

    // Opens a menu in my app which contains the table view
    app.buttons["openMenu"].tap()

    // Get a handle for the tableView
    let listpagetableviewTable = app.tables["myTableView"]

    // Get a handle for the not yet existing cell by its content text
    let cell = listpagetableviewTable.staticTexts["This text is from the cell"]

    // Swipe down until it is visible
    while !cell.exists {
        app.swipeUp()
    }

    // Interact with it when visible
    cell.tap()
}

One thing I had to do for this in order to work is set isAccessibilityElement to true and also assign accessibilityLabel as a String to the table view so it can be queried by it within the test code.

This might not be best practice but for what I could see in my test it works very well. I don't know how it would work when the cell has no text, one might be able to reference the cell(which is not really directly referenced here) by an image view or something else. It's obviously missing the counter from Blaines answer but I left it out for simplicity reasons.

like image 26
Cronay Avatar answered Dec 04 '22 16:12

Cronay