net自动化测试之道基于反射的UI自动化测试—例子程序:ReflectionUITest

This program(see Listing 2-1)combinesseveral of the techniques in the chapter to demon-

strate a lightweight reflection-based UItest-automation scenario.The scenario tests the

“paper-rock-scissors”form application described in the introductionto this chapter.The test

scenario launches the application,moves theform,and then simulates typing rock into the

TextBox control and a user selecting scissorsfrom the ComboBox control.Then the scenariosim-

ulates a button click.The automation checksto see if the expected TextBox1winsstring is in

the ListBox control,and determines ascenario pass or fail result.The scenario finishes by

simulating a user selecting File》Exit toclose the application.Figure 2-1 in the introduction

to this chapter shows the result of runningthis test scenario.

This program assumes you haveadded project references to the System.Windows.Forms

and System.Drawingnamespaces.You can extend this automation scenario by using some of

the techniques described inChapter 1 and Chapter 4.For example,you can log test results to

external storage,or parameterizethe scenario to accept multiple input states.

程序2-1整合了本节中介绍的若干个技术,来展示轻量级基于反射的UI自动化测试场景。场景是测试本节的简介中描述的“石头-剪刀-布”form应用程序。该测试场景执行以下动作:运行form,移动form,模拟用户在TextBox控件中输入“rock”,模拟用户从ComboBox控件中选择“scissors”,模拟用户点击按钮。然后套件检查ListBox控件中显示“TextBox1 wins”的字符串,确定测试是通过,还是失败。最后模拟用户选择菜单File》Exit关闭form。简介中的图中显示了测试的结果。

我们假设已经添加了System.Windows.Forms和System.Drawing引用。你也可以使用前面章节描述的技巧扩展测试套件。例如将测试结果记录到外部文件中,或者参数化输入数据。

Listing 2-1.Program ReflectionUITest

using System;

using System.Reflection;

using System.Windows.Forms;

using System.Threading;

using System.Drawing;

namespace ReflectionUITest

{

class Class1

{

static BindingFlagsflags=BindingFlags.Public|

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance;

static AutoResetEvent are=newAutoResetEvent(false);

[STAThread]

static void Main(string[]args)

{

try

{

Console.WriteLine("\nStarting testscenario");

Console.WriteLine("\nLaunchingForm1");

Form theForm=null;

string formName="AUT.Form1";

stringpath="..\\..\\..\\AUT\\bin\\Debug\\AUT.exe";

theForm=LaunchApp(path,formName);

Console.WriteLine("\nMovingForm1");

Point pt=new Point(320,100);

SetFormPropertyValue(theForm,"Location",pt);

Console.WriteLine("\nSetting textBox1to'rock'");

SetControlPropertyValue(theForm,"textBox1","Text","rock

Console.WriteLine("Setting comboBox1to'scissors'");

SetControlPropertyValue(theForm,"comboBox1","Text",

"scissors");

Console.WriteLine("\nClickingbutton1");

object[]parms=newobject[]{null,EventArgs.Empty};

InvokeMethod(theForm,"button1_Click",parms);

bool pass=true;

Console.WriteLine("\nChecking listBox1for'TextBox wins'");

ListBox.ObjectCollection oc=

(ListBox.ObjectCollection)

GetControlPropertyValue(theForm,"listBox1",

"Items");

string s=oc[0].ToString();

if(s.IndexOf("TextBox wins")==-1)

pass=false;

if(pass)

Console.WriteLine("\n--Scenarioresult=Pass--");

else

Console.WriteLine("\n--Scenarioresult=*FAIL*--");

Console.WriteLine("\nClickingFile->Exit in 3 seconds");

Thread.Sleep(3000);

InvokeMethod(theForm,"menuItem2_Click",parms);

Console.WriteLine("\nEnd testscenario");

}

catch(Exception ex)

{

Console.WriteLine("Fatalerror:"+ex.Message);

}

}//Main()

static Form LaunchApp(string path,stringformName)

{

Form result=null;

Assembly a=Assembly.LoadFrom(path);

Type t=a.GetType(formName);

result=(Form)a.CreateInstance(t.FullName);

AppState aps=new AppState(result);

ThreadStart ts=new ThreadStart(aps.RunApp);

Thread thread=new Thread(ts);

thread.Start();

return result;

}

private class AppState

{

public readonly Form formToRun;

public AppState(Form f)

{

this.formToRun=f;

}

public void RunApp()

{

Application.Run(formToRun);

}

}//class AppState

delegate voidSetFormPropertyValueHandler(Form f,

string propertyName,object newValue);

static void SetFormPropertyValue(Formf,string propertyName,

object newValue)

{

if(f.InvokeRequired)

{

Delegate d=

new SetFormPropertyValueHandler(SetFormPropertyValue);

object[]o=newobject[]{f,propertyName,newValue};

f.Invoke(d,o);

are.WaitOne();

}

else

{

Type t=f.GetType();

PropertyInfopi=t.GetProperty(propertyName);

pi.SetValue(f,newValue,null);

are.Set();

}

}

delegate voidSetControlPropertyValueHandler(Form f,

string controlName,stringpropertyName,object newValue);

static void SetControlPropertyValue(Formf,string controlName,

string propertyName,object newValue)

{

if(f.InvokeRequired)

{

Delegate d=

new SetControlPropertyValueHandler(SetControlPropertyValue);

object[]o=newobject[]{f,controlName,propertyName,

newValue};

f.Invoke(d,o);

are.WaitOne();

}

else

{

Type t1=f.GetType();

FieldInfofi=t1.GetField(controlName,flags);

object ctrl=fi.GetValue(f);

Type t2=ctrl.GetType();

PropertyInfopi=t2.GetProperty(propertyName);

pi.SetValue(ctrl,newValue,null);

are.Set();

}

}

delegate void InvokeMethodHandler(Formf,string methodName,

params object[]parms);

static void InvokeMethod(Form f,stringmethodName,

params object[]parms)

{

if(f.InvokeRequired)

{

Delegate d=newInvokeMethodHandler(InvokeMethod);

f.Invoke(d,newobject[]{f,methodName,parms});

are.WaitOne();

}

else

{

Type t=f.GetType();

MethodInfomi=t.GetMethod(methodName,flags);

mi.Invoke(f,parms);

are.Set();

}

}

delegate objectGetControlPropertyValueHandler(Form f,

string controlName,string propertyName);

static object GetControlPropertyValue(Formf,string controlName,

string propertyName)

{

if(f.InvokeRequired)

{

Delegate d=

new GetControlPropertyValueHandler(GetControlPropertyValue);

object[]o=newobject[]{f,controlName,propertyName};

object iResult=f.Invoke(d,o);

are.WaitOne();

return iResult;

}

else

{

Type t1=f.GetType();

FieldInfofi=t1.GetField(controlName,flags);

object ctrl=fi.GetValue(f);

Type t2=ctrl.GetType();

PropertyInfopi=t2.GetProperty(propertyName);

object gResult=pi.GetValue(ctrl,null);

are.Set();

return gResult;

}

}

}//Class1

}//ns

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值