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

最新文章

  1. beaglebone black 固定IP上网(ubuntu16.04,console)
  2. ORA-00257 archiver error 处理思路
  3. CocoaPods常用终端命令及Profile文件简单介绍
  4. 屠蛟之路_集木成舟_ForthDay
  5. Android应用字体更改
  6. Effective Java 09 Always override hashCode when you override equals
  7. hdu - 2216 Game III &amp;&amp; xtu 1187 Double Maze (两个点的普通bfs)
  8. 【高精度】Vijos P1010 清帝之惑之乾隆
  9. JAVA-POI实现EXCEL的读写
  10. Android开源中国客户端学习 (自定义View)左右滑动控件ScrollLayout
  11. 【PHP系列】PHP推荐标准之PSR-4,自动加载器策略
  12. CF55D Beautiful numbers
  13. 初始化HTML样式(转载)
  14. http请求流程
  15. String类型的方法总结
  16. Flex自定义组件、皮肤,并调用
  17. SQL SERVER 2012/ 2014 分页,用 OFFSET,FETCH NEXT改写ROW_NUMBER的用法(转)
  18. 【Python】tuple and list 练习
  19. Backup Log
  20. python入门13 集合set

热门文章

  1. mybatis--多对一关联
  2. Python 多任务(进程) day1(3)
  3. 数据库程序接口——JDBC——API解读第一篇——建立连接的核心对象
  4. 【音乐欣赏】《Fake》 - The Tech Thieves
  5. 列表与数组 Perl入门第三章
  6. springBoot日志快速上手简单配置
  7. AbstractQueuedSynchronizer(AQS) 超详细原理解析
  8. contextField 键盘只允许输入数字和小数点,并且现在小数点后位数
  9. 学习JavaScript数据结构与算法---前端进阶系列
  10. Springboot学习:底层依赖与自动配置的原理