浅谈QTP中的测试对象

发表于:2009-05-07来源:作者:点击数: 标签:qtpQTP对象
在功能的 自动化测试 中 QTP 被人们广泛的使用着,关于使用 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

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

原文转自:http://www.ltesting.net