我的 WPF 自定义控件具有内部元素。就我而言,我的自定义控件是网格,内部元素是单元格。您可以在下面找到 AutomationPeer 实现,
public class GridAutomationPeer : FrameworkElementAutomationPeer, IGridProvider
{
public virtual GridModel Model
{
get
{
var grid = this.Owner as GridBase;
return grid.Model;
}
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.DataGrid;
}
protected override string GetClassNameCore()
{
return this.Owner.GetType().Name;
}
protected override List<AutomationPeer> GetChildrenCore()
{
var peers = new List<AutomationPeer>();
foreach (var kvp in this.cellPeers)
{
peers.Add(kvp.Value);
}
return peers;
}
public override object GetPattern(PatternInterface patternInterface)
{
switch (patternInterface)
{
case PatternInterface.Grid:
case PatternInterface.Selection:
case PatternInterface.Table:
return this;
return base.GetPattern(patternInterface);
}
}
int IGridProvider.ColumnCount
{
get
{
return this.Model.ColumnCount;
}
}
IRawElementProviderSimple IGridProvider.GetItem(int row, int column)
{
if (row < this.Model.RowCount && column < this.Model.ColumnCount)
{
var cellPeer = this.GetOrCreateCellPeer(row, column);
if (cellPeer != null)
{
var item = this.ProviderFromPeer(cellPeer);
return item;
}
}
return null;
}
int IGridProvider.RowCount
{
get
{
return this.Model.RowCount;
}
}
}
我的问题是,只有在打开 inspect.exe 时,它才能在 Winium 测试脚本中正确识别。如果 inspect.exe 没有打开,Winium 会抛出“Element not found”异常。
下面是我的测试脚本,
try
{
var grid = driver.FindElementById("grid");
var cell = grid.FindElement(By.Id("6,1"));
if (cell.Text != "Movie Poster")
Assert.Fail("Cell value is not Movie Poster");
driver.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
异常详情,
请为此建议我任何解决方案。