static void Main(string[] args)
{
Console.WriteLine("\n开始窗口程序自动化测试\n");
//启动被测试程序
string path = @"程序路径";
Process p = Process.Start(path); //自动化跟元素
AutomationElement desktop = AutomationElement.RootElement;
//查找主窗体方法1
//Thread.Sleep(2000);
//AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
AutomationElement aeForm;
//方法2
int numWaits = 0;
do
{
Console.WriteLine("等待测试窗口……"); //查找第一个自动化元素
aeForm = desktop.FindFirst(TreeScope.Children, new PropertyCondition( AutomationElement.NameProperty, "Form1")); ++numWaits; Thread.Sleep(100);
} while (null == aeForm && numWaits < 50); if (aeForm == null)
throw new NullReferenceException("找不到测试窗口!");
else
Console.WriteLine("找到测试窗口~");
//找到第一个Button
AutomationElement aeButton = aeForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "btnCalc")); //找到所有textbox控件
AutomationElementCollection aeAllTextBoxs=aeForm.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Edit)); AutomationElement aeText3 = aeAllTextBoxs[0];//集合的索引和控件位置相反
AutomationElement aeText2 = aeAllTextBoxs[1];
AutomationElement aeText1 = aeAllTextBoxs[2]; ValuePattern vpText1 = (ValuePattern)aeText1.GetCurrentPattern(ValuePattern.Pattern);
vpText1.SetValue("300");
ValuePattern vpText2 = (ValuePattern)aeText2.GetCurrentPattern(ValuePattern.Pattern);//把控件转换成Value模式
vpText2.SetValue("500");
Thread.Sleep(500); //通过InvokePattern模拟点击按钮
InvokePattern ipbtnCalc = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern);//把控件转换成Invoke模式
ipbtnCalc.Invoke();
Thread.Sleep(500);
// Console.ReadKey(); //验证实际结果和预期结果是否相符
TextPattern tpText3 = (TextPattern)aeText3.GetCurrentPattern(TextPattern.Pattern);//把控件转换成Text模式
string actual= tpText3.DocumentRange.GetText(-1);
// string actual = tpText3.DocumentRange.GetText(2);//getext中的值要>=返回结果的长度,才可以取全值.方便起见-1也可以取全值
string expect = "800";
if (actual == expect)
{
Console.WriteLine("【实际值】=" + actual);
}
else {
Console.WriteLine("【实际值】="+actual+";【期望值】"+expect);
}
Thread.Sleep(5000);
WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern);
wpCloseForm.Close();
Console.WriteLine("\n测试结束\n");
Console.ReadKey();
}

MS提供的控件Pattern
 
DockPattern                              
ExpandCollapsePattern

GridPattern                                 
GridItemPattern

InvokePattern                             
MultipleViewPattern

RangeValuePattern

ScrollPattern
ScrollItemPattern

SelectionPattern

SelectionItemPattern

TablePattern
TableItemPattern

TextPattern
TogglePattern

TransformPattern
ValuePattern

WindowPattern

UIAutomation 测试winForm的更多相关文章

  1. 测试winform自动悬浮

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  2. 测试winform程序到树莓派运行

    啥也不说了,都在下图中了.winform可以在树莓派上跑了

  3. Winform开发框架之存储过程的支持--存储过程的实现和演化提炼(2)

    本篇继续上篇<Winform开发框架之存储过程的支持--存储过程的实现和演化提炼(1)>来对Winform开发框架之存储过程的支持进行介绍,上篇主要介绍了SQLServer和Oracle两 ...

  4. 2015年iOS测试现状

    本文由 伯乐在线 - nathanw 翻译,dopcn 校稿.未经许可,禁止转载! 英文出处:www.mokacoding.com.欢迎加入翻译小组. 几周前,我决定将将我在 mokacoding 上 ...

  5. 如何使用UIAutomation进行iOS 自动化测试(Part I)

    转自:http://www.cnblogs.com/vowei/archive/2012/08/10/2631949.html 写在前面 研究iOS的自动化测试也有些日子了,刚开始的时候,一直苦于找不 ...

  6. Uiautomator1.0与Uiautomator2.0测试项目搭建与运行原理

    Uiautomator是Android原生测试框架,可以用于白盒接口测试也可以用于UI自动化测试,Uiautomator分1.0版本与2.0版本,它们都是基于UiAutomation的测试框架,都是通 ...

  7. 跟UI自动化测试有关的技术

    大家都知道,针对UI的自动化技术一般要支持下列的东西: 1. 识别窗口   能够识别尽量多的窗口种类,支持尽量多的UI技术.比如Win32.WinForm.WPF以及WebPage(这个比较特殊,确切 ...

  8. 第一次做socket的一些心得

    理论什么的bilibala的就是自己百度吧 推荐一篇不错的关于socket的文章 http://www.cnblogs.com/sunway/archive/2010/01/29/1659074.ht ...

  9. 用Ghostscript API将PDF格式转换为图像格式(C#)

    原文:用Ghostscript API将PDF格式转换为图像格式(C#) 由于项目需要在.net下将pdf转换为普通图像格式,在网上搜了好久终于找到一个解决方案,于是采用拿来主义直接用.来源见代码中注 ...

随机推荐

  1. Bash Game hdu 1846

    (一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者 ...

  2. python的scribe client

    在网上找了一个python的scribe client使用方法 依赖的模块: pip install facebook-scribe pip install thrift 代码例子: #!/usr/b ...

  3. 删除文件时提示,你需来自SYSTEM的权限

    1. 提示如下 2. 对要删除的文件操作如下 2.1 为删除的文件添加本地账户 2.2 提示如下,多点几次继续就好 2.3 给本地账户添加完全控制权限

  4. MySQL存储引擎优化

    如何在两种存储引擎中进行选择? ① 是否有事务操作?有,InnoDB. ②是否存储并发修改?有,InnoDB. ③是否追求快速查询,且数据修改较少?是,MyISAM. ④是否使用全文索引?如果不引用第 ...

  5. 「JSOI2015」地铁线路

    「JSOI2015」地铁线路 传送门 第一问很简单:对于每条线路建一个点,然后所有该条线路覆盖的点向它连边,权值为 \(1\) ,然后它向所有线路上的点连边,权值为 \(0\) . 然后,跑一边最短路 ...

  6. js实现上移下移

    直接上代码 //上移 var $up = $(".up") $up.click(function () { var $tr = $(this).parents("tr&q ...

  7. JavaScript自学笔记(1)---表单验证,let和const,JSON文件

    今天开个JS自学笔记,本身JS的语法很简单,如果学过Java或者C系的都很容易,就不讨论了.主要是讨论实际应用的问题. 1.表单验证: a.html自动验证: HTML 表单验证可以通过浏览器来自动完 ...

  8. 日期格式在ios中的兼容性

    在IOS中支持 2017/3/2 这种格式的日期 不支持2017-3-2日期 /** * 返回兼容ios.android的日期时间格式 * @param dateTime String * @retu ...

  9. 推荐算法之---FM算法;

    一,FM算法: 1,逻辑回归上面进行了交叉特征.算法复杂度优化从O(n^3)->O(k*n^2)->O(k*n). 2,本质:每个特征都有一个k维的向量,代表的是每个特征都有k个不可告人的 ...

  10. Apache的虚拟主机功能(基于IP地址、基于虚拟主机、基于端口)

    1. 安装Apache服务程序(系统用户,1-199之间) 第一步:在虚拟机软件里选中光盘镜像: 第二步:将光盘设备挂载到/media/cdrom目录 输入:mkdir -p /media/cdrom ...