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. Git 新项目关联到远程仓库

    最近前端学到小有成果,准备写一个新项目放在githup,结果没有提前在仓库创建项目,现在我把写好的项目推送到远程gitHup 1.先初始化本地仓库 Git init : 这样在项目里面都创建了一个隐藏 ...

  2. UVA 10912 Simple Minded Hashing

    题意就略了.刚一看被数据吓住了.看到字符要求严格递增.那么如果字串长大于26那必然方案数目为0:同时1+2+3....+24+25+26=351如果大于这个数也是不可能的 令dp[i][j][k]表示 ...

  3. jquery验证前端页面

    一共三个页面 jquery.html文件(前端页面,jquery验证用户信息) jquerytest.php文件(后台处理页面) jquerydb.php文件(数据库) 数据表结构 jquery.ht ...

  4. picker(拖拽上下拉动的选项)

    [b]新版本更新:鼠标上下拖动选择内容:http://hiuman.iteye.com/blog/2353563[/b] (如有错敬请指点,以下是我工作中遇到并且解决的问题) 一开始搜这个内容的时候, ...

  5. python 独立环境安装

    python 即使是单独编译安装的,库文件的安装还是会与其它python的库存放到相同的地方 使用同版本库不会有问题,但是需要升级库的时候,就会出现冲突,导致依赖这个旧库的python出现问题 这时候 ...

  6. pythontips(2):hasattr的用法

    class Xiaorui: def __init__(self): self.name = 'xiaorui' def setName(self, name=''): if name.strip() ...

  7. 开发框架 springBoot

    1.多个环境的配置文件 在application.yml 中配置需要调用的配置文件 spring: profiles: active: dev 运行方式的,先运行application.yml 再根据 ...

  8. Ansible 删除多个文件或目录

    翻译和转载该网页内容 http://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/ 背景 ans ...

  9. Gmail进程信息转储分析工具pdgmail

    Gmail进程信息转储分析工具pdgmail   进程信息转储(Process Memory Dump)是数字取证的重要方式.通过分析对应进程的信息转储,可以获取大量的信息.Kali Linux提供一 ...

  10. 深入JS正则先行断言

    这里是 Mastering Lookahead and Lookbehind 文章的简单翻译,这篇文章是在自己搜索问题的时候stackoverflow上回答问题的人推荐的,看完觉得写得很不错.这里的简 ...