gpt4 book ai didi

c# - 如何使用 Winappdriver 访问 GridView 单元格?

转载 作者:行者123 更新时间:2023-12-03 14:53:02 28 4
gpt4 key购买 nike

我正在尝试从 中的 GridView 中获取单元格值WPF 项目与 winappdriver .
我在这条线上遇到了一个问题:

 string name = row.FindElementByName("Name1").Text;

An element could not be located on the page using the given searchparameters.


您能否检查我的以下代码:
 <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);

最佳答案

您显然选择了错误的工具来完成您的任务。
自动化旨在处理 UI 元素,但您需要为任务提供数据。
看看你的 DataGrid 的可视化树是什么样子的:
enter image description here
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);
}

}
在此基础上,您可以为集合 edements 创建一个类型:
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); }
}
和带有集合的 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"},
};
}
将其连接到 DataContext 窗口:
<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>
现在,您的任务简化为在 People 集合中查找所需的项目。

关于c# - 如何使用 Winappdriver 访问 GridView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62608757/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com