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工具,一个是开源的自动化工具. 只 ...
随机推荐
- select 标签的两种方式(以动态插入为例)
标准形式: html <select id="sorte_piceid_copy" name="tea" style="display:none ...
- maven配置之setting配置
<!--声明语句--> <?xml version="1.0" encoding="UTF-8"?> <settings xmln ...
- Centos 7U盘安装
1. 8G U盘 老毛桃软件 centos 7 everything.iso 7.4G左右 2.遇到问题 安装一刚开始 报错 unexpected exception attribute error ...
- java keytool证书工具使用小结
java keytool证书工具使用小结 在Security编程中,有几种典型的密码交换信息文件格式: DER-encoded certificate: .cer, .crt PEM-encod ...
- c#窗体虚线图形验证码设计
/************************窗体验证码背景图形设计及核心代码**********/ using System;using System.Collections.Generic;u ...
- [转] Android 4.4中播放HTML5视频<video>的Bug
近期Nexus 4手机自动升级到Android4.4,本来挺好的一件事儿,结果发现自己的应用中出现一个Bug,应用中使用了Webview播放HTML5视频,代码如下: <video width= ...
- LoadRunner参数更新方法和数据分配
参数化的定义:使用指定的数据源中的值来替换脚本录制生成的语句中的参数. 对Vuser脚本进行参数化的好处: 1.减小脚本的大小 2.提供了使用不同的脚本的值执行脚本的能力 参数化涉及两个任务: 1.用 ...
- ww
#!bin/python #coding=utf-8 """ Create by he """ import sys import re i ...
- 关于AJAX
Ajax(异步JavaScript和XML) ajax主要用于异步加载页面,可以使用户在不刷新页面的情况下进行更新. ajax的主要优势是对页面的请求以异步的方式发送到服务器.而服务器不会再用整个页面 ...
- Java(String)
1>String类型判等 (1)请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? package StringPool; publi ...