1. 首先建立一个待测试的winform程序,即UI Automation的服务端。

下面是button事件处理程序。

 private void CalculateButton_Click(object sender, EventArgs e)
{
int num1 = ;
int num2 = ;
if (!int.TryParse(textBox1.Text.Trim(), out num1) || !int.TryParse(textBox2.Text.Trim(), out num2))
{
MessageBox.Show("Please input a correct number!");
return;
}
textBox3.Text = (num1 + num2).ToString();
}

  2. 建立一个测试程序,做UI Automaion的客户端。

  添加引用:UIAutomationClient.dll 和 UIAutomationTypes.dll

public static void TestWinFrom(int num1, int num2)
{
string winfromPath = @"C:\Users\v-lix\Documents\Visual Studio 2010\Projects\UIAutomationDemo\WinformForTesting\bin\Debug\WinformForTesting.exe";
Console.WriteLine("Begin WinForm UIAutomation test runn");
Console.WriteLine("Launching WinFormTest application");
Process p = Process.Start(winfromPath);
Thread.Sleep(TimeSpan.FromSeconds());
Console.WriteLine("Get desktop");
var desktop = AutomationElement.RootElement; // Method 1 by windowhandle
AutomationElement testForm = AutomationElement.FromHandle(p.MainWindowHandle); // Method 2 by name property
//PropertyCondition proCon = new PropertyCondition(AutomationElement.NameProperty,"TestForm1");
//testForm = desktop.FindFirst(TreeScope.Children,proCon); if (testForm == null)
{
Console.WriteLine("Could find testform!");
return;
} Console.WriteLine("Try to find the button control in testForm");
AutomationElement button = testForm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Calculate")); Console.WriteLine("Try to find all the textbox in testform!");
AutomationElementCollection aeAllTextBoxes = testForm.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); // 控件初始化的顺序是先初始化后添加到控件
// this.Controls.Add(this.textBox3);                 
// this.Controls.Add(this.textBox2);
// this.Controls.Add(this.textBox1); AutomationElement aeTextBox1 = aeAllTextBoxes[];
AutomationElement aeTextBox2 = aeAllTextBoxes[];
AutomationElement aeTextBox3 = aeAllTextBoxes[]; Console.WriteLine("Settiing input to '30'");
//set textboxs' value by ValuePattern
ValuePattern vpTextBox1 = (ValuePattern)aeTextBox1.GetCurrentPattern(ValuePattern.Pattern);
vpTextBox1.SetValue(num1.ToString());
ValuePattern vpTextBox2 = (ValuePattern)aeTextBox2.GetCurrentPattern(ValuePattern.Pattern);
vpTextBox2.SetValue(num2.ToString());
Thread.Sleep();
Console.WriteLine("Clickinig on button1 Button.");
InvokePattern ipButton = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
ipButton.Invoke();
Thread.Sleep(); ValuePattern vpTextBox3 = (ValuePattern)aeTextBox3.GetCurrentPattern(ValuePattern.Pattern);
if (int.Parse(vpTextBox3.Current.Value.Trim()) != (num1 + num2))
{
Console.WriteLine("Failed");
}
else
{
Console.WriteLine("Pass");
} Thread.Sleep();
//close testform
WindowPattern wpCloseForm = (WindowPattern)testForm.GetCurrentPattern(WindowPattern.Pattern);
wpCloseForm.Close();
}

最新文章

  1. 【HTML5&CSS3进阶03】Jser与Csser如何一起愉快的翻新老组件
  2. 输出NSRange类型的数据
  3. Kendo UI - Observable
  4. LabelMe图像数据集下载
  5. ti processor sdk linux am335x evm /bin/commom.sh hacking
  6. [Ramda] Compose and Curry
  7. 【转】Android之NDK开发
  8. Unity NGUI的多分辨率适配
  9. PNG文件格式具体解释
  10. CevaEclipse - 编译器attribute扩展
  11. WebService(1-1)webservice调用
  12. jquery easyui datagrid 如何第一次点击列标题时是降序排列
  13. TCP时间获取程序
  14. caffe入门-人脸检测1
  15. Golang go get第三方库的坑
  16. xcode代码提示没了
  17. Docker系列之Docker镜像(读书笔记)
  18. Hanlp等七种优秀的开源中文分词库推荐
  19. ubuntu 12.04和Windows 7双系统的安装方法
  20. vulcanjs 核心架构概念

热门文章

  1. HDU 4649 Professor Tian (概率DP)
  2. Html5学习进阶四 表单元素和表单属性
  3. Kubernetes DNS安装配置
  4. openstack token
  5. Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
  6. AC日记——[HAOI2007]覆盖问题 bzoj 1052
  7. JavaScript实现数字配对游戏
  8. hadoop之linux常用命令
  9. Python的支持工具[0] -> 环境包管理工具[1] -> Anaconda
  10. vue的路由设置小结