C# 初探UI Automation
来源:互联网 发布:软件项目报价表 编辑:程序博客网 时间:2024/05/22 12:00
最近研究自动化测试,看了一下UI Automation的微软例子,表示太老了,遇到各种问题,
UI Spy 好像已经被放弃了,可以用inspect.exe来代替,win10 的路径为:"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\inspect.exe"
这个用来查询automationId,
官网是以计算器例子,下面是在win10 修改后能运行版本
class CalcAutomationClient
{
AutomationElement calWindow = null;//计算器窗口主窗口元素
string resultTextAutoID = "CalculatorResults";
string btn5AutoID = "num5Button";
string btn3AutoID = "num3Button";
string btn2AutoID = "num2Button";
string btnPlusAutoID = "plusButton";
string btnSubAutoId = "94";
string btnEqualAutoID = "equalButton";
static void Main(string[] args)
{
CalcAutomationClient autoClient = new CalcAutomationClient();
AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.OnWindowOpenOrClose);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);
Process.Start("calc.exe");
Console.ReadLine();
}
private void OnWindowOpenOrClose(object sender, AutomationEventArgs e)
{
if (calWindow != null)
return;
if(e.EventId!=WindowPattern.WindowOpenedEvent)
{
return;
}
if(sender ==null)
{
Console.WriteLine("sender is null");
return;
}
Thread.Sleep(1000);//此处必须等待一下,应该是计算器的等待计算器完全加载,不然控件 找不到
AutomationElement sourceElement = null;
sourceElement = sender as AutomationElement;
Console.WriteLine(sourceElement.Current.Name);
try
{
sourceElement = sender as AutomationElement;
Console.WriteLine(sourceElement.Current.Name);
if (sourceElement.Current.Name=="计算器")
{
calWindow = sourceElement;
}
}
catch(Exception ex)
{
Console.WriteLine("ex:" + ex.Message);
return;
}
if(calWindow == null)
{
return;
}
ExcuteTest();
}
private void ExcuteTest()
{
ExcuteButtonInvoke(btn2AutoID);
ExcuteButtonInvoke(btnPlusAutoID);
ExcuteButtonInvoke(btn3AutoID);
ExcuteButtonInvoke(btnEqualAutoID);
string rs = GetCurrentResult();
Console.WriteLine(rs);
}
private void ExcuteButtonInvoke(string automationId)
{
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.AutomationIdProperty,automationId),
new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Button));
if (calWindow == null)
return;
AutomationElementCollection collection = calWindow.FindAll(TreeScope.Descendants, conditions);
if (collection == null || collection.Count == 0)
return;
AutomationElement btn = collection[0];
if (btn != null)
{
InvokePattern invokeptn = (InvokePattern)btn.GetCurrentPattern(InvokePattern.Pattern);
invokeptn.Invoke();
}
Thread.Sleep(1000);
}
private string GetCurrentResult()
{
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.AutomationIdProperty, resultTextAutoID),
new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Text));
AutomationElement text = calWindow.FindAll(TreeScope.Descendants, conditions)[0];
return text.Current.Name;
}
}
- C# 初探UI Automation
- iOS UI Automation 初探
- UI Automation
- CATIA Automation 编程初探
- CATIA Automation 编程初探
- UI Automation Introduction.
- UI Automation(一)
- UI Automation-InvokePattern
- UI Automation-ValuePattern
- UI Automation-ExpandCollapsePattern
- UI Automation-SelectItemPattern
- UI Automation test
- UI Automation-ScrollPattern
- UI Automation-TogglePattern
- UI automation - .net
- UI Automation-WindowPattern
- UI Automation-GridPattern
- UI Automation-TablePattern
- 路由器和节点交换机
- 解决:错误1error MSB8031: Building an MFC project for a non-Unicode character set is deprecated
- Android学习路之SeekBar&RatingBar
- 关于eclipse中的web项目没有部署到tomcat中
- 第三章 列表
- C# 初探UI Automation
- OBJECTIVE-C编程(第2版)学习笔记3
- 关于NoSuchBeanDefinitionException: No bean named 'xxxx' is defined问题解决
- 【实践】RGW+Ganesha环境部署
- 单行函数简介
- History隐藏历史记录
- mybatis
- 算法概论8章,部分习题证明
- 深入理解JVM之类的加载机制