1. 首先建立一个待测试的winform程序,即UI Automation的服务端。

下面是button事件处理程序。

 private void CalculateButton_Click(object sender, EventArgs e)
{
int num1 = ;
int num2 = ;
if (!int.TryParse(textBox1.Text.Trim(), out num1) || !int.TryParse(textBox2.Text.Trim(), out num2))
{
MessageBox.Show("Please input a correct number!");
return;
}
textBox3.Text = (num1 + num2).ToString();
}

  2. 建立一个测试程序,做UI Automaion的客户端。

  添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll

public static void TestWinFrom(int num1, int num2)
{
string winfromPath = @"C:\Users\v-lix\Documents\Visual Studio 2010\Projects\UIAutomationDemo\WinformForTesting\bin\Debug\WinformForTesting.exe";
Console.WriteLine("Begin WinForm UIAutomation test runn");
Console.WriteLine("Launching WinFormTest application");
Process p = Process.Start(winfromPath);
Thread.Sleep(TimeSpan.FromSeconds());
Console.WriteLine("Get desktop");
var desktop = AutomationElement.RootElement; // Method 1 by windowhandle
AutomationElement testForm = AutomationElement.FromHandle(p.MainWindowHandle); // Method 2 by name property
//PropertyCondition proCon = new PropertyCondition(AutomationElement.NameProperty,"TestForm1");
//testForm = desktop.FindFirst(TreeScope.Children,proCon); if (testForm == null)
{
Console.WriteLine("Could find testform!");
return;
} Console.WriteLine("Try to find the button control in testForm");
AutomationElement button = testForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculate")); Console.WriteLine("Try to find all the textbox in testform!");
AutomationElementCollection aeAllTextBoxes = testForm.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); // 控件初始化的顺序是先初始化后添加到控件
// this.Controls.Add(this.textBox3);                 
// this.Controls.Add(this.textBox2);
// this.Controls.Add(this.textBox1); AutomationElement aeTextBox1 = aeAllTextBoxes[];
AutomationElement aeTextBox2 = aeAllTextBoxes[];
AutomationElement aeTextBox3 = aeAllTextBoxes[]; Console.WriteLine("Settiing input to '30'");
//set textboxs' value by ValuePattern
ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern);
vpTextBox1.SetValue(num1.ToString());
ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern);
vpTextBox2.SetValue(num2.ToString());
Thread.Sleep();
Console.WriteLine("Clickinig on button1 Button.");
InvokePattern ipButton = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
ipButton.Invoke();
Thread.Sleep(); ValuePattern vpTextBox3 = (ValuePattern)aeTextBox3.GetCurrentPattern(ValuePattern.Pattern);
if (int.Parse(vpTextBox3.Current.Value.Trim()) != (num1 + num2))
{
Console.WriteLine("Failed");
}
else
{
Console.WriteLine("Pass");
} Thread.Sleep();
//close testform
WindowPattern wpCloseForm = (WindowPattern)testForm.GetCurrentPattern(WindowPattern.Pattern);
wpCloseForm.Close();
}

使用UI Automation实现自动化测试--2的更多相关文章

  1. 使用UI Automation实现自动化测试--1-4

    Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...

  2. 使用UI Automation实现自动化测试--5-7

    使用UI Automation实现自动化测试--5 (Winfrom和WPF中弹出和关闭对话框的不同处理方式) 在使用UI Automation对Winform和WPF的程序测试中发现有一些不同的地方 ...

  3. 使用UI Automation实现自动化测试--1

    Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active ...

  4. 使用UI Automation实现自动化测试 --工具使用

    当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...

  5. 使用UI Automation实现自动化测试 --微软提供的控件Pattern

    微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...

  6. 从UI Automation看Windows平台自动化测试原理

    前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...

  7. UI Automation 简介

    转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...

  8. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  9. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

随机推荐

  1. symfony3常用记忆

    1.控制器里获取当前用户信息 $user = $this->getUser(); 2.判断当前用户是否登录 // yay! Use this to see if the user is logg ...

  2. Selenium2 鼠标悬停效果实现

    对一些js控件,鼠标悬停的时候出发下拉层的实现 1.使用Action public void moveToElement(WebDriver driver, By locator) { Actions ...

  3. List<?>和List<Class<?>>区别及泛型相关

    ?表示是任意类型,但是编译器不能确定他是什么类型,所以你add的时候什么参数也不能传给它Class<?>表示任意类型的Class对象,list里面可以添加任何类型的Class对象,其它的对 ...

  4. 读取文本文件时<U+FEFF> 导致的奇怪问题

    项目中经常会从一些文本文件中读取数据进行业务处理,最近遇到一个问题,另外一个部门提供一个txt文本给我们进行业务处理,当我们使用字符流读取文本之后,处理时,发现第一行数据无法匹配,其他数据可以正常处理 ...

  5. 出现函数重载错误call of overloaded ‘printfSth(double)’ is ambiguous

    class C: { public: void printfSth(int i) { cout<<"C::printfSth(int i):"<<i< ...

  6. Switch能否用string做参数

    在Java5以前,switch(expr)中,exper只能是byte,short,char,int类型(或其包装类)的常量表达式. 从Java5开始,java中引入了枚举类型,即enum类型. 从J ...

  7. delphi 开机自动运行代码

    unit Unit1;//download by http://www.codefans.netinterface uses  Windows,Registry, Messages, SysUtils ...

  8. 51nod 1086 背包问题 V2 【二进制/多重背包】

    1086 背包问题 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...

  9. 用fiddler测试移动端翻页

    大家在移动端是怎么测试翻页的,肯定都是下拉或上滑吧,我也是这样测试的 但如果你要验证数据是否与pc端数据一致时,可能是第一页,第二页看看,或最后几页数据看看,在pc端看简单,直接点击最后一页就行,在移 ...

  10. RMQ ---- ST(Sparse Table)算法

    [概述]      RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返 ...