【问题标题】:How to get count of the listview items at Xamarin UITests如何在 Xamarin UITests 获取列表视图项的计数
【发布时间】:2018-04-13 09:49:16
【问题描述】:

假设我有显示特定项目列表的 Xamarin 应用程序和页面。 我想编写 UI 测试来添加一些项目,而不是尝试计算它们。

我有下一个列表视图:

<ListView x:Name="ListView" Grid.Row="1" ItemsSource="{Binding Items, Mode=TwoWay}" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand"
              BackgroundColor="Transparent" SeparatorVisibility="None" HasUnevenRows="false" Margin="20, 0">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <ViewCell.View>
                        <Grid AutomationId="ListItem" VerticalOptions="FillAndExpand">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>
                            <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand" VerticalOptions="Center" HeightRequest="38" Grid.Row="1">
                                <!-- item's description -->
                            </StackLayout>          
                        </Grid>
                    </ViewCell.View>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

还有我的 UI 测试:

[Test]
    public void CheckForSamplesAdding()
    {
        app.Tap(c => c.Id("PageWithList"));

        // add some items: 

        app.EnterText("inputForItem", "item001");
        app.PressEnter();

        Task.Delay(250);

        app.EnterText("inputForItem", "item002");
        app.PressEnter();

        Task.Delay(250);

        app.EnterText("inputForItem", "item003");
        app.PressEnter();

        Task.Delay(250);

        // assert to check count of listView items
    }

我想我必须将automationId添加到列表视图中,并以某种方式描述检查此列表视图中的项目计数..但我找不到任何信息如何执行它.. 先感谢您。

【问题讨论】:

    标签: listview xamarin ui-testing xamarin.uitest


    【解决方案1】:

    用类似的方法很容易计算屏幕上显示的元素数量:

    app.Query(x => x.Marked("ListView").Child()).Length
    

    如果你想计算所有元素,你应该使用backdoor,比如this answer

    另一种解决方案是滚动以尝试在列表中查找元素(如果您想测试的不仅仅是元素创建,这很有用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2019-08-17
      • 1970-01-01
      相关资源
      最近更新 更多