UIAutomation测试WPF桌面程序

人狠话不多~!

  • 做一个WPF程序用于计算两个数之和,用UIAutomation测试输入两个数字,后WPF计算的程序是否为预期的数字。
  • 创建一个名称为WPFUIAutomation的WPF项目,在MainWindows.xaml的窗体中做如下布局及后台代码。
<Window x:Class="WPFUIAutomation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFUIAutomation"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
        <TextBox x:Name="tbNo1" Height="30" Width="50" Margin="10"/>
        <TextBlock Text="+"/>
        <TextBox x:Name="tbNo2" Height="30" Width="50" Margin="10"/>
        <TextBlock Text="="/>
        <TextBox x:Name="tbNo3" Height="30" Width="50" Margin="10"/>
        <Button x:Name="btnSum" Click="BtnSum_OnClick" Height="30" Width="50" Content="计算"/>
    </StackPanel>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFUIAutomation
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnSum_OnClick(object sender, RoutedEventArgs e)
        {
            int i = int.Parse(tbNo1.Text);
            int j = int.Parse(tbNo2.Text);

            tbNo3.Text = (i + j).ToString();
        }
    }
}
  • 创建名称为WPFUIAutomationConsole的控制台项目,引入UIAutomationClient.dll、UIAutomationClientsideProviders.dll、UIAutomationProvider.dll、UIAutomationTypes.dll
  • 将WPFUIAutomation的输出路径修改为WPFUIAutomationConsole的bin目录下。WPFUIAutomationConsole的代码如下。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using AppDomain = System.AppDomain;
using System.Windows.Automation;

namespace WPFUIAutomationConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取exe程序
            string path = AppDomain.CurrentDomain.BaseDirectory + "WPFUIAutomation.exe";
            //运行exe程序
            Process p = Process.Start(path);
            //自动化根元素
            AutomationElement aeRoot=AutomationElement.RootElement;
            //等待exe程序启动
            Thread.Sleep(2000);
            //获取主窗体
            AutomationElement aeFrom=AutomationElement.FromHandle(p.MainWindowHandle);
            //等待计数
            int waitIndex = 0;
            //获得主窗体对象的引用
            do
            {
                Console.WriteLine("第{0}查找主窗体", waitIndex);
                //查找第一个自动化元素
                aeFrom = aeRoot.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.NameProperty, "MainWindow"));

            } while (aeFrom==null && waitIndex<50);
            //没有获取到主窗体
            if (aeFrom==null)
            {
                throw new NullReferenceException("未找到窗体。");
            }
            else
            {
                Console.WriteLine("找到窗体。");
            }
            //Name为btnSum的控件
            AutomationElement aeButton = aeFrom.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "btnSum"));
            //Name为tbNo1的TextBox控件
            AutomationElement aeTextBo1 = aeFrom.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "tbNo1"));
            //Name为tbNo2的TextBox控件
            AutomationElement aeTextBo2 = aeFrom.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "tbNo2"));
            //Name为tbNo3的TextBox控件
            AutomationElement aeTextBo3 = aeFrom.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.AutomationIdProperty, "tbNo3"));
            //设置第一个TextBox的值为25
            ValuePattern vp1 = (ValuePattern) aeTextBo1.GetCurrentPattern(ValuePattern.Pattern);
            vp1.SetValue("25");
            //设置第二个TextBox的值为25
            ValuePattern vp2 = (ValuePattern)aeTextBo2.GetCurrentPattern(ValuePattern.Pattern);
            vp2.SetValue("25");
            //让子弹飞一会
            Thread.Sleep(1500);
            //通过InvokePattern,实现模拟点击按钮
            InvokePattern clickButton = (InvokePattern) aeButton.GetCurrentPattern(InvokePattern.Pattern);
            clickButton.Invoke();
            //让子弹飞一会
            Thread.Sleep(1500);
            //获取第三个TextBox的值
            ValuePattern vp3 = (ValuePattern)aeTextBo3.GetCurrentPattern(ValuePattern.Pattern);
            string result = vp3.Current.Value;

            //判断是否为预期的60
            if (result.Equals("60"))
            {
                Console.WriteLine("验证通过。");
            }
            else
            {
                Console.WriteLine("验证失败,测试结果为{0}。", result);
            }

            Thread.Sleep(3000);
            //关闭被测试程序
            WindowPattern closeForm = (WindowPattern) aeFrom.GetCurrentPattern(WindowPattern.Pattern);
            closeForm.Close();

            Console.WriteLine("测试程序已关闭。");
            Console.ReadLine();
        }
    }
}

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读