假设我有Xamarin应用程序和显示特定项目列表的页面。 我想写一个UI测试,它会添加一些项目,而不是尝试计算它们。
我有下一个listview:
<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并以某种方式描述检查此列表视图中的项目数量。但是我找不到任何有关如何执行它的信息。 先感谢您。
答案 0 :(得分:2)
可以很容易地计算屏幕上显示的元素数量:
app.Query(x => x.Marked("ListView").Child()).Length
如果您想要计算所有元素,请使用backdoor中的this answer。
另一种解决方案可能是滚动以尝试在列表中找到该元素(如果您想要测试的不仅仅是元素创建,那么这将非常有用)。