灵月SK 发表于 2011-01-26 17:08

浅谈QTP中的测试对象及其进一步改进

在功能的自动化测试中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

  通过这种方式实现了测试对象的过度复用,更符合自动化测试的思想,在项目的测试过程中,我们只要不断的加入新类型的测试对象即可,关键是这些测试对象基本不需要怎么维护,便可以很好的进行复用,这些对象不仅仅在一个项目中可以使用,只要是同一类型的项目,我们都可以来使用这些对象,而不需要随着不同的项目我们还要去重复的去维护一个个的对象仓库了。这样大大的提高了我们的测试效率,也便于多人集体合作。

针对完全的手动开发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 = Nothing

End Function

*******************************************

qtp script. code

JavaWindow(getWindowByTitle(getText(TITLE))

  我们都知道,在软件开发中,界面上显示的文字,都是属于软件的资源而已,是不会写入到代码中去的,而是配置中软件的资源文件中而已,那在我们开发测试脚本的时候,同样这些具体的用于识别集体对象的属性不过也是属于一种资源而已,我们同样可以把这样资源写到我们的资源文件中去配置,这样当修改修改时,我们同样只需要去维护一份或几份资源文件而已,而不需要去到代码中去修改,这样大大增加了我们代码的可维护性。写一个简单的例子程序,来说明这个问题:

  这里只是一个简单demo,在实际使用中我们可以按照这种思路去开发自己的测试脚本。
页: [1]
查看完整版本: 浅谈QTP中的测试对象及其进一步改进