首页 测试 工具 查看内容

透析QTP自动化测试框架SAFFRON(上)

2014-4-24 22:36| 发布者: tianzc| 查看: 847| 评论: 0

摘要:   1、为什么要使用框架?  框架是一组自动化测试的规范、测试脚本的基础代码,以及测试思想、惯例的集合。可用于减少冗余代码、提高代码生产率、提高代码重用性和可维护性。例如QTestWare就是QTP自动化测试框架 ...

  Public Function BrowseTo (url)

  thirdlevel = ""

  Report micPass, "Navigate to URL", "Navigating to URL: " & Quote(url)

  If initialized Then

  Execute GenerateDescription("Browser") & "Navigate " & Quote(url)

  Else

  Launch "website", url

  End If

  Reporter.Filter = rfDisableAll

  End Function

  在脚本中,会判断是否初始化了浏览器,如果有则执行导航动作,导航到指定的URL。导航动作是执行这行脚本来完成的:

  Execute GenerateDescription("Browser") & "Navigate " & Quote(url)

  Execute是一个用于执行指定VBScript脚本语句的函数,GenerateDescription函数的定义如下所示:

  ' Generates a generic description based up on the "level" viarable

  ' levelstr - will be one of the values that is in the level array

  ' returns - string representative of the object hierarchy

  Public Function GenerateDescription (levelstr)

  l = IndexOf(level, levelstr)

  If l >=0 Then

    fdesc = level(0) & "(" & Quote(desc(0)) & ")."

    If l >= 1 Then

      fdesc = fdesc + level(1) & "(" & Quote(desc(1)) & ")."

       If 2 >= l Then

          If thirdlevel <> "" Then

          fdesc = fdesc + level(2) & "(" & Quote(desc(2)) & "," & Quote("name:=" & thirdlevel) & ")."

          End If

       End If

    End If

   End If

  GenerateDescription = fdesc

  End Function

  4.2 返回测试对象的描述

  GenerateDescription函数用于返回对象的描述性语句,例如,指定Browser,则返回如下语句:

  "Browser("micclass:=Browser")."

  该语句代表了当前浏览器对象,并且后面加了个点号,这是为了方便后接"Navigate "这个浏览器对象的导航操作,以及指定的URL字符串,例如"http://blog.csdn.net/testing_is_believing"。在Execute时,其实执行的VBScript语句如下所示:

  Browser("micclass:=Browser").Navigate "http://blog.csdn.net/testing_is_believing"

  经过SAFFRON的框架封装后,则只需要使用如下语句即可达到同样的效果:

  BrowseTo "http://blog.csdn.net/testing_is_believing"

  4.3 启动浏览器

  SAFFRON使用名为BrowseTo函数来负责导航到指定的URL,但是如果浏览器未启动,则会先调用函数Launch来打开浏览器。Launch函数的定义如下所示:

  prepares the framework for usage, and configures all internal framework

  ' variables and structures

  ' apptype - used to launch different types of applications based

  ' upon different technologies -- currently there is only web

  ' val - string that represents what to launch

  ' returns - always returns true

  Public Function Launch (apptype, val)

  If "website" = apptype Then

  thirdlevel = ""

  Report micPass, "Initialize", "Initializing Framework"

  level = split(webLevels, leveldelimiter, -1, 1)

  desc = split(webLevelsDesc, leveldescdelimiter, -1, 1)

  object = split(objects, objectdelimiter, -1, 1)

  objectDescription = split(objectsDescription, objectsDescriptiondelimiter, -1, 1)

  CloseBrowsers

  Set IE = CreateObject("InternetExplorer.Application")

  IE.visible = true

  IE.Navigate val

  While IE.Busy

  wait 1

  Wend

  End If

  initialized = true

  Launch = true

  End Function

  可看到脚本中创建了IE的COM对象,然后设置IE的Visible属性设置为Tue,让浏览器可见,然后调用IE对象的Navigate方法导航到指定的URL。除了创建IE的COM对象外,在Launch函数中还进行框架其它方面的初始化。

  4.4 给指定字符串前后加双引号

  在BrowseTo函数的定义脚本中,调用了一个名为Quote的函数,该函数的定义如下所示:

  ' generates a string with embedded/surrounding quotes

  Public Function Quote (txt)

  Quote = chr(34) & txt & chr(34)

  End Function

  该函数的作用是给指定的字符串前后加上双引号字符,例如下面代码

  Msgbox "The message is " & Quote("hello world!")

  执行结果显示如图所示。

  如果我们不使用这个函数,则需要这样写我们的代码来实现同样的功能:

  Msgbox "The message is ""hello world!"""

  很明显,这样的写法写出来的代码的可读性和可维护性都差一截。

  4、SAFFRON框架代码剖析

  为了深入了解SAFFRON,以及框架的使用方法,下面我们将分别介绍SAFFRON中的主要函数,对SAFFRON代码进行深入剖析。

  4.1 导航到指定URL

  SAFFRON使用名为BrowseTo函数来负责导航到指定的URL,如果浏览器尚未启动,则先调用函数Launch来打开浏览器。BrowseTo函数的定义如下所示:


鲜花

握手

雷人

路过

鸡蛋

扫一扫关注最新动态

毒镜头:老镜头、摄影器材资料库、老镜头样片、摄影
爱评测 aipingce.com  
返回顶部