net自动化测试之道基于反射的UI自动化测试—简介

Introduction

The most fundamental and simplest form ofapplication testing is manual testing through the

application’s userinterface(UI).Paradoxically,automated testing through a user interface

(automated UI testing for short)ischallenging.The.NET environment provides you with many

classes in the System.Reflection namespacethat can access and manipulate an application at

run time.Using reflection,you can writelightweight automated UI tests.For example,suppose

you had a simple form-based Windowsapplication,as shown in the foreground of Figure 2-1.

 简介

测试应用程序最基础、最简单的方式是通过应用程序UI(用户接口)的手工测试。似乎通过UI进行自动化测试(简称自动化UI测试)很具挑战。.NET环境在System.Reflection 命名空间中提供了许多类,通过使用这些类可以访问和操作运行中的应用程序。应用反射,我们可以编写轻量级自动化UI测试。例如,假设我们有一个如图2-1上面的那张图(名为Form1的那张图)显示的简单的winform应用程序。

 

Figure 2-1.Reflection-based UI testing

A user types paper,rock,or scissors intothe TextBox control,and a second user selects one

of those strings from the ComboBoxcontrol.When either user clicks on the Button control,a

message with the winner is displayed in theListBox control.The key code for this dummy

application is:

该程序实现划拳功能,一个用户在TextBox控件中输入paper(布)、rock(石头)或者scissors(剪刀),第二个用户从Comobox控件中选择paper、rock或scissors。任何一个用户点击Button控件后,在ListBox控件中将显示获胜者的信息。这个简单的应用的关键代码如下:

private void button1_Click(objectsender,System.EventArgs e)

{

string tb=textBox1.Text;

string cb=comboBox1.Text;

if(tb==cb)

listBox1.Items.Add("Result is atie");

elseif(tb=="paper"&&cb=="rock"||

tb=="rock"&&cb=="scissors"||

tb=="scissors"&&cb=="paper")

listBox1.Items.Add("The TextBoxwins");

else

listBox1.Items.Add("The ComboBoxwins");

}

Note that this is not an example of goodcoding,and many deliberate errors are included.

For example,the ComboBox player can win byleaving the ComboBox control empty.This simulates

the unrefined character of an applicationwhile still under development.Using the techniques

in this chapter,you can write automated UItests as shown in the background of Figure 2-1.To

write reflection-based lightweight UI testautomation,you must be able to perform six tasks

programmatically(each test automation taskcorresponds to a section in this chapter):

1、Launch theapplication under test(AUT)from your test-harness program in a way that

allows the two programs to communicate.

2、Manipulate theapplication form to simulate a user moving and resizing the form.

3、Examine theapplication form properties to verify that the resulting state of the applica-

tion is correct so you can determine a testscenario pass or fail result.

4、Manipulate theapplication control properties to simulate actions such as a user typing

into a TextBox control.

5、Examine theapplication control properties to verify that the resulting state of the

application is correct so you can determinea test scenario pass or fail result.

6、Invoke theapplication methods to simulate actions such as a user clicking on a Button

control.

The techniques in this chapter are verylightweight.The main advantage of using these

reflection-based test techniques is thatthey are very quick and easy to implement.The main

disadvantages are that they apply only topure.NET applications and that they cannot deal

with complex test scenarios.The techniquesin Chapter 3 provide you with lower-level,more

powerful UI test-automation techniques atthe expense of increased complexity.

 注意到,这段代码不是不严谨,并且故意包含了一些错误。如:如果第二个用户不选,ComboBox为空,点击按钮,将显示ComboBox赢。这其实是模拟还处在开发中的应用程序不精炼的特征,我们可以编写图2-1下面的那张图(命令窗口的那张图)显示的自动化UI测试。要编写基于反射的轻量级自动化UI测试,我们必须编程完成6项任务(每项任务对应于本章中的一节)

1、在测试套件中加载被测应用程序,保证两个程序可以通信。

2、操作form应用程序,模拟用户移动form和调整form的大小。

3、通过检查form应用程序的属性来验证应用程序的结果状态是正确的,从而确定测试场景是通过还是失败。

4、通过操作应用程序控件的属性来模拟用户行为,如在TextBox中输入值。

5、通过检查应用程序控件的属性来验证应用程序的结果状态是正确的,从而确定测试场景是通过还是失败。

6、激活应用程序方法,模拟用户行为,如点击Button控件。

本节中介绍的技巧很轻量。使用基于反射的测试技巧最大的优点是它们容易并且快捷的实现,最大的缺点是只能应用到纯.net的应用程序,并且不能处理复杂的测试场景。在第三节中将介绍底层的、更强大的自动化测试技巧,当然会更复杂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值