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. socket-自我总结(2)

    这里总结下一个服务端与多个客户端之间的通信. 先看demo: #/usr/bin/env python #_*_coding:utf-8_*_ __author__ = 'ganzl' import ...

  2. Python执行系统命令的方法 os.system(),os.popen(),commands

    os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...

  3. 一般处理程序中使用session

    首先引用:using System.Web.SessionState;  再在 IHttpHandler 后面加逗号加IReadOnlySessionState:IHttpHandler,IReadO ...

  4. iOS高效开发必备的10款Objective-C类库

    http://blog.csdn.net/yhawaii/article/details/7392988

  5. Odoo Web Service API

    来自 Odoo Web服务暴露出相关的服务,路由分别是 /xmlrpc/ /xmlrpc/2/ /jsonrpc 根据 services 调用 后端对应服务的 方法method [定义 openerp ...

  6. tfs 任务自定义项

    vs 命令 cd C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies 导出工作项类型文件 修改xml ...

  7. Redis 缓存 + Spring 的集成示例

    参考网址:http://blog.csdn.net/defonds/article/details/48716161

  8. Block回调

    •Block的定义   •Block.委托.通知.回调函数,它们虽然名字不一样,但是原理都一样,都是"回调机制"的思想的具体实现 •前面的代理模式的项目改为Block回调实现    ...

  9. iOS中的上传、下载流程心得

    访问相册 1.  判断资源库是否有效 2.  创建imagePickerController 设置代理 弹出视图控制器 3.  实现协议方法 > iOS10 访问系统相册需要在info.plis ...

  10. vim--macro

    例: qa some vim command q 这个宏只记录了vim命令到寄存器a中,执行这个宏可以用命令: @a 也可以加上执行次数: 10@a 执行10次 当你执行过一次@a之后,你可以用 @@ ...