public Form1()
{
InitializeComponent();
this.textBox1.AccessibilityObject.Name = "t1";
this.textBox3.AccessibilityObject.Name = "t3";
} private void button1_Click(object sender, EventArgs e)
{
var t = this.textBox1;
int i = int.Parse(textBox1.Text);
int j = int.Parse(textBox2.Text); textBox3.Text = (i + j).ToString();
} private void testBtn_Name_Click(object sender, EventArgs e)
{
textBox3.Text = "test";
}

上面是被自动化的代码。

 /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//启动被测试的程序
Process p = Process.Start(@"C:\vs2015项目\Automation\Automation\bin\Debug\Automation.exe"); //自动化根元素,可以认为是桌面.
AutomationElement aeDeskTop = AutomationElement.RootElement; Thread.Sleep();
AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle); #region 获取 button 并模拟点击事件。 //找到窗体。
var testForm = aeDeskTop.FindFirst(TreeScope.Children, new PropertyCondition(
AutomationElement.NameProperty, "Form1")); //通过条件找到窗体里面的 btn。这个位置 name 属性对应控件的 Text 属性。
var btnCondition = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
new PropertyCondition(AutomationElement.NameProperty, "testBtn")); var singalCondition = new PropertyCondition(AutomationElement.NameProperty, "testBtn"); var testBtn = testForm.FindFirst(TreeScope.Children, btnCondition); // 在桌面的子控件中查找第一个符合条件的窗体。 //通过InvokePattern模拟点击按钮
InvokePattern ipClickTestBtn = (InvokePattern)testBtn.GetCurrentPattern(InvokePattern.Pattern);
ipClickTestBtn.Invoke(); #endregion #region 获取 textbox 并赋值 var txtCondition1 = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.NameProperty, "t1")); var txtCondition3 = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
new PropertyCondition(AutomationElement.NameProperty, "t3")); //设置值
var testText1 = testForm.FindFirst(TreeScope.Children, txtCondition1);
ValuePattern t1 = (ValuePattern)testText1.GetCurrentPattern(ValuePattern.Pattern);
t1.SetValue(""); //获取值
var testText3 = testForm.FindFirst(TreeScope.Children, txtCondition3);
var t3 = (TextPattern)testText3.GetCurrentPattern(TextPattern.Pattern);
string result2 = t3.DocumentRange.GetText(-); #endregion #region 实现关闭被测试程序 //实现关闭被测试程序
WindowPattern wpCloseForm = (WindowPattern)aeForm.GetCurrentPattern(WindowPattern.Pattern); #endregion
}

上面是控制代码

  

UI Automation的更多相关文章

  1. UI Automation Test

    UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://m ...

  2. UI Automation 简介

    转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...

  3. 使用UI Automation实现自动化测试 --工具使用

    当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...

  4. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  5. Server-Side UI Automation Provider - WPF Sample

    Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...

  6. Client-Side UI Automation Provider - WinForm Sample

    Client-Side UI Automation Provider -  WinForm Sample 2014-09-15 源代码 目录 引用程序集实现提供程序接口分发客户端提供程序注册和配置客户 ...

  7. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  8. 从UI Automation看Windows平台自动化测试原理

    前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...

  9. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

  10. UI Automation的两个成熟的框架(QTP 和Selenium)

    自己在google code中开源了自己一直以来做的两个自动化的框架,一个是针对QTP的一个是针对Selenium的,显而易见,一个是商业的UI automation工具,一个是开源的自动化工具. 只 ...

随机推荐

  1. 455. Assign Cookies.md

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  2. [置顶] Xamarin android 调用Web Api(ListView使用远程数据)

    xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...

  3. Linux第四节 组管理、用户管理、权限管理 / chmod /chown / umask / vim

    三期第三讲1.组管理/用户管理(重要文件系统会实时备份 file-) vim/etc/group: 组管理文件://组名:密码控位键:组id:成员 vim/etc/gshadow:组密码管理文件:// ...

  4. Maven安装教程

    一.安装Maven及配置环境变量 1.Maven官网地址:http://maven.apache.org/download.cgi  下载apache-maven-3.5.0-bin.zip文件 2. ...

  5. SQL Server授权购买简单介绍

    SQL Server授权购买简单介绍 之前有同事问我,使用盗版序列号的SQL Server到底有没有性能限制,之前本人一直没有深入研究过,后来经过一番资料搜集和查证,汇总成这篇文章 微软的SQL Se ...

  6. 中文代码示例之Angular入门教程尝试

    原址: https://zhuanlan.zhihu.com/p/30853705 原文: 中文代码示例教程之Angular尝试 为了检验中文命名在Angular中的支持程度, 把Angular官方入 ...

  7. copy&deepcopy

    import copy 字典参照列表结论,看是否有深层嵌套. a = {'name':1,'age':2} b = a a['name'] = 'ff' print(a) print(b) print ...

  8. Vue 爬坑之路(三)—— 使用 vue-router 跳转页面

    使用 Vue.js 做项目的时候,一个页面是由多个组件构成的,所以在跳转页面的时候,并不适合用传统的 href,于是 vue-router 应运而生. 官方文档: https://router.vue ...

  9. SVG 入门——理解viewport,viewbox,preserveAspectRatio

    工欲善其事必先利其器,没有真正搞懂SVG里的viewport,viewbox, preserveAspectRatio这三个属性,就很容易遇到坑,最近写项目用到svg这三个属性被我一眼就略过 ,后来发 ...

  10. SpringMVC ModelAndView、Map、Model、ModelMap

    目标方法返回值可以是ModelAndView .Map.Model.ModelMap类型,但最根本还都是ModelAndView. 其中可以包含试图和模型信息. SpringMVC 会把ModelAn ...