在目前进行软件测试时,都或多或少的引入了自动化测试的概念,而且市面上也有好多软件自动化方面相关的工具,比如QTP,比如LoadRunner,但是这些工具要么售价不菲,要么对某些方面功能支持的不够全面,那么我们在引入软件自动化测试时,都需要考虑哪些方面呢?当然是最符合自己项目的工具最合适,同时费用也比较低,那么除了市面上这些商业软件外,还有没有哪些方法可以自己动手来做软件的自动化测试呢?答案是肯定的.
本文将介绍实现软件自动化测试其中一种测试方法------UIAutomation技术,当然,此种方法仅限于对.Net软件使用.
反射技术具体是什么原理,本文将不做任何介绍,大家可以去网上搜索一下,有很多这方面的文章介绍,本文只介绍如何使用UIAutomation技术进行.NET软件的UI界面的自动化测试.
废话少说,多做实事!(本文所有代码均在VS2008环境下测试通过)
一. 创建待测试程序
1. 启动VS2008,建立一个C#的WinForm工程,并将工程文件名命名为AUT(Application Under Test)
2. 创建的Form上的按钮布局,如下图所示
3. 一个菜单(包含一个以及菜单File以及一个二级菜单Exit),一个TextBox,一个ComboBox,一个Button以及一个ListBox
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
4. 给Button按钮添加一个消息响应函数
private void button1_Click(object sender, EventArgs e)
{
string tb = textBox1.Text;
string cb = comboBox1.Text;
if (tb == cb)
{
listBox1.Items.Add("Result is a tie");
}
else if (tb == "paper" && cb == "rock" ||
tb == "rock" && cb == "scissors" ||
tb == "scissors" && cb == "paper")
{
listBox1.Items.Add("The TextBox wins");
}
else
{
listBox1.Items.Add("The ComboBox wins");
}
}
5. 给Exit菜单添加一个消息响应函数
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
6.给ComboBox控件添加三个Item,分别为paper,rock,scissions
7. 编译待测试程序,生成文件名为AUT.exe的待测试程序
二. 创建测试程序
1. 启动VS2008,创建一个C#控制台程序,并命名为AutomationUITest
2. 在引用中添加以下三个类: UIAutomationClient, UIAutomationClientsideProviders以及UIAutomationTypes
3. 在工程中添加以下using语句
using System;
using System.Windows.Automation;
using System.Threading;
using System.Diagnostics;
using System.IO;
4. 定义启动测试程序以及获取窗体句柄的函数
///<summary>
///根据传入的路径启动相应的可执行程序,并返回进程ID
///</summary>
public static Int32 StartExe(string strExePath)
{
if (null == strExePath)
{
return 0;
}
Process ps = Process.Start(strExePath);
Threa

本文介绍了如何利用UIAutomation技术进行.NET软件的自动化测试,包括创建待测试程序,创建测试程序,以及详细的操作TextBox、ComboBox、Button和ListBox等控件的方法,最后展示了完整的测试代码流程。
最低0.47元/天 解锁文章
1785

被折叠的 条评论
为什么被折叠?



