AutomationPeer

本文介绍了如何在WPF中为第三方控件ActiPro的TreeListBox创建AutomationPeer,以支持微软的TAF技术进行自动化测试。通过派生自己的TreeListBox,选择适当的AutomationPeer基类并重写关键方法如GetClassNameCore和GetChildrenCore,实现树形结构的自动化支持。遇到的问题包括第三方控件类型不匹配导致的自动化难度,以及使用Inspect.exe工具进行验证。
摘要由CSDN通过智能技术生成

微软的TAF技术,可以实现自动化测试,或是对其他进程进行UI操作。https://blog.csdn.net/jfyy/article/details/80700977

参考:https://www.codeproject.com/Articles/172391/UIAutomation-Coded-UI-Tests-AutomationPeer-and-WPF

https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/ui-automation-of-a-wpf-custom-control

 

为了在WPF里支持这套技术,微软使用了Peer对象,并为其标准控件(button,textblock)制作了对应的Peer类,例如ButtonAutomationPeer。并在控件的protected override AutomationPeer OnCreateAutomationPeer()方法里,实例化对应的Peer类。

 

但如果是我们制作的Customize控件,或使用第三方的控件,因为没有对应地制作Peer类,就不能支持TAF。下面的例子就是我在研究如何为第三方ActiPro的TreeListBox控件,制作Peer的过程。

 

1,先如下派生自己的TreeListBox,并实现OnCreateAutomationPeer()

    public class AutomationTreeListBox : TreeListBox
    {
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new TreeListAutomationPeer(this);
        }
    }

只要我们能够制作出TreeListAutomationPeer,就能完美支持TAF。

 

2, TreeListAutomationPeer基类的选择

没必要重复做轮子,选择恰当的AutomationPeer基类会减少很多工作量。

如果是制作的是一个简单Control, 可使用FrameworkElementAutomationPeer。
如果是制作一个TabControl,可使用TabControlAutomationPeer。

所以我第一时间想选择TreeViewAutomationPeer作为基类,可第三方ActiPro的TreeListBox控件是派生于ItemsControl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值