【问题标题】:How can Viewbox/Canvas/Path elements be retrieved for Coded UI testing?如何检索 Viewbox/Canvas/Path 元素以进行编码 UI 测试?
【发布时间】:2014-06-20 15:29:39
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 出于好奇,您为什么不使用 UI 地图?至少,您可以调查生成的代码以了解它是如何到达这些元素的。
  • @DanielMann 您是指 Coded UI Test Builder 实用程序生成的代码吗?不幸的是,它不能“看到”画布。例如,如果我单击 Window 中包含的 Canvas,它将仅识别 Window。但是对于 TextBlock 或 Button 这样的元素,一切都会好的。

标签: c# wpf ui-automation coded-ui-tests


【解决方案1】:

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

public class AutomatisableCanvas : Canvas
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new CanvasAutomationPeer(this);
    }
}

其次,应该从FrameworkElementAutomationPeer派生一个新的CanvasAutomationPeer类:

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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2023-03-23
    • 2021-07-22
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多