Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检索Viewbox/Canvas/Path元素进行编码UI测试?_C#_Wpf_Ui Automation_Coded Ui Tests - Fatal编程技术网

C# 如何检索Viewbox/Canvas/Path元素进行编码UI测试?

C# 如何检索Viewbox/Canvas/Path元素进行编码UI测试?,c#,wpf,ui-automation,coded-ui-tests,C#,Wpf,Ui Automation,Coded Ui Tests,在WPF应用程序上执行编码UI测试时,可以通过以下方式找到我需要的WPF按钮: WpfButton button = new WpfButton(mainWindow); button.SearchProperties[WpfButton.PropertyNames.AutomationId] = "btn"; button.WindowTitles.Add("MainWindow"); 在此初始化之后,我可以成功地执行任何检查和验证 当我试图检查ViewBox元素的状态时,问题就开始了,该元

在WPF应用程序上执行编码UI测试时,可以通过以下方式找到我需要的WPF按钮:

WpfButton button = new WpfButton(mainWindow);
button.SearchProperties[WpfButton.PropertyNames.AutomationId] = "btn";
button.WindowTitles.Add("MainWindow");
在此初始化之后,我可以成功地执行任何检查和验证

当我试图检查ViewBox元素的状态时,问题就开始了,该元素包含画布,而画布又包含路径。这些元素中没有一个具有来自
Microsoft.VisualStudio.TestTools.UITesting.WpfControls
命名空间的模拟类型。经过短暂的调查,我发现这些类型也没有覆盖
OnCreateAutomationPeer
方法

那么,检索画布、ViewBox或UI测试路径最方便的方法是什么


也许,我错过了Microsoft.VisualStudio.TestTools.UITesting.WpfControls命名空间中的兼容类型,或者,也许,我应该派生自定义类型(例如,从画布)并覆盖其中的
OnCreateAutomationPeer
方法,然后为我的DerivedCanvas类创建一个自动化对等体?我是编码UI测试的新手,所以如果第二个解决方案解决了问题,那么如何实现它呢

我找到了答案。看起来很简单。首先,一个新的
AutomatisableCanvas
类应该从
画布
派生:

public class AutomatisableCanvas : Canvas
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new CanvasAutomationPeer(this);
    }
}
其次,一个新的
CanvasAutomationPeer
类应该从
FrameworkElementAutomationPeer
派生:

class CanvasAutomationPeer : FrameworkElementAutomationPeer
{
    public CanvasAutomationPeer(Canvas owner)
        : base(owner) { }

    protected override AutomationControlType GetAutomationControlTypeCore()
    {
        return AutomationControlType.Custom;
    }
}
现在可以简单地找到
AutomatisableCanvas
控件,如下所示:

WpfCustom canvas = new WpfCustom(mainWindow);
canvas.SearchProperties[WpfCustom.PropertyNames.AutomationId] = "an AutomationId you've specified for an AutomatisableCanvas instance";
canvas.WindowTitles.Add("MainWindow");

出于好奇,为什么不使用UI地图?至少,您可以研究生成的代码,看看它是如何到达这些元素的。@DanielMann您是指由编码的UI测试生成器实用程序生成的代码吗?不幸的是,它无法“看到”画布。例如,如果我单击窗口中包含的画布,它将只识别窗口。但是对于诸如TextBlock或Button之类的元素,一切都可以。