技术开发 频道

透析QTP自动化测试框架SAFFRON

  4.5 点击链接

  作为一个针对WEB应用的脚本框架,除了能启动浏览器导航到指定的页面外,还需要针对页面的各种元素进行测试操作,例如链接的点击、按钮的点击操作。在SAFFRON框架中,使用Activate函数来点击链接、按钮,其函数定义如下所示:

' Activates an object based upon its object type

  
' objtype - the type of object should be limited to values in the object array

  
' text - identifying text for the control - for a link, it's the text of the link

  
Public Function Activate (objtype, text)

  localDesc
= ""

  
If thirdlevel <> "" Then

  localDesc
= GenerateDescription(level(2))

  
Else

  localDesc
= GenerateDescription(level(1))

  
End If

  AutoSync()

  
Select Case objtype

  
Case "Link"

  
Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"

  Report micPass, "Link Activation", "The Link "
& Quote(text) & " was clicked."

  
Case "WebButton"

  
Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"

  Report micPass, "WebButton Activation", "The WebButton "
& Quote(text) & " was clicked."

  
End Select

  
End Function

  函数首先判断对象的类型,然后根据对象类型分别处理,如果是链接对象,则通过以下语句组合成可执行的VBScript语句,然后用Execute函数来执行:

  Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"

  如果是按钮对象,则组合成:

  Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"

  在这里,调用了GenerateObjectDescription函数,GenerateObjectDescription函数的作用与GenerateDescription函数的作用类似,都是用于返回一个测试对象的描述,不同的是GenerateObjectDescription函数需要传入测试对象的描述数组,GenerateObjectDescription函数的定义如下:

 ' Generates an object description based upon the object, and objectDescription arrays

  
' obj - name of the object in the object array

  
' prop - additional property to help uniquely identify the object

  
' returns - a string representative of the object description

  
Public Function GenerateObjectDescription (obj, prop)

  i
= IndexOf(object, obj)

  ndesc
= ""

  
If i <> -1 Then

  ndesc
= obj & "(" & Quote(objectDescription(i)) & "," & Quote(prop) & ")."

  
End If

  GenerateobjectDescription
= ndesc

  
End Function

  有了Activate函数,我们在写脚本的时候就可以充分利用,简化脚本的编写,例如下面是两句简单的脚本,分别点击页面上的一个链接和一个按钮:

  Activate "Link", "Person"

  Activate "WebButton", "Search"

  在Activate函数中,调用了一个名为AutoSync的函数,该函数的作用与QTP的Sync方法是一样的,只是在外面封装了一层,函数定义如下所示:

 ' waits for the web page to finish loading

  Public Function AutoSync

  Execute GenerateDescription("Browser") & "Sync"

  End Function

  AutoSync函数用于等待WEB页面加载完成。

  4.6 一个小例子

  到现在为止,我们可以使用SAFFRON的Launch、BrowserTo和Activate函数来编写简单的脚本启动浏览器,导航到指定的页面,点击链接和按钮,例如下面就是一个综合了这几个功能的脚本:

  ' 启动浏览器

  Launch "website","http://127.0.0.1:1080"

  ' 导航到http://127.0.0.1:1080/WebTours

  BrowseTo "http://127.0.0.1:1080/WebTours/"

  ' 点击名为“administration”的链接

  Activate "Link","administration"

  该脚本调用SAFFRON框架的Launch函数启动IE浏览器,然后导航到http://127.0.0.1:1080/WebTours,点击如图所示的页面中名为"administration"的链接。

  脚本的测试结果如图所示。


 
0
相关文章