使用UI Automation实现自动化测试--2
2024-09-08 02:20:51
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();
}
最新文章
- 【HTML5&;CSS3进阶03】Jser与Csser如何一起愉快的翻新老组件
- 输出NSRange类型的数据
- Kendo UI - Observable
- LabelMe图像数据集下载
- ti processor sdk linux am335x evm /bin/commom.sh hacking
- [Ramda] Compose and Curry
- 【转】Android之NDK开发
- Unity NGUI的多分辨率适配
- PNG文件格式具体解释
- CevaEclipse - 编译器attribute扩展
- WebService(1-1)webservice调用
- jquery easyui datagrid 如何第一次点击列标题时是降序排列
- TCP时间获取程序
- caffe入门-人脸检测1
- Golang go get第三方库的坑
- xcode代码提示没了
- Docker系列之Docker镜像(读书笔记)
- Hanlp等七种优秀的开源中文分词库推荐
- ubuntu 12.04和Windows 7双系统的安装方法
- vulcanjs 核心架构概念
热门文章
- HDU 4649 Professor Tian (概率DP)
- Html5学习进阶四 表单元素和表单属性
- Kubernetes DNS安装配置
- openstack token
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
- AC日记——[HAOI2007]覆盖问题 bzoj 1052
- JavaScript实现数字配对游戏
- hadoop之linux常用命令
- Python的支持工具[0] ->; 环境包管理工具[1] ->; Anaconda
- vue的路由设置小结