Search code examples
c#xamarinuitest

Get an element from a list with automation id


I have a listview, and I want to get let's say the second element from it and tap on it with uitest, I have AutomationId for the listview ("ReportsList"). Can someone write me a function that can help me?


Solution

  • Basically, this code works but when xaml is updated, you must also update query.

    app.ScrollTo(app.Query(q => q.Id("ListViewAutomationId").Child(0).Child(0)).First().Label)) 
    

    The usage of child at query changes according to listview's item template. You can test it in Repl.

    The other method is that you can add automationId to listview's data source and you can bind it into datatemplate:

    <DataTemplate>
        <ViewCell>
            <Grid AutomationId="{Binding YourAutomationIdFromDataSource}">
    

    after this, in ui test project:

    App.ScrollTo("itemAutomationId")
    App.TapAndWait("itemAutomationId")
    

    I know this method is not exactly a clean one but I've spent so much time with the other method, at the end my last method works well.