6

I have used app.staticTexts["String"].tap() to tap on a button containing that string which is working completely fine.
But the problem here is that i want to print all the static texts which are present on that page, how can i do it in XCUITest ?
My aim here is to iterate through all the static texts present on the page and then add an if condition on my expected text.

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • you can try to find all accessibility elemtns and iterate, check XCUIElementQuery. But what you want to do looks more like unit test than UITest, – Lu_ Jul 02 '19 at 07:52
  • @Lu_ I actually want to iterate through all the static texts present on the page and then i would be adding a `if` condition on the expected text. So how can i perform that – Sameer Arora Jul 02 '19 at 07:54

3 Answers3

11

You can use something like that:

    for staticText in app.staticTexts.allElementsBoundByIndex {
        if staticText.label == "test" {

        }
    }
cesarmarch
  • 617
  • 4
  • 13
1
//Returns all the labels,buttons, textfield,images in the page with its  name.
//Just change the element name in loop according to the need.




 for staticText in  app.staticTexts.allElementsBoundByIndex{

            if staticText != nil{
                print(staticText.label)
            }else{
                print("No static text found")
            }

        }
Ronak Patel
  • 609
  • 4
  • 16
0

I suppose the text is present in the identifier attribute of the staticText element. You can try the following

for i in 0..<app.staticTexts.count {
    let text = app.staticTexts.element(boundBy: i).identifier
    print(text)
    if text == "Your String" {
        // Your code
    }
}