wpf 如何使用Winappdriver访问GridView单元格?

gzszwxb4  于 2023-04-22  发布在  其他
关注(0)|答案(3)|浏览(221)

我正在尝试使用winappdriverWPF项目的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);
rqqzpn5f

rqqzpn5f1#

很明显,您选择了错误的工具来完成任务。自动化设计用于处理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);
    }

}

在此基础上,您可以创建集合元素的类型:

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集合中查找所需的项目。

nafvub8i

nafvub8i2#

如果你知道单元格在网格中的确切位置(例如x行,y列),请使用以下自定义代码。
它对我起作用了,我必须得到第3行第2列的数字。网格有6列。

var gridItemsCollection = grid.FindElementsByXPath("//ListItem/Text");
List<int> allIds = HelperClass.GetColumnValuesFromGrid(gridItemsCollection, 6,2).ConvertAll(int.Parse);
var myId = allIds[2];//3rd row. 3-1

下面是函数定义。(虽然不是一个完美的代码)

public static List<string> GetColumnValuesFromGrid(IReadOnlyCollection<AppiumWebElement> gridItemsCollection, int gridColumns, int selectColumn)
    {
        List<string> list = new List<string>();
    List<string> selectList = new List<string>();

int index = selectColumn - 1;
if (index < 0 || gridItemsCollection.Count == 0)
{
return null;
}

foreach (var element in gridItemsCollection)
{
    list.Add(element.Text);
}            

while (index < list.Count)
{
    selectList.Add(list[index]);
    index += gridColumns;
}

return selectList;
}

我也必须得到最大的数字。所以,我做了以下事情。

allIds.Sort();
allIds.Reverse();
var maxId = allIds[0];
w46czmvw

w46czmvw3#

使用inspect.exe(与WinAppDriver一起下载),我发现在DataGridView中可以访问DataGridView的每个单元格中的文本,如下所示

string text = driver.FindElementByName( "<ColumnName> Row <x>" ).Text

其中ColumnName是列的名称,x是行号(从0开始)
但是,我发现上面的方法非常慢,一种更快的方法是定位DataGridView,然后使用XPath定位它的所有元素(单元格),如下所示

var Results_DGV = Driver.FindElementByAccessibilityId( "DGV_Results" );

var DGV_Cells = Results_DGV.FindElementsByXPath("//*");

for ( for loop controls ) {
     loop over the cells
}

相关问题