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. 几个opencv 的iOS的编译问题解决

    一个iOS项目需要用到opencv,而且要支持arm64的,以前有个demo的,只支持32位的.到官网下载了最新支持64位库,结果编译无法通过. google了好久也没法解决,后来问了一个同事,找出原 ...

  2. test3

    下面写几个示例:这是行内公式: \( e^{\pi i} + 1 = 0\) ,下面的是行间公式: \[ e^{\pi i} + 1 = 0. \] 另一个复杂的公式: $$J_\alpha (x) ...

  3. ubuntu Unity Tweak Tool

    Unity Tweak Tool first install main program if do not run,so,second run : sudo apt-get install unity ...

  4. Reflector.exe 破解注意事项

    需要把网络断掉,然后选择手动激活 总结经验:操作步骤要仔细看清,否则会更浪费时间

  5. selenium--环境搭建步骤

    1.安装Python 2.安装setuptools 3.安装pip(Python3.X自带pip) 4.安装selenium(步骤在另一个博客中已提及)

  6. java io流之字符流

    字符流 在程序中一个字符等于两个字节,那么java提供了Reader.Writer两个专门操作字符流的类. 字符输出流:Writer Writer本身是一个字符流的输出类,此类的定义如下: publi ...

  7. JS绑定JavaScript事件

    //onblur="onblurs(this)" // function onblurs(e) { // alert(e.value); // }

  8. php学习笔记-基础篇

    1."var_dump"函数可以将变量的数据类型显示出来. 2."memory_get_usage"获取当前PHP消耗的内存. 3.php中的字符串型分单引号, ...

  9. Ajax 知识点

    AJAX 即"Asynchronous Javascript And XML"(异步JavaScript和XML) Ajax 不是某种编程语言,只是一种在无需重新加载整个网页的情况 ...

  10. centos文件误删除恢复

    Centos 文件误删除 当意识到误删除文件后,切忌千万不要再频繁写入了,否则 你的数据恢复的数量将会很少. 而我们要做的是,第一时间把服务器上的服务全部停掉,直接killall 进程名 或者 kil ...