zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C# 模拟界面点击/UI自动化测试

2023-09-27 14:26:39 时间

有一些UI自动化测试框架,能够实现自动化测试。

本文介绍Peer(微软的TAF技术),也可以实现自动化测试,或是对其他进程进行UI操作。下面是案例~

在界面上添加俩个按钮:

 

 并处理相应的点击事件:

 1     private void Test1Button_OnClick(object sender, RoutedEventArgs e)
 2     {
 3         ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(Test2Button);
 4         IInvokeProvider invokeProvider = buttonPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
 5         invokeProvider.Invoke();
 6     }
 7 
 8     private void Test2Button_OnClick(object sender, RoutedEventArgs e)
 9     {
10         MessageBox.Show("Test2");
11     }

点击Test1按钮,弹出“Test2”提示框:

 

 通过new ButtonAutomationPeer(Test2Button)对按钮2引用,然后GetPattern()完成了自动化关联,Invoke()激活按钮的操作。

以上是一个最简单的UI自动化案例介绍,更多的可以查看System.Windows.Automation.Peers官方文档

 

关键字:Test Automation Frame、UI自动化