【发布时间】:2020-10-17 21:11:09
【问题描述】:
我正在尝试使用 winappdriver 从 WPF 项目中的 GridView 获取单元格值。
我对这一行有疑问:
string name = row.FindElementByName("Name1").Text;
使用给定搜索无法在页面上找到元素 参数。
请检查我的以下代码:
<Grid>
<ListView Margin="10" Name="lvUsers" AutomationProperties.AutomationId="lvUsers">
<ListView.View>
<GridView x:Name="ListViewItem" AutomationProperties.AutomationId="ListViewItem">
<GridViewColumn x:Name="Name1" AutomationProperties.Name="Name1" AutomationProperties.AutomationId="Name1" Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
var listBox = session.FindElementByAccessibilityId("lvUsers");
var comboBoxItems = listBox.FindElementsByClassName("ListViewItem");
foreach (var row in comboBoxItems)
{
string name = row.FindElementByName("Name1").Text;
if (name == "John Doe")
{
findName = true;
break;
}
}
Assert.AreEqual(findName, true);
【问题讨论】:
-
可能不是您想听到的,但是...通常对 wpf 视图模型而不是视图进行自动化测试。遍历数据网格或列表视图以查找单元格的内容总是很痛苦。 gridviewcolumn 是一个抽象的东西。您不会在可视化树中找到它。我建议您下载 snoop(.net core 有不同的版本)并使用它来探索您在运行时在 UI 中实际获得的内容。
-
WinAppDrive 使用 UI 自动化。使用 Windows SDK 中的 inspect.exe 来检查 UI 自动化从正在运行的应用程序中“看到”了什么。 github.com/microsoft/WinAppDriver/blob/master/Docs/… 只有 UIElement 派生类可以支持 UI 自动化。 GridViewColumn 不是 UIElement
标签: c# wpf automated-tests ui-automation winappdriver