《软件测试自动化之道》读书笔记 之 基于反射的UI测试
《软件测试自动化之道》读书笔记 之 基于反射的UI测试
2014-09-24
测试自动化程序的任务
待测程序
测试程序
启动待测程序
设置窗体的属性
获取窗体的属性
设置控件的属性
获取控件的属性
方法调用
测试程序代码
测试自动化程序的任务
基于反射的ui测试自动化程序,要完成的6项任务:
- 通过某种方式从测试套件程序中运行待测程序(AUT: Applicaton Under Test),以便于两个程序之间进行通信
- 操纵应用程序的窗体,从而模拟用户对窗体所实施的moving和resizing操作
- 检查应用程序窗体,确定应用程序的状态是否准确
- 操纵应用程序控件的属性,从而模拟用户的一些操作,比如模拟在一个TextBox控件里输入字符
- 检查应用程序控件的属性,确定应用程序的状态是否准确
- 调用应用程序的方法,从而模拟一些用户操作,比如模拟单击一个按钮
待测程序
AUT是一个剪刀、石头、布的猜拳软件,当点击button1时,会在listbox中显示谁是胜者。
图1 待测程序GUI
AUT代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace AUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} 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");
} private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.Items[].ToString();
this.comboBox1.Text = this.comboBox1.Items[].ToString();
} private void menuItem2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
测试程序
启动待测程序
using System; using System.Reflection;
using System.Windows.Forms;
using System.Threading; static void Main(string[] args)
{
string formName = "AUT.Form1";
string path = "..\\..\\..\\AUT\\bin\\Debug\\AUT.exe";
theForm = LaunchApp(path, formName); //...
} 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);
}
}
要使用反射技术通过UI来测试Windows窗体,必须要在测试套件所在的进程内创建一个单独的线程来运行被测程序。这样,测试程序和被测程序就会在运行在同一进程里面,从而可以相互进行通信。
设置窗体的属性
static void Main(string[] args)
{
//...
//移动窗体到指定位置
SetFormPropertyValue(theForm, "Location", pt);
//...
} 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.Invoke(d, o);
are.WaitOne();
}
else
{
Type t = f.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(f, newValue, null);
are.Set();
}
}
问题1:如果在测试程序中直接调用PropertyInfo.SetValue()会抛错:"Exception has been thrown by the target of an invocation."。这是因为,不是在窗体的主线程里调用,而是在自动化测试程序所创建的一个线程里调用。因此,我们用Form.Invoke()方法以间接的方式调用SetValue。间接的方式调用,就是用delegate对象调用SetValue()。见如下代码:
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);
问题2:假如测试套件触发了待测程序的某个方法,而这个方法直接或间接创建一个新的线程去执行。如果需要等新线程执行结束以后才能在测试套间里继续下一步操作,可用AutoResetEvent对象来进行同步。代码如下:
//在类的作用域内定义如下对象,false参数意味着把这个对象出示初始化为未设置
static AutoResetEvent are = new AutoResetEvent(false); //这个语句把AutoResetEvent对象的值设为未设置,当前线程暂停执行,直到are.Set()语句把AutoResetEvent对象的值设为已设置
are.WaitOne()
获取窗体的属性
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;
}
}
设置控件的属性
static void Main(string[] args)
{
//...
SetControlPropertyValue(theForm, "textBox1", "Text", "rock");
//...
} 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();
}
}
BingFlags对象是用来过滤System.Reflection命名空间里许多不同类型的方法的。定义如下:
static BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance;
获取控件的属性
static void Main(string[] args)
{
//...
ListBox.ObjectCollection oc = (ListBox.ObjectCollection)GetControlPropertyValue(theForm, "listBox1", "Items");
string s = oc[].ToString();
if (s.IndexOf("TextBox wins") == -)
pass = false;
//...
} 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;
}
方法调用
static void Main(string[] args)
{
//...
object[] parms = new object[] { null, EventArgs.Empty };
InvokeMethod(theForm, "button1_Click", parms);
//...
} 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();
}
}
测试程序代码
// 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(); Console.WriteLine("\nMoving Form1");
Point pt = new Point(, );
SetFormPropertyValue(theForm, "Location", pt); System.Threading.Thread.Sleep(); 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(); 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[].ToString();
if (s.IndexOf("TextBox wins") == -)
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();
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
图2 测试结果
《软件测试自动化之道》读书笔记 之 基于反射的UI测试的更多相关文章
- 《软件测试自动化之道》读书笔记 之 基于Windows的UI测试
<软件测试自动化之道>读书笔记 之 基于Windows的UI测试 2014-09-25 测试自动化程序的任务待测程序测试程序 启动待测程序 获得待测程序主窗体的句柄 获得有名字控件的 ...
- 《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
<软件测试自动化之道>读书笔记 之 底层的Web UI 测试 2014-09-28 测试自动化程序的任务待测程序测试程序 启动IE并连接到这个实例 如何判断待测web程序完全加载到浏览 ...
- 软件测试自动化之- 基于反射的UI自动化测试框架 - UI Automation Test Framework
测试自动化程序的任务 基于反射的ui测试自动化程序,要完成的6项任务: 通过某种方式从测试套件程序中运行待测程序(AUT: Applicaton Under Test),以便于两个程序之间进行通信 操 ...
- 《软件测试自动化之道》读书笔记 之 SQL 存储过程测试
<软件测试自动化之道>读书笔记 之 SQL 存储过程测试 2014-09-28 待测程序测试程序 创建测试用例以及测试结果存储 执行T-SQL脚本 使用BCP工具导入测试用例数据 ...
- 《软件测试自动化之道》读书笔记 之 XML测试
<软件测试自动化之道>读书笔记 之 XML测试 2014-10-07 待测程序测试程序 通过XmlTextReader解析XML 通过XmlDocument解析XML 通过XmlPa ...
- 《Essential C++》读书笔记 之 基于对象编程风格
<Essential C++>读书笔记 之 基于对象编程风格 2014-07-13 4.1 如何实现一个class 4.2 什么是Constructors(构造函数)和Destructor ...
- 【哲学角度看软件测试】要想软件“一想之美”,UI 测试少不了
摘要:软件测试的最高层次需求是:UI测试,也就是这个软件"长得好不好看". 为了让读者更好地理解测试,我们从最基础的概念开始介绍.以一个软件的"轮回"为例,下图 ...
- <<google软件测试之道>>读书笔记
以前一直从开发的角度来看待测试,看完这本书以后感觉错了,难怪之前公司的测试一直搭建不起来 1.开发人员,开发测试人员,测试人员 * 开发人员负责开发 * 开发测试人员近距离接触代码,负责编写测试用例, ...
- 图论——读书笔记(基于BFS广度优先算法的广度优先树)
广度优先树 对于一个图G=(V,E)在跑过BFS算法的过程中会创建一棵广度优先树. 形式化一点的表示该广度 优先树的形成过程是这样的: 对于图G=(V,E)是有向图或是无向图, 和图中的源结点s, 我 ...
随机推荐
- Vim-一款好用的文本编辑器
关于vim的使用,通过博客,无论是静态的截图或者是代码/文本复制,很难展示所要表现的内容.更多需要读者亲自敲键盘实践之后才知道其作用. 本文不会也无法描述vim过多的细节,仅仅是对于常用的命令做一下记 ...
- 本地搭建ELK(elasticsearch, logstash, kibana)日志收集系统
环境准备:macos 预先安装brew包管理器 1.安装elasticsearch流程 那么,咱们先去安装java8 接着,咱们继续按照elasticsearch 接着,咱们启动elasticsear ...
- BZOJ.1430.小猴打架(Prufer)
题目链接 猴子之间的打架是棵无根树,有\(n^{n-2}\)种可能:同时n-1个过程的排列是\((n-1)!\) //820kb 104ms #include <cstdio> const ...
- Python3正则表示式(3)
正则表示式对象 对象1: 案例1: import re example = 'ShanDong Institute of Business and Technology' pattern = re.c ...
- 滑动CheckBox样式
<Style x:Key="SliderCheckBox" TargetType="{x:Type CheckBox}"> <Setter P ...
- IETester是一个免费的Web浏览器调试工具
功能简介 IETester是一个免费的Web浏览器调试工具,可以模拟出不同的js引擎来帮助程序员设计效果统一的代码.IETester可以在独立的标签页中开启IE5.5.IE6.IE7以及最I新的IE8 ...
- MoreWindows 微软认证专家博客目录(白话算法,C++ STL,windows编程)
为了方便大家查找和学习,现将本人博客中所有博客文章列出目录. (http://blog.csdn.net/morewindows) 一. 白话经典算法 目前有17篇,分为七大排序和经典面试题 ...
- 分布式队列ZooKeeper的实现
一.背景 有一些时候,多个团队需要共同完成一个任务,比如,A团队将Hadoop集群计算的结果交给B团队继续计算,B完成了自己任务再交给C团队继续做.这就有点像业务系统的工作流一样,一环一环地传下 去, ...
- Launch 启动全屏 隐藏上方状态栏
1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...
- apache 通过ajp访问tomcat多个站点
copy mod_jk.so to modules下 httpd的配置项中添加如下内容 LoadModule proxy_module modules/mod_proxy.so LoadModule ...