UI Automation Test
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的更多相关文章
- UI Automation 简介
转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interf ...
- 使用UI Automation实现自动化测试 --工具使用
当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...
- MS UI Automation Introduction
MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...
- Server-Side UI Automation Provider - WPF Sample
Server-Side UI Automation Provider - WPF Sample 2014-09-14 引用程序集 自动化对等类 WPF Sample 参考 引用程序集 返回 UIAut ...
- Client-Side UI Automation Provider - WinForm Sample
Client-Side UI Automation Provider - WinForm Sample 2014-09-15 源代码 目录 引用程序集实现提供程序接口分发客户端提供程序注册和配置客户 ...
- Server-Side UI Automation Provider - WinForm Sample
Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码 目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...
- 从UI Automation看Windows平台自动化测试原理
前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...
- 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...
- UI Automation的两个成熟的框架(QTP 和Selenium)
自己在google code中开源了自己一直以来做的两个自动化的框架,一个是针对QTP的一个是针对Selenium的,显而易见,一个是商业的UI automation工具,一个是开源的自动化工具. 只 ...
随机推荐
- DataSnap修改数据ApplyUpdates出现错误:连接繁忙导致另一个命令
最近准备尝试用DBExpress做个SQL Serer应用,在学习的时候发现一个问题使用DBExpress连接Sql server 2008 express使用以下控件SQLConnection-&g ...
- Ubuntu 安装 SublimeText 3
1. 下载 $ cd ~/Downloads $ wget https://download.sublimetext.com/sublime-text_build-3083_i386.deb 2. 安 ...
- input[file]标签的accept=”image/*”属性响应很慢的解决办法
转自:http://blog.csdn.net/lx583274568/article/details/52983693 input[file]标签的accept属性可用于指定上传文件的 MIME类型 ...
- springbootboot-HttpServletRequest.getInputStream() 获取post内容
问题描述: 在php端用curl post一段json到java springboot.在java端用request.getInputStream()获取到的数据为空. 问题确认: 询问度娘后, 她告 ...
- xcode7、iOS9 设置启动图片(Launch Image)
主要是解决上架的时候遇到的问题,顺便把LaunchImage的使用学习一下,一开始项目使用的xib作为启动页的,最近上架打包的时候报错,通不过,问题如下: ERROR ITMS-90096: &quo ...
- win10家庭版快速升级专业版
win10家庭普通版升级专业版方法: 1.点击“开始”,选择控制面板. 2.点击“系统与安全”,选择“Windows Anytime Upgrade”.(或者:单击「开始」按钮,在搜索框中,键入any ...
- 安装android
http://www.oschina.net/question/1463998_220998 http://www.cnblogs.com/zoupeiyang/p/4034517.html
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- 技术英文单词贴--M
M mention 提到,提及,说起 merge 合并,融入 multiple 多重的,复杂的
- MAC 安装 Protobuf
1.确认MAC装有g++.make.vim工具 2.安装make工具使用 brew install make 3.安装protobuf brew install protobuf 4.安装 ...