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. python 网页中文显示Unicode码

    print repr(a).decode("unicode–escape") 注:a是要输出的结果,

  2. ISE-Backup Data Type

    Cisco ISE allows you to back up data from the Primary PAN and from the Monitoring node. Back up can ...

  3. angular 输入框自动绑定值最长为16位,超过16位则会报错

    最近发现angular在使用input输入框的ng-model绑定scope变量的时候,发现,输入长串的数字将会出错.代码如下: <html> <head> <meta ...

  4. kali Linux 2020.1最新安装教程,亲身尝试,绝对能帮你安装好!不是root、没有桌面、中文乱码、下载太慢、ssh链接等问题!

    既然已经开始研究kali Linux,小编就认为在下已经有了一定的基础.当然小编也是个小白用户.本人用的是Vmware虚拟机,这里只说一点,内存我选择的是4g因为这个包含桌面,所以稍微大一点.Linx ...

  5. Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:

    创建数据库表格式: CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, P ...

  6. java判断两个时间相差得天数

    方法一:通过Calendar类得日期比较,在这需要考虑闰年和平年,也要考虑跨年份 /** * date2比date1多的天数 * @param date1 * @param date2 * @retu ...

  7. 一文搞懂vim复制粘贴

    转载自本人独立博客https://liushiming.cn/2020/01/18/copy-and-paste-in-vim/ 概述 复制粘贴是文本编辑最常用的功能,但是在vim中复制粘贴还是有点麻 ...

  8. django+vue基础框架:django one对one格式

    创建app:python manage.py startapp  app01(这里的app01是指名字,可以是a或b等等) 生成迁移文件:python manage.py makemigrations ...

  9. Python连载59-HTTP首部字段和消息头,Thinker简介

    一.首部字段或者消息头 1.下面几个类型都是请求的: User-Agent:关于浏览器和它平台的消息,如Mozilla5.0 Accept:客户端能处理的页面的类型,如text/html Accept ...

  10. tensorflow之tensorboard

    参考https://www.cnblogs.com/felixwang2/p/9184344.html 边学习,边练习 # https://www.cnblogs.com/felixwang2/p/9 ...