您的位置:首页 > 产品设计 > UI/UE

基于ui automation的计算器测试例子

2014-01-09 09:31 477 查看
基于ui automation的计算器测试程序

using System;

using System.Text;

using System.Collections.Generic;

using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Windows;

using System.Windows.Automation;

using System.Diagnostics;

namespace TestProject2

{

[TestClass]

public class UnitTest1

{

[TestMethod]

public void TestMethod1()

{

//Process p = Process.Start(@"E:\Project\WinFormTest\WinFormTest\bin\Debug\WinFormTest.exe");

var desktop = AutomationElement.RootElement; // 先找到根元素,可以认为是桌面

var condition = new PropertyCondition(AutomationElement.NameProperty, "计算器"); // 定义我们的查找条件,名字是test

var window = desktop.FindFirst(TreeScope.Children, condition); // 在桌面的子控件中查找第一个符合条件的窗体。

// AutomationElement btnpane = window.FindFirst(TreeScope.Children,

// new PropertyCondition(AutomationElement.ClassNameProperty, "CalcFrame"));

AutomationElement btn9 = window.FindFirst(TreeScope.Children,

// new PropertyCondition(AutomationElement.NameProperty, "9"));

new PropertyCondition(AutomationElement.AutomationIdProperty, "133"));

AutomationElement btnPlus = window.FindFirst(TreeScope.Children,

new PropertyCondition(AutomationElement.NameProperty, "+"));

AutomationElement btn3 = window.FindFirst(TreeScope.Children,

new PropertyCondition(AutomationElement.NameProperty, "3"));

AutomationElement btnEq = window.FindFirst(TreeScope.Children,

new PropertyCondition(AutomationElement.NameProperty, "="));

List<AutomationElement> ele_list = new List<AutomationElement>();

ele_list.Add(btn9);

ele_list.Add(btnPlus);

ele_list.Add(btn3);

ele_list.Add(btnEq);

InvokePattern invokeptn = null;

foreach (AutomationElement elem in ele_list)

{

invokeptn = (InvokePattern)elem.GetCurrentPattern(InvokePattern.Pattern);

invokeptn.Invoke();

}

AutomationElement result = window.FindFirst(TreeScope.Children,

new PropertyCondition(AutomationElement.AutomationIdProperty, "403"));

if (result.Equals(12))

{

int t1 = 9;

}

else {

int t2 = 7;

}

//通过ValuePattern设置或者获得TextBox1的值

//ValuePattern vpTextBox1 = (ValuePattern)result.GetCurrentPattern(ValuePattern.Pattern);

//vpTextBox1.SetValue("50");

TextPattern tpTextBox3 = (TextPattern)result.GetCurrentPattern(TextPattern.Pattern);

string results = tpTextBox3.DocumentRange.GetText(-1); //获取textbox3中的值

//AutomationEventHandler eventHandler = new AutomationEventHandler(autoClient.onWindowOpenOrClose);

//Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, eventHandler);

/*

Condition conditions = new AndCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, resultTextAutoId),

new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));

AutomationElement btn = window.FindAll(TreeScope.Descendants, conditions)[0];

InvokePattern invokeptn = (InvokePattern)btn.GetCurrentPattern(InvokePattern.Pattern);

invokeptn.Invoke();

var btnCondition = new AndCondition(

new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),

new PropertyCondition(AutomationElement.NameProperty, "ok"));

var button = window.FindFirst(TreeScope.Children, btnCondition);

var clickPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);

clickPattern.Invoke();

*/

}

}

}

最后添加引用UIAutomationClientsideProviders.dll,UIAutomationTypes.dll,UIAutomationClient.dll三个dll的引用即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: