-
关于qtp的测试对象的进一步改进
2009-04-19 14:05:20
这次是在上次写的文章“关于qtp的测试对象”,地址:http://www.51testing.com/index.php?uid-174344-action-viewspace-itemid-113944 的基础上的进一步讨论,这里的改进主要是针对完全的手动开发qtp自动化测试脚本而言的,对于录制后简单的修改并不使用。在功能的自动化测试中,测试对象是基础,为了提高测试对象的高度可复用性以及便于日后的升级和维护,我们应该使用尽量少的属性去标示出这个对象,例如:创建一个JavaWindow对象
set win = description.Create
win("class description").value = "window"
这样就足可以来表示出给对象了,但是当有多个JavaWindow对象对象同时存在时,这样创建时明显有问题的,这时我们就不得不再去增加一个title来识别出每一个不同的JavaWindow对象了,但是如果我们直接把title属性写入到代码中,这样在升级版本或客户要就修改标题后,我们就不得不去相应的到我们的代码中去修改这个标题,在一个大型的项目中会有很多地方需要去修改,这样既不容易该全,也很麻烦。解决办法:
我们都知道,在软件开发中,界面上显示的文字,都是属于软件的资源而已,是不会写入到代码中去的,而是配置中软件的资源文件中而已,那在我们开发测试脚本的时候,同样这些具体的用于识别集体对象的属性不过也是属于一种资源而已,我们同样可以把这样资源写到我们的资源文件中去配置,这样当修改修改时,我们同样只需要去维护一份或几份资源文件而已,而不需要去到代码中去修改,这样大大增加了我们代码的可维护性。写一个简单的例子程序,来说明这个问题:
get the JavaWindow object
function getWindowByTitle(title)
set win = description.Create
win("class description").value = "window"
win("title").value = title
set getWindowByTitle = win
end function
*********************************************
test.txt --this is the configure file
#the login dialog
TITLE="backup express 3.1"
*********************************************
this function get the value by name
Function getText(name)
Dim fso,cPath,f
cPath = "E:\javaGui.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(cPath,1,False)
Do While Not f.AtEndOfStream
str = f.ReadLine()
If Not InStr(str,"#") > 0 Then
tmpArr = Split(str,"=",-1,1)
If StrComp(tmpArr(0),name) = 0 Then
getText = tmpArr(1)
Exit Do
End If
End If
Loop
Set f = Nothing
Set fso = NothingEnd Function
*******************************************
qtp script. code
JavaWindow(getWindowByTitle(getText(TITLE))
这里只是一个简单demo,在实际使用中我们可以按照这种思路去开发自己的测试脚本。
以上仅是我自己的一点想法,欢迎大家讨论。
-
The different of run and exec method
2009-04-07 13:05:16
the run and exec method are very important method, I found a good article to introduce the two method, so I decided to share it, I hope this will help you if you need it, the detail as follows:
The fact that there are two ways to run programs from a script. leads to an obvious question: which method should you use in your scripts? The answer to that
question depends on the script. and what it needs to accomplish.
script. can use either the Run method or the Exec method to run a program in a
manner similar to using the Run dialog box from the Start menu. Regardless of the
method used, the program starts, and runs in a new process.
However, when you use the Run method, your script. will not have access to the
standard input, output, and error streams generated by the program being run. A
script. cannot use the Run method to run a command-line tool and retrieve its
output.
For example, suppose you want to run Ping.exe and then examine the output to
see whether the computer could be successfully contacted. This cannot be done
using the Run command. Instead, you would need to ping the computer, save the
results of the ping command to a text file, open the text file, read the results, and
then parse those results to determine the success or failure of the command.
The following script. uses the Run method to call Ping.exe, redirecting the output
to a temporary file. The script. opens and reads the text file, checks to see whether
the command succeeded (by determining whether any of the lines of output begin
with the word Reply), and then closes and deletes the temporary file:
Option Explicit
Dim oShell, oFSO, oTextFile
Dim sTempName, sText
Dim sComputer : sComputer = "62.241.53.16"
'--- Creating a shell object
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
sTempName = oFSO.GetTempName
oShell.Run "%comspec% /c ping -n 3 -w 1000 " & sComputer & " > " & sTempName, 0, True
'--- Reading from output file
Set TextFile = oFSO.OpenTextFile(sTempName, 1)
Do While oTextFile.AtEndOfStream <> True
sText = oTextFile.ReadLine
If Instr(sText, "Reply") > 0 Then
MsgBox "Reply received."
Exit Do
End If
Loop
'--- Closing and deleting temporary file
oTextFile.Close : oFSO.DeleteFile(sTempName)
'--- Cleaning used objects
Set TextFile = Nothing : Set FSO = Nothing
Set Shell = Nothing
Although this approach works, it is somewhat complicated. If you need access to
command-line output, you should use the Exec method instead. The following
script. also parses the output generated by Ping.exe. However, it does so by using
the Exec method and by directly reading the output. There is no need to create,
open, read, and delete a temporary file, and the script. is only 9 lines long,
compared with the 15 lines required to perform. this same task using the Run
method:
Option explicit
Dim oShell, oExec
Dim sText
Dim sComputer : sComputer = "62.241.53.16"
'--- Creating a shell object
Set Shell = CreateObject("WScript.Shell")
Set Exec = oShell.Exec("cmd /c ping -n 3 -w 1000 " & sComputer)
'--- Reading from output stream
Do While Not oExec.StdOut.AtEndOfStream
sText = oExec.StdOut.ReadLine()
If Instr(sText, "Reply") > 0 Then
MsgBox "Reply received."
Exit Do
End If
Loop
'--- Cleaning used objects
Set Shell = Nothing
In many respects, this makes the Exec method a better choice than the Run
method. However, the Run method is still useful in a number of situations:
You might want to run the application in a specified window type, such as a
minimized window. Exec offers no control over window style; Run offers
Chapter 09 Scripting Quicktest Professional Page 43
Dani Vainstein Windows Script. Host Page 43 of 73
more options.
You might need to run a script. on computers that do not have WSH 5.6
installed. Exec is supported only on WSH 5.6.
You might want to wait for the application being called to finish running
before the script. resumes. This can be done with either Run or Exec but
requires less coding with Run.
-
浅谈QTP中的测试对象
2009-03-31 15:11:13
在功能的自动化测试中QTP被人们广泛的使用着,关于使用qtp自身的对象仓库的方法来管理测试对象的方式,本人认为存在着很大的局限性,在这里介绍一种通过函数的方式来管理和使用测试对象的方法,该方法的思想是基于利用函数来实现的,把要测试的每一类对象通过一个或几个公用的函数来实现,下面通过具体的code来说明,先建立一个TestObject.qfl的文件,用来模拟对象仓库的功能。
Function getWindow
Set win = description.Create
win("class description").value = "window"
Set getWindow = win
End Function
Function getWindowByTitle(title)
Set win = description.Create
win("class description").value = "window"
win("title").value = title
Set getWindowByTitle = win
End Function
Function getDialog
Set dia = description.Create
dia("class description").value = "window"
Set getDialog = dia
End Function
Function getDialogByTitle(title)
Set dia = description.Create
dia("class description").value = "window"
dia("title").value = title
Set getDialogByTitle = dia
End Function
Function getEdit
Set edt = description.Create
edt("class description").value = "edit"
Set getEdit = edt
End Function
Function getEditByText(text)
Set edt = description.Create
edt("class description").value = "edit"
edt("attached text").value = text
Set getEditByText = edt
End Function
Function getEditByIndex(index)
Set edt = description.Create
edt("class description").value = "edit"
edt("index").value = index
Set getEditByIndex = edt
End Function
Function getButton
Set btn = description.Create
btn("class description").value = "push_button"
Set getButton = btn
End Function
Function getButtonByText(text)
Set btn = description.Create
btn("class description").value = "push_button"
btn("attached text").value = text
Set getButtonByText = btn
End Function
我们通过以上这种方式,可以把我们测试项目中用到的所有的测试对象加入到这个“对象仓库”中来,在使用的时候,只需要调用相应的函数即可实现,如下面的coad所示。
SystemUtil.Run "fileName","","filePath"
If JavaWindow(getWindow).JavaDialog(getDialog).Exist(30) Then
With JavaWindow(getWindow).JavaDialog(getDialog)
.JavaEdit(getEditByText("User Name")).Set "userName"
.JavaEdit(getEditByText("Password")).Set "userPwd"
.JavaButton(getButtonByText("Ok")).Click
End with
Else
Reporter.ReportEvent micFail,"lauch error","launch error, please check the application!"
ExitTest
End If
通过这种方式实现了测试对象的过度复用,更符合自动化测试的思想,在项目的测试过程中,我们只要不断的加入新类型的测试对象即可,关键是这些测试对象基本不需要怎么维护,便可以很好的进行复用,这些对象不仅仅在一个项目中可以使用,只要是同一类型的项目,我们都可以来使用这些对象,而不需要随着不同的项目我们还要去重复的去维护一个个的对象仓库了。这样大大的提高了我们的测试效率,也便于多人集体合作。
标题搜索
我的存档
数据统计
- 访问量: 9695
- 日志数: 9
- 建立时间: 2008-03-12
- 更新时间: 2015-05-31