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. 洛谷P1044栈(DP)

    题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即poppoppop(从栈顶弹出一个元素)和pushpushpush(将一个元素进栈) ...

  2. 【Hibernate 懒加载】debug模式懒加载获取空数据

    <many-to-one name="department" class="k.domain.Department" column="depar ...

  3. Linux shell lrzsz上传下载命令

    安装lrzsz做上传下载 工具使用secretCRT yum install -y lrzsz 1. 服务器<发送>文件,使用命令sz 2. 服务器<接收>文件,使用命令rz

  4. Go非缓冲/缓冲/双向/单向通道

    1. 非缓冲和缓冲 package main import ( "fmt" "strconv" ) func main() { /* 非缓冲通道:make(ch ...

  5. JDK-13下载安装及环境变量配置

    1.JDK-13下载安装及环境变量配置 直接去官网下载 附下载链接:https://www.oracle.com/technetwork/java/javase/downloads/index.htm ...

  6. 解决docker: error pulling image configuration: Get https://registry-1.docker.io/v2/library/mysql/: TLS handshake timeout.

    出现这个问题,一般的原因是无法连接到 docker hub,通过: systemctl stop docker echo "DOCKER_OPTS=\"\$DOCKER_OPTS ...

  7. composer update 或者 composer install提示killed解决办法

    出现此原因大多因为缓存不足造成,在linux环境可增加缓存解决. free -mmkdir -p /var/_swap_cd /var/_swap_#Here, 1M * 2000 ~= 2GB of ...

  8. MyBatis-Spring整合之方式2

    提前叨叨:此方法优化了上一个方式的事务支持,同时简化了一个bean的配置 1.在方式1的基础上修改UserDaoImp文件,改用使用继承SqlSessionDaoSupport的方式.代码如下: pu ...

  9. 《实战Java高并发程序设计》读书笔记四

    第四章 锁的优化及注意事项 1.锁性能的几点建议 减小锁持有时间: 系统持有锁时间越长锁竞争程度就越激烈,只对需要同步的方法加锁,可以减小锁持有时间进而提高锁性能. 减少锁的持有时间有助于降低锁冲突的 ...

  10. 从QC到QA

    QC遇到了什么无法逾越的障碍 我们公司的主要业务是项目外包,一般的项目都在2-3个月的周期,采用瀑布模式.这种模式本身是相对简单,且十分成熟的模式.但是在实际的工作中,我们还是遇到了前所未有的挑战. ...