自动化测试小实例
2015-12-17 16:13
369 查看
注:查看窗口句柄工具下载地址:http://download.csdn.net/detail/chao88552828/9364845
<span style="white-space:pre"> </span>private void button3_Click(object sender, EventArgs e) { Process.Start(@"calc.exe"); //获取跟节点 AutomationElement rootElement = AutomationElement.RootElement; //获取计算机窗体节点 AndCondition conCalc = new AndCondition( new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame"), new PropertyCondition(AutomationElement.NameProperty, "计算器")); AutomationElement calcElement = rootElement.FindFirst(TreeScope.Children,conCalc); if (calcElement == null) { MessageBox.Show("计算器未启用"); return; } //找按钮3 var btn3 = GetButtonElementByName(calcElement, "3"); InvokeButton(btn3); System.Threading.Thread.Sleep(1000); //找按钮+ var btnPlus = GetButtonElementByName(calcElement, "乘"); InvokeButton(btnPlus); System.Threading.Thread.Sleep(1000); //找按钮2 var btn2 = GetButtonElementByName(calcElement, "2"); InvokeButton(btn2); System.Threading.Thread.Sleep(1000); //找按钮= var btnEqual = GetButtonElementByName(calcElement, "等于"); InvokeButton(btnEqual); System.Threading.Thread.Sleep(1000); //找结果 var findElements = calcElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Static")); AutomationElement calcResult = findElements[3]; MessageBox.Show("计算结果:"+GetStaticText(calcResult)); } /// <summary> /// 根据名称查找按钮 /// </summary> /// <param name="parentElement">父窗体句柄</param> /// <param name="elName"></param> /// <returns></returns> private AutomationElement GetButtonElementByName(AutomationElement parentElement,string elName) { AndCondition con =new AndCondition( new PropertyCondition(AutomationElement.ClassNameProperty, "Button"), new PropertyCondition(AutomationElement.NameProperty, elName)); AutomationElement findElement = parentElement.FindFirst(TreeScope.Descendants, con); if (findElement == null) throw new Exception("未找到按钮"+elName); return findElement; } /// <summary> /// 触发按钮事件 /// </summary> /// <param name="btnElement"></param> private void InvokeButton(AutomationElement btnElement) { if (btnElement == null) return; InvokePattern btnInvoke = (InvokePattern)btnElement.GetCurrentPattern(InvokePattern.Pattern); btnInvoke.Invoke(); } /// <summary> /// 设置文本的值 /// </summary> /// <param name="txtElement"></param> /// <param name="setValue"></param> private void SetValue(AutomationElement txtElement,string setValue) { if (txtElement == null) return; ValuePattern txtValue = (ValuePattern)txtElement.GetCurrentPattern(ValuePattern.Pattern); txtValue.SetValue(setValue); } /// <summary> /// 获取文本的值 /// </summary> /// <param name="txtElement"></param> /// <returns></returns> private string GetText(AutomationElement txtElement) { if (txtElement == null) return ""; TextPattern txtText = (TextPattern)txtElement.GetCurrentPattern(TextPattern.Pattern); string strText = txtText.DocumentRange.GetText(-1); return strText; } /// <summary> /// 获取静态类的值 /// </summary> /// <param name="staticElement"></param> /// <returns></returns> private string GetStaticText(AutomationElement staticElement) { if (staticElement == null) return ""; return staticElement.Current.Name; }
相关文章推荐
- Gradle学习笔记(二)
- Oracle 11g R2 64位在 win7 64位的安装流程图解
- hdu 4735Little Wish~ lyrical step~ 重复覆盖
- 信息学奥林匹克竞赛-津津的储蓄计划
- gdb 技巧学习
- 数据挖掘:K最近邻(KNN)算法的java实现
- SELECTOR
- shell脚本中导入mysql数据&&执行mysql语句
- Java输入输出流详解
- MathJax在IE中公式不解析
- java 读取ftp文件时特殊字符乱码
- PHPUnit测试私有属性和方法
- 半平面交总结and模板
- android——生成或者下载的图片在相册中找不到
- 問題排查:index was out of range (1)
- python操作excel表格详解(xlrd/xlwt)
- php屏蔽错误消息
- jquery文件上传控件 Uploadify 问题记录
- jQuery formValidator表单验证插件常见问题
- Linux增加swap空间