CollectionView item with nested buttons tapped UITest

Leonard Harris 6 Reputation points
2023-09-05T22:07:28.44+00:00

Question relating to Xamarin.UITest. Is it possible to tap an element inside a collection's ItemTemplate for a particular row item? For example in the below CollectionView I might have multiple row items in this horizontal list and for the 2nd row item I want to invoke the AcceptDependantRequestCommand. Is this possible and if so how? Would I need to set an automationId on the CollectionView or work directly on the button.

<CollectionView
                                Grid.Row="1"
                                Margin="16,0"
                                HeightRequest="200"
                                ItemsSource="{Binding DependantRequestViewModels}">
                                <CollectionView.ItemsLayout>
                                    <LinearItemsLayout
                                        ItemSpacing="16"
                                        Orientation="Horizontal" />
                                </CollectionView.ItemsLayout>
                                <CollectionView.ItemTemplate>
                                    <DataTemplate x:DataType="viewmodels1:DependantRequestViewModel">
                                        <Frame
                                            Padding="16"
                                            CornerRadius="16"
                                            Style="{StaticResource GenericFrameStyle}">
                                            <Grid
                                                RowDefinitions="auto,auto"
                                                RowSpacing="16"
                                                VerticalOptions="Center">
                                                <Label
                                                    Grid.Row="0"
                                                    Style="{StaticResource LabelSecondaryRegularStyle}">
                                                    <Label.FormattedText>
                                                        <FormattedString>
                                                            <FormattedString.Spans>
                                                                <Span Text="{x:Static resx:AppResources.AddText}" />
                                                                <Span Text=" " />
                                                                <Span Text="{Binding FirstName}" />
                                                                <Span Text=" " />
                                                                <Span Text="{x:Static resx:AppResources.AsADependantToYourAccountText}" />
                                                            </FormattedString.Spans>
                                                        </FormattedString>
                                                    </Label.FormattedText>
                                                </Label>
                                                <Grid
                                                    Grid.Row="1"
                                                    ColumnDefinitions="5*,5*"
                                                    ColumnSpacing="14">
                                                    <Button
                                                        Grid.Column="0" 
                                                        Command="{Binding DeleteDependantRequestCommand}"
                                                        CommandParameter="{Binding .}"
                                                        Style="{StaticResource NegativeSmallGhostButtonStyle}"
                                                        Text="{x:Static resx:AppResources.DeleteText}" />
                                                    <Button
                                                        Grid.Column="1"
                                                        Command="{Binding AcceptDependantRequestCommand}"
                                                        CommandParameter="{Binding .}"
                                                        Style="{StaticResource PositiveSmallButtonStyle}"
                                                        Text="{x:Static resx:AppResources.AcceptText}" />
                                                </Grid>
                                            </Grid>
                                        </Frame>
                                    </DataTemplate>
                                </CollectionView.ItemTemplate>
                            </CollectionView>
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
38,664 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,491 Reputation points Microsoft Vendor
    2023-09-06T06:03:00.4166667+00:00

    Hello,

    I want to invoke the AcceptDependantRequestCommand. Is this possible and if so how? Would I need to set an automationId on the CollectionView or work directly on the button.

    Add AutomationId="testButton" directly like following code.

     <Button
    		AutomationId="testButton"
    		Grid.Column="1"
    		Command="{Binding AcceptDependantRequestCommand}"
    		CommandParameter="{Binding .}"
    		Style="{StaticResource PositiveSmallButtonStyle}"
    		Text="{x:Static resx:AppResources.AcceptText}" />
    

    Then open your Tests.cs, add test method and add following code.

         app.Tap(c => c.Marked("testButton"));
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.