Mik*_*ish 6 c# wpf automated-tests ui-automation winappdriver
我正在尝试使用winappdriver从WPF项目中的GridView 获取单元格值。
我在这条线上遇到了一个问题:
string name = row.FindElementByName("Name1").Text;
Run Code Online (Sandbox Code Playgroud)
使用给定的搜索参数无法在页面上定位元素。
您能否检查我的以下代码:
<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);
Run Code Online (Sandbox Code Playgroud)
显然您选择了错误的工具来完成您的任务。自动化旨在与 UI 元素配合使用,但您需要该任务的数据。查看 DataGrid 的可视化树是什么样子的:
DataGrid 继承自 ItemsControl。在他的想象中只有行。没有专栏。从特定单元格中提取数据是可能的,但这非常困难并且没有意义。
您需要创建一个普通的数据源。首先,采用 INotifyPropertyChanged 的某种实现。例如,这个:
/// <summary>Base class implementing INotifyPropertyChanged.</summary>
public abstract class BaseINPC : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>Called AFTER the property value changes.</summary>
/// <param name="propertyName">The name of the property.
/// In the property setter, the parameter is not specified. </param>
public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// <summary> A virtual method that defines changes in the value field of a property value. </summary>
/// <typeparam name = "T"> Type of property value. </typeparam>
/// <param name = "oldValue"> Reference to the field with the old value. </param>
/// <param name = "newValue"> New value. </param>
/// <param name = "propertyName"> The name of the property. If <see cref = "string.IsNullOrWhiteSpace (string)" />,
/// then ArgumentNullException. </param>
/// <remarks> If the base method is not called in the derived class,
/// then the value will not change.</remarks>
protected virtual void Set<T>(ref T oldValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (string.IsNullOrWhiteSpace(propertyName))
throw new ArgumentNullException(nameof(propertyName));
if ((oldValue == null && newValue != null) || (oldValue != null && !oldValue.Equals(newValue)))
OnValueChange(ref oldValue, newValue, propertyName);
}
/// <summary> A virtual method that changes the value of a property. </summary>
/// <typeparam name = "T"> Type of property value. </typeparam>
/// <param name = "oldValue"> Reference to the property value field. </param>
/// <param name = "newValue"> New value. </param>
/// <param name = "propertyName"> The name of the property. </param>
/// <remarks> If the base method is not called in the derived class,
/// then the value will not change.</remarks>
protected virtual void OnValueChange<T>(ref T oldValue, T newValue, string propertyName)
{
oldValue = newValue;
RaisePropertyChanged(propertyName);
}
}
Run Code Online (Sandbox Code Playgroud)
在此基础上,您可以创建集合元素的类型:
public class PersonVM : BaseINPC
{
private string _name;
private uint _age;
private string _mail;
public string Name { get => _name; set => Set(ref _name, value); }
public uint Age { get => _age; set => Set(ref _age, value); }
public string Mail { get => _mail; set => Set(ref _mail, value); }
}
Run Code Online (Sandbox Code Playgroud)
和 ViewModel 与集合:
public class ViewModel
{
public ObservableCollection<PersonVM> People { get; }
= new ObservableCollection<PersonVM>()
{
new PersonVM(){Name="Peter", Age=20, Mail="Peter@mail.com"},
new PersonVM(){Name="Alex", Age=30, Mail="Alex@mail.com"},
new PersonVM(){Name="Nina", Age=25, Mail="Nina@mail.com"},
};
}
Run Code Online (Sandbox Code Playgroud)
将其连接到 DataContext Windows:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<ListView Margin="10" ItemsSource="{Binding People}">
<ListView.View>
<GridView x:Name="ListViewItem" >
<GridViewColumn x:Name="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>
Run Code Online (Sandbox Code Playgroud)
现在,您的任务简化为在人员集合中查找所需的项目。