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. http://twitter.github.com/bootstrap/

    原文发布时间为:2012-05-22 -- 来源于本人的百度文章 [由搬家工具导入] http://twitter.github.com/bootstrap/

  2. 2018多校第三场 hdu6331 M :Walking Plan

    题目链接 hdu6331 自我吐槽,这场多校大失败,开局签到因输入输出格式写错,wa了3发.队友C题wa了1个小时,还硬说自己写的没错,结果我随便造了个小数据,他都没跑对.然后跑对了后又进入了无限的卡 ...

  3. Sqlite 修改字段的名称。

    Sqlite 不支持直接修改字段的名称. 我们可以使用别的方法来实现修改字段名. 1.修改原表的名称 ALTER TABLE table RENAME TO tableOld; 2.新建修改字段后的表 ...

  4. selenium运行js下载文书网的文件

    from selenium import webdriver driver=webdriver.Chrome() driver.get("http://wenshu.court.gov.cn ...

  5. hadoop使用supervisord

    #安装 wget https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b ...

  6. (3) python--matplotlib

    (一)1.如何绘制散点图 import numpy as np import matplotlib.pyplot as plt # 如何绘制散点图 # 先随机生成数据 x = np.array(ran ...

  7. EntityFramework之多对多关系(四)

    上篇介绍了一对多关系,下面介绍下多对多关系代码编写. 1.新建model实体,User是用户类,Role是角色类,由于是多对多关系,必须得有一个中间类,所以产生了UserRole类 public cl ...

  8. POJ 1862 Stripies【哈夫曼/贪心/优先队列】

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18198   Accepted: 8175 Descrip ...

  9. navicat连接MySQL8.0.11提示2059错误

    错误原因:mysql加密规则的改变: mysql加密规则:mysql_native_password      mysql8之前的版本   caching_sha2_password     mysq ...

  10. App Class Loader

    Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...