6

I understand how I could, for example look at one element in a table, but what's the correct way to simply iterate through all the elements, however many are found, to for example tap on them?

let indexFromTheQuery = tables.staticTexts.elementBoundByIndex(2)

Dave Kliman
  • 441
  • 4
  • 17
  • Table view cells are reused, so unless you cause the table view to scroll, the ones "off screen" aren't instantiated and hence don't really exist. – Nicolas Miari Feb 20 '16 at 10:11
  • ok so two questions... what is the easiest way to iterate through all the ones on just one screen... let's say 5 items ... and then what would be the most efficient way to find all of them, and try tapping on each one, then coming back to the initial table view afterwards, if there is a segue for example? – Dave Kliman Feb 20 '16 at 10:17
  • For a limited set of cells, you could record your interaction with the app and use as a an UI test; UI testing were introduced in XCode 7, [see e.g. this tutorial](https://www.bignerdranch.com/blog/ui-testing-in-xcode-7-part-1-ui-testing-gotchas/). – dfrib Feb 20 '16 at 10:52
  • I am actually just interested in seeing the syntax of the for in loop and which item to be iterating over... – Dave Kliman Feb 20 '16 at 14:22

3 Answers3

8

Okay I succeeded in figuring out the simple syntax. I have just started working with Swift and so it took me sleeping on it to think of the answer.

This code works:

var elementLabels = [String]()
for i in 0..<tables.staticTexts.count {
    elementLabels.append (tables.staticTexts.elementBoundByIndex(i).label)
}  
print (elementLabels)
Dave Kliman
  • 441
  • 4
  • 17
0

My guess is the answer is there is no way to do so.

The reason is because of my experience with one of the critical iOS components while making a number of UI tests: UIDatePicker.

If you record a test where you get the page up and then you spin the picker, you will notice that the commands are all non-specific and are screen related. In the case of the picker, however, community requests resulted in the addition of a method for doing tests: How to select a picker view item in an iOS UI test in Xcode?.

Maybe you can add a helper method to whatever controller contains this table. Also, keep in mind that you can easily add methods without polluting the class interface by defining them as extensions that are in test scope only.

Community
  • 1
  • 1
Rob
  • 11,446
  • 7
  • 39
  • 57
0

For Xcode 11.2.1, SwiftUI and swift 5 based app, the following code works for testing a list, each element in this case appears as a button in the test code. The table is set up like this (for each row) :

         NavigationLink(destination: TopicDetail(name: "Topic name", longDesc: "A description")) {
                TopicRow(thisTopic: top).accessibility(identifier: "newTopicRow_\(top.name!)")
            }

Then I catch the members of the table by getting the buttons into an array:

    let myTable = app.tables.matching(identifier: "newTopicTable")

    var elementLabels = [String]()

    for i in 0..<myTable.buttons.count {
        elementLabels.append (tablesQuery.buttons.element(boundBy: i).label)
    }

    print (elementLabels)

Finally, I deleted each member of the table by selecting the detail view where I have a delete button, again with

.accessibility(identifier: "deleteTopic"

I wanted to delete all members of the table:

    for topicLabel  in elementLabels {

        let myButton = app.buttons[topicLabel]

        myButton.firstMatch.tap()

        app.buttons["deleteTopic"].tap()


    }
Tomm P
  • 761
  • 1
  • 8
  • 19