gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:05:33 25 4
gpt4 key购买 nike

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

WpfButton button = new WpfButton(mainWindow);
button.SearchProperties[WpfButton.PropertyNames.AutomationId] = "btn";
button.WindowTitles.Add("MainWindow");

在此初始化之后,我可以成功执行任何检查和验证。

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

那么,检索 Canvas 或 ViewBox 或用于 UI 测试的 Path 最方便的方法是什么?

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

最佳答案

我找到了答案。看起来很简单。首先,应该从 Canvas 派生一个新的 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");

关于c# - 如何检索 Viewbox/Canvas/Path 元素以进行编码 UI 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24330788/

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