Test Automation Frame(TAF)

本文介绍了Test Automation Frame (TAF),一个支持自动化UI测试的框架,能够操控第三方软件。针对WPF应用,控件通过实现AutomationPeer和标准控制模式进行交互。文章详细讲解了如何查找和操作控件,包括使用Inspect.exe获取控件信息,通过AutomationProperties.AutomationId或Name定位,并提供了微软官方文档链接以获取更多查找和操作控件的方法。此外,还讨论了在特定情况下如华泰证券登录时遇到的控件查找挑战和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview (没怎么看)

https://www.codeproject.com/Articles/141842/Automate-your-UI-using-Microsoft-Automation-Framew

(里面的示例工程代码可以下载,代码是一个test project。如果转project失败,可自己再建一个,将代码和xml文件重新导入,注意要将xml文件属性改成"Embedded Resource"。另外注意GetManifestResourceStream()函数里资源的命名空间。

 

简单概括:

 

  • 支持自动化UI测试
  • 可以操控第三方软件

对WPF来说,所有控件是通过实现抽象类AutomationPeer来实现的。每个AutomationPeer都会实现 “standard control patterns”。就是利用这些Pattern来"Invoke", "Select item", "Get Value", “Expand Collapse”等操作。这些Pattern是固定的,不能自定义。操作系统只支持这些Pattern。

步骤:

1. 先找控件:一般通过名字去找。

如何得知到名字呢? 使用Inspect.exe!。 vs装好后,就会有此文件。

https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

_calculatorProcess = Process.Start("Calc.exe");
_calculatorAutomationElement = AutomationElement.RootElement.FindFirst
	(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, 
	"Calculator")); 

        public AutomationElement GetFunctionButton(string functionName)
        {
            AutomationElement functionButton = _calculatorAutomationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, functionName));

            if (functionButton == null)
            {
                throw new InvalidOperationException("No function button found with name: " + functionName);
            }

            return functionButton;
        }

 

2, 找到控件想执行的pattern,找到后再执行。例如下面执行的Invoke操作

 

        public InvokePattern GetInvokePattern(AutomationElement element)
        {
            return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        }
GetInvokePattern(GetFunctionButton(Functions.Clear)).Invoke();

在sample代码中还有菜单展开,执行的操作是ExpandCollapsePattern。 

        private ExpandCollapsePattern FindMenu(CalculatorMenu menu)
        {
            AutomationElement menuElement = _calculatorAutomationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, menu.ToString()));
            ExpandCollapsePattern expPattern = menuElement.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            return expPattern;
        }

 

 

在WPF中,如果控件不使用x:Name属性,很难找到该控件。如果使用,Name的值会自动设置到AutomationProperties.AutomationId,然后可用AutomationProperties.AutomationId找到该控件。当然也可以像下面这样,两个属性都指定。

 

 

<TextBox Text="{Binding Telephone}" x:Name="See_My_Name" AutomationProperties.AutomationId="See My AutomationId!"/>

TAF也支持WPF,实现方式类似,可参考:

https://www.codeproject.com/Articles/33049/WPF-UI-Automation

 

2019-12-11日更新

昨天做个自动登录华泰证券,发现有时找控件比较难,以前大多按name找,但可能会重复而且慢,有时控件还没有name属性。另外automaitonID还有重复的情况。

找控件可以参考:

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/obtaining-ui-automation-elements?redirectedfrom=MSDN

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/expose-the-content-of-a-table-using-ui-automation

如果由自身进程启动:

    Process p = Process.Start(exe, filename);

    // targetApp --> the root AutomationElement
    AutomationElement targetApp =
        AutomationElement.FromHandle(p.MainWindowHandle);

创建condition,用AndCondition去查找。

可以FindFirst,可以FindAll

可以子节点查找,可以同级查找

华泰软件的automaitonID还有重复的情况,今天可以用findall方法试试。

 

找控件支持的pattern:GetSupportedPatterns 

 

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/get-supported-ui-automation-control-patterns

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-control-patterns-how-to-topics

 

所有的pattern:

https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-control-patterns

例如:给textbox设置text用的是ValuePattern

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值