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. Coursera深度学习(DeepLearning.ai)编程题&笔记

    因为是Jupyter Notebook的形式,所以不方便在博客中展示,具体可在我的github上查看. 第一章 Neural Network & DeepLearning week2 Logi ...

  2. UVALive 3716 DNA Regions

    题目大意:给定两个长度相等的字符串A和B,与一个百分比p%,求最长的.失配不超过p%的区间长度.O(nlogn). 题目比较简单套路,推推式子就好了. 记S[i]表示到下标i一共有多少个失配,就相当于 ...

  3. 某次送温暖考试的 c题

    题目大意: 给定n个点的无根树,树上每个点都有一个非负的点权. 树上的路径的价值定义为树上路径的点权和-树上路径的点权最大值; 现在给定一个参数P询问有多少条路径的价值是P的倍数(注意单点也算路径,路 ...

  4. bat修改密码

    @echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...

  5. virtual box未卸载报"Invalid Drive:F:\"的解决方案

    =============================================== 20170417_第一次修改                       ccb_warlock === ...

  6. 2、公司部门的组成 - CEO之公司管理经验谈

    今天讲讲公司部门的组成.公司部门一般是根据公司业务来进行划分的,IT企业和其它企业的部门划分有一定的区别.企业部门的划分还是比较重要的,部门主要明确各部门所具有的自己的职责.这里对IT企业的部门做了一 ...

  7. FFmpeg编译iOS静态库

    第一步:下载gas-preprocessor 1.1 下载https://github.com/libav/gas-preprocessor 1.2 拷贝 gas-preprocessor.pl 到 ...

  8. Python进阶内容(六)--- 函数式编程

    斐波那契数列(Fibonacci)的递归与非递归实现 费波那契数列由0和1开始,之后的数就由之前的两数相加 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

  9. JAVA 将图片转换为Base64编码

    这里使用的jar包是commons-codec-1.10.jar; 示例代码 import java.io.FileInputStream; import java.io.FileOutputStre ...

  10. html的布局demo

    header section footer 都是水平,垂直居中,文本内容居中 section的高度是根据文本内容自适应的,footer会一直在最下面 <!DOCTYPE html> < ...