Search code examples
xcode-ui-testing

SwiftUI UI Test How To Display Dynamic Buttons Text Values in an app.scrollViews?


How To Display Dynamic Buttons Text Values in an app.scrollViews?

I would like to able to tap the button inside first row in the scrollViews, but not sure what the index of the button is. I tried the 1, 2 and 3 with no luck.

let scrollViewsQuery = app/*@START_MENU_TOKEN@*/.scrollViews/*[[".otherElements[\"Tabbar\"].scrollViews",".scrollViews"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/
    let elementsQuery = scrollViewsQuery.otherElements
    elementsQuery.buttons.element(boundBy: 0).tap() //
    print("----------------------------------------------")
    var i = 0
    for element in elementsQuery.buttons.allElementsBoundByIndex{
        i += 1
        print(i)
        print(element) //How To Display the Button Text here?
       // print( elementsQuery.buttons.element(boundBy: i))
    }

Solution

  • Assuming you only have one scrollView present, the code to tap the first button in it would be the following:

    let myScrollView = app.scrollViews.firstMatch
    let myScrollViewsButtons = myScrollView.buttons
    let myScrollViewsFirstButton = myScrollViewButtons.firstMatch
    
    myScrollViewsFirstButton.tap()
    

    A button in this context is an XCUIElement, not something that is particularly printable. Buttons do have label attributes that are generally the text displayed on them...