UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://msdn.microsoft.com/en-us/library/ms753107.aspx link.

The important part is Using UI Automation fir Automated Testing.

As a new tester of UI Automation. I can give a demo that U can understand the UI automation better.

1st. Adding the refer reference. The reference U need add include(UIAutomation.dll, UIAutomationClientSideProvider.dll, UIAutomationTypes.dll). if U can not find these DLL U can find these in your computer C disc.

2nd. Add the using system.(System.Window.Automation).

3rd. Find the control. U must find the control which U using such as Button, textbox or label.

  var desktop = AutomationElement.RootElement; // find the root element, can be consider as desktop
  var condition = new PropertyCondition(AutomationElement.NameProperty, "test"); // define the string that should be find,name as "test"
  var window = desktop.FindFirst(TreeScope.Children, condition); //find the desktop child control which confirm the string control

UI Automation  need a tool to find the control property and the event. The tool is the UI Spy.

If U want managemet the more properties, U can use the AndContion object.  Such as click the "OK" button.

  var btnCondition = new AndCondition(
                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
                new PropertyCondition(AutomationElement.NameProperty, "ok"));

4th. How to trigger the control. Like Button click event, window drag event and so on. An easy example Button click:

  var button = window.FindFirst(TreeScope.Children, btnCondition);
  var clickPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
  clickPattern.Invoke();

How to find the control contains which Patter?  See the UI Spy.

UI Automation Control Patterns Overview

Summary:

  Read more MDSN file, test mare.

Next give a demo of UI Automation of Notepad Test.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Windows.Automation.Text; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
#region step 0: create a txt file and name it with Demon.txt.
if (!File.Exists("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\Demo.txt"))
{
FileStream fs = new FileStream("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs); //FileStream fs = new FileStream(@"C:\Users\MBSUser\Documents\Visual Studio 2010\Projects\ConsoleApplication2", FileMode.Create, FileAccess.Write);
//StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(, SeekOrigin.Begin);
sw.WriteLine("This is just a txt demo, Test World!.");
sw.Close();
}
else
{
FileStream fsexist = new FileStream("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt", FileMode.Open, FileAccess.Write);
StreamWriter swexist = new StreamWriter(fsexist);
swexist.WriteLine("This is just a txt demo, Test World!.");
swexist.Close();
fsexist.Close();
}
#endregion #region step 1: Open 1 txt file by notepad
ProcessStartInfo psi = new ProcessStartInfo("C:\\Users\\MBSUser\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Demo.txt");
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.UseShellExecute = true; Console.WriteLine("1.open 1 txt file by notepad.");
Process p = Process.Start(psi); Thread.Sleep(); IntPtr windowHandle = p.MainWindowHandle;
AutomationElement notepad = AutomationElement.FromHandle(windowHandle);
#endregion #region Step2: Click "Edit->Find" menu.
// Step2-1: Expand Edit Menu.
string editMenuAutomationId = "Item 2";
Console.WriteLine("Step2: Click Find menu.");
PropertyCondition propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
editMenuAutomationId);
AutomationElement editMenu = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition); if (editMenu == null)
{
Console.WriteLine(editMenuAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)editMenu.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
ExpandCollapsePattern expandEdit = editMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern)
as ExpandCollapsePattern;
expandEdit.Expand();
Console.WriteLine(editMenuAutomationId.ToString() + " expanded."); // Step2-2: Click Find menu item.
string findMenuItemAutomationId = "Item 21";
propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
findMenuItemAutomationId);
AutomationElement findMenuItem = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition); if (findMenuItem == null)
{
Console.WriteLine(findMenuItemAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)findMenuItem.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
InvokePattern invokePattern = findMenuItem.GetCurrentPattern(InvokePattern.Pattern)
as InvokePattern;
invokePattern.Invoke(); // Wait for Find dialog pop up. Can use verify Find dialog existing to avoid hardcode sleep time.
Thread.Sleep();
Console.WriteLine(findMenuItemAutomationId.ToString() + " invoked.");
#endregion #region Step3: Set text: "World" in Find Dialog.
Console.WriteLine("Step3: Set text in Find Dialog."); string findTextAutomationId = "";
string findText = "World";
propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
findTextAutomationId);
AutomationElement findTextField = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition);
if (findTextField == null)
{
Console.WriteLine(findTextAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)findTextField.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
ValuePattern valuePattern =
findTextField.GetCurrentPattern(ValuePattern.Pattern)
as ValuePattern;
valuePattern.SetValue(findText);
Console.WriteLine(findTextAutomationId.ToString() + " value changed.");
#endregion #region Step4: Click "Find Next".
Console.WriteLine("Step3: Click Find Next button in Find Dialog."); string findNextAutomationId = "";
propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
findNextAutomationId);
AutomationElement findNextButton = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition); if (findNextButton == null)
{
Console.WriteLine(findNextAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)findNextButton.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
invokePattern =
findNextButton.GetCurrentPattern(InvokePattern.Pattern)
as InvokePattern;
invokePattern.Invoke();
Console.WriteLine(findNextAutomationId.ToString() + " invoked.");
#endregion #region Step5: Click "Cancel" to close the Find dialog.
Console.WriteLine("Step3: Click Cancel button to close the Find dialog."); string cancelAutomationId = "";
propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
cancelAutomationId);
AutomationElement cancelButton = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition); if (cancelButton == null)
{
Console.WriteLine(cancelAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)cancelButton.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
invokePattern =
cancelButton.GetCurrentPattern(InvokePattern.Pattern)
as InvokePattern;
invokePattern.Invoke();
Console.WriteLine(cancelAutomationId.ToString() + " invoked.");
#endregion #region Step6: Verify the string is found.
Console.WriteLine("Step6: Verify the string is found."); string actualResult = "";
string expectedResult = findText;
string editAreaAutomationId = "";
propertyCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty,
editAreaAutomationId);
AutomationElement editArea = notepad.FindFirst(TreeScope.Element | TreeScope.Descendants, propertyCondition); if (editArea == null)
{
Console.WriteLine(editAreaAutomationId.ToString() + " could not be found.");
return;
}
if ((bool)editArea.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) == false)
{
Console.WriteLine("Element not enabled.");
return;
}
TextPattern textPattern =
editArea.GetCurrentPattern(TextPattern.Pattern)
as TextPattern;
actualResult = textPattern.DocumentRange.GetText(-);
TextPatternRange[] searchRange = textPattern.GetSelection();
int startPoints = textPattern.DocumentRange.CompareEndpoints(
TextPatternRangeEndpoint.Start,
searchRange[],
TextPatternRangeEndpoint.Start);
int endPoints = textPattern.DocumentRange.CompareEndpoints(
TextPatternRangeEndpoint.End,
searchRange[],
TextPatternRangeEndpoint.End);
actualResult = actualResult.Substring(actualResult.Length + startPoints, -(startPoints + endPoints)); if (expectedResult == actualResult)
{
Console.WriteLine("Pass. The expected string is found.");
}
else
{
Console.WriteLine(String.Format("Fail. The expected string is {0}; the actual string is {1}", expectedResult, actualResult));
} #endregion #region Test Cleanup: Close notepad.
Console.WriteLine("Test Cleanup: Close notepad.");
p.CloseMainWindow();
#endregion
}
}
}

UI Automation Test的更多相关文章

  1. UI Automation 简介

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

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

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

  3. MS UI Automation Introduction

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

  4. Server-Side UI Automation Provider - WPF Sample

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

  5. Client-Side UI Automation Provider - WinForm Sample

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

  6. Server-Side UI Automation Provider - WinForm Sample

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

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

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

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

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

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

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

随机推荐

  1. android 开发 程序中下载安装APK文件 问题汇总 解析程序包时出现问题

    1 若把APK文件保存到应用程序的files目录下,则一定注意保存时使用 FileOutputStream os = openFileOutput(fileName, MODE_WORLD_READA ...

  2. Activex、OLE、COM、OCX、DLL之间的区别(转)

    熟悉面向对象编程和网络编程的人一定对ActiveX.OLE和COM/DCOM这些概念不会陌生,但是它们之间究竟是什么样的关系,对许多们还是比较模糊的.在具体介绍它们的关系之间,我们还是先明确组件(Co ...

  3. Sql Server隔离级别(2)

    Sql Server2005之后,引入了一个新的隔离级别Snapshot(Read Committed Snapshot Isolation (RCSI))和(Snapshot Isolation ( ...

  4. 调用KEditor批量上传图片

    调用KEditor批量上传图片: <script charset="utf-8" src="/Keditor/kindeditor.js">< ...

  5. Java和.NET使用DES对称加密的区别

    Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...

  6. js中原型继承的概念

  7. SQL Server 导入数据失败:无法在只读列“Id”中插入数据

    解决方案: 在数据库导入"选择源表和源视图"->点击对应表后的“编辑映射”按钮->勾选“启用标识列插入”->导入即可.

  8. XCode 7.3.1(dmg) 官方直接下载地址

    xcode7.3.1下载 https://idmsa.apple.com/IDMSWebAuth/login?appIdKey=891bd3417a7776362562d2197f89480a8547 ...

  9. (转载)自动化基础普及之selenium是啥?

    转载:http://www.cnblogs.com/fnng/p/3980093.html Selenium 并不像QTP那样让人一下子就明白是什么?它是编程人员的最爱,但它却对测试新手产生了很大的阻 ...

  10. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...