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

《软件测试自动化之道》读书笔记 之 基于反射的UI测试

2014-09-25 16:14 309 查看

《软件测试自动化之道》读书笔记 之 基于反射的UI测试

2014-09-24

测试自动化程序的任务
待测程序
测试程序
启动待测程序
设置窗体的属性
获取窗体的属性
设置控件的属性
获取控件的属性
方法调用
测试程序代码

测试自动化程序的任务

返回

基于反射的ui测试自动化程序,要完成的6项任务:

通过某种方式从测试套件程序中运行待测程序(AUT: Applicaton Under Test),以便于两个程序之间进行通信

操纵应用程序的窗体,从而模拟用户对窗体所实施的moving和resizing操作

检查应用程序窗体,确定应用程序的状态是否准确

操纵应用程序控件的属性,从而模拟用户的一些操作,比如模拟在一个TextBox控件里输入字符

检查应用程序控件的属性,确定应用程序的状态是否准确

调用应用程序的方法,从而模拟一些用户操作,比如模拟单击一个按钮

待测程序

返回

AUT是一个剪刀、石头、布的猜拳软件,当点击button1时,会在listbox中显示谁是胜者。

// Chapter 2 - Reflection-Based UI Testing
// Example Program: ReflectionUITest

using System;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace ReflectionUITest
{
class Class1
{
static BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance;
static AutoResetEvent are = new AutoResetEvent(false);

[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("\nStarting test scenario");
Console.WriteLine("\nLaunching Form1");
Form theForm = null;
string formName = "AUT.Form1";
string path = "..\\..\\..\\AUT\\bin\\Debug\\AUT.exe";
theForm = LaunchApp(path, formName);

System.Threading.Thread.Sleep(1000);

Console.WriteLine("\nMoving Form1");
Point pt = new Point(320, 100);
SetFormPropertyValue(theForm, "Location", pt);

System.Threading.Thread.Sleep(1000);

Console.WriteLine("\nSetting textBox1 to 'rock'");
SetControlPropertyValue(theForm, "textBox1", "Text", "rock");
Console.WriteLine("Setting comboBox1 to 'scissors'");
SetControlPropertyValue(theForm, "comboBox1", "Text", "scissors");

System.Threading.Thread.Sleep(1000);

Console.WriteLine("\nClicking button1");
object[] parms = new object[] { null, EventArgs.Empty };
InvokeMethod(theForm, "button1_Click", parms);

bool pass = true;

Console.WriteLine("\nChecking listBox1 for '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-- Scenario result = Pass --");
else
Console.WriteLine("\n-- Scenario result = *FAIL* --");

Console.WriteLine("\nClicking File->Exit in 3 seconds");
Thread.Sleep(3000);
InvokeMethod(theForm, "menuItem2_Click", parms);

Console.WriteLine("\nEnd test scenario");
}
catch (Exception ex)
{
Console.WriteLine("Fatal error: " + ex.Message);
}
Console.Read();
} // Main()

static Form LaunchApp(string path, string formName)
{
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 void SetFormPropertyValueHandler(Form f, string propertyName, object newValue);
static void SetFormPropertyValue(Form f, string propertyName, object newValue)
{
//true if the control's System.Windows.Forms.Control.Handle was created on
//     a different thread than the calling thread
if (f.InvokeRequired)
{
Delegate d = new SetFormPropertyValueHandler(SetFormPropertyValue);
object[] o = new object[] { f, propertyName, newValue };
//f type is Form, in this case, is instance of AUT.Form1
f.Invoke(d, o);
are.WaitOne();
}
else
{
Type t = f.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(f, newValue, null);
are.Set();
}
}

delegate object GetFormPropertyValueHandler(Form f, string propertyName);
static object GetFormPropertyValue(Form f, string propertyName)
{
if (f.InvokeRequired)
{
Delegate d = new SetFormPropertyValueHandler(SetFormPropertyValue);
object[] o = new object[] { f, propertyName };
object iResult = f.Invoke(d, o);
are.WaitOne();
return iResult;
}
else
{
Type t = f.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
object gResult = pi.GetValue(f, null);
are.Set();
return gResult;
}
}

delegate void SetControlPropertyValueHandler(Form f, string controlName, string propertyName, object newValue);
static void SetControlPropertyValue(Form f, string controlName, string propertyName, object newValue)
{
if (f.InvokeRequired)
{
Delegate d = new SetControlPropertyValueHandler(SetControlPropertyValue);
object[] o = new object[] { f, controlName, propertyName, newValue };
f.Invoke(d, o);
are.WaitOne();
}
else
{
Type t1 = f.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(f);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName);
pi.SetValue(ctrl, newValue, null);
are.Set();
}
}

delegate void InvokeMethodHandler(Form f, string methodName, params object[] parms);
static void InvokeMethod(Form f, string methodName, params object[] parms)
{
if (f.InvokeRequired)
{
Delegate d = new InvokeMethodHandler(InvokeMethod);
f.Invoke(d, new object[] { f, methodName, parms });
are.WaitOne();
}
else
{
Type t = f.GetType();
MethodInfo mi = t.GetMethod(methodName, flags);
mi.Invoke(f, parms);
are.Set();
}
}

delegate object GetControlPropertyValueHandler(Form f, string controlName, string propertyName);
static object GetControlPropertyValue(Form f, string controlName, string propertyName)
{
if (f.InvokeRequired)
{
Delegate d = new GetControlPropertyValueHandler(GetControlPropertyValue);
object[] o = new object[] { f, controlName, propertyName };
object iResult = f.Invoke(d, o);
are.WaitOne();
return iResult;
}
else
{
Type t1 = f.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(f);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName);
object gResult = pi.GetValue(ctrl, null);
are.Set();
return gResult;
}
}
} // Class1
} // ns


View Code



图2 测试结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: