来源:

http://www.itestware.com/ctest/index.php?option=com_content&view=article&id=62:webqtp-saffron&catid=35:testing_is_believing

06年的时候,Mercury 提供了一个QTP自动化测试框架原型 - SAFFRON,可用于指导我们开发基于WEB的自动化测试框架,有时间会仔细分析一下SAFFRON,陆续POST一些研究成果出来。

下面是该框架的源代码:

'************************************************************
' S.A.F.F.R.O.N. Prototype 1.1'
' Simple Automation Framework For Remarkably Obvious Notes
' Copyright © 2006 Mercury Interactive Corporation
'
' Notes:
'
' Requires QuickTest Professional 9.1
'
' Author       : Adam Gensler
' Created      : July 12, 2006
' Last Updated : September 11, 2006
'
' This prototype framework is provided AS IS, and is meant
' to be used for instructional purposes.
'
' This framework is a prototype, and is not supported
' by Mercury Interactive.
'
'************************************************************
initialized = false
thirdlevel = ""
level = ""
desc = ""
object = ""
objectDescription = ""

levelsubdescriptiondelimiter = ","
leveldescdelimiter = "|"
objectdelimiter = "|"
leveldelimiter = "|"
objectsDescriptiondelimiter = "|"

webLevels = "Browser|Page|Frame"
webLevelsDesc = "micclass:=Browser|micclass:=Page|micclass:=Frame|"
' 扩展对Image对象的支持
objects = "Link|WebButton|WebList|WebEdit|Image"
objectsDescription = "micclass:=Link|micclass:=WebButton|micclass:=WebList|micclass:=WebEdit|micclass:=Image"

' 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

' 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

' given an array, returns the index of the value to search for
' ary - an array
' str - value to search for in an array
' returns - index in array
Public Function IndexOf (ary, str)
 val = -1
 For i = 0 to UBound(ary)
  If ary(i) = str Then
   val = i
  End If
 Next
 IndexOf = val
End Function

' configures framework to work within the context of a specific frame
' val - the Name of the frame to work within -- use Object Spy if you don't
'       already know the frame name
Public Function WorkInFrame (val)
 Report micPass, "Enter Frame", "Entered scope of frame " & Quote(val)
 thirdlevel = val
End Function

' configures the framework to work outside the context of a specific frame
Public Function StopWorkingInFrame
 Report micPass, "Exit Frame", "Exited scope of frame " & Quote(thirdlevel)
 thirdlevel = ""
End Function

' generates a string with embedded/surrounding quotes
Public Function Quote (txt)
 Quote = chr(34) & txt & chr(34)
End Function

' navigate to a site if the browser is already opened, otherwise run initialization
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

' waits for the web page to finish loading
Public Function AutoSync
    Execute GenerateDescription("Browser") & "Sync"
End Function

' close all opened browsers
Public Function CloseBrowsers
 If Browser("micclass:=Browser").Exist (0) Then
  Browser("micclass:=Browser").Close
 End If
 While Browser("micclass:=Browser", "index:=1").Exist (0)
  Browser("index:=1").Close
 Wend
 If Browser("micclass:=Browser").Exist (0) Then
  Browser("micclass:=Browser").Close
 End If
End Function

' 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

' Verify the Existence of an object
' objtype - values should be limited to values in the object array
' text    - multi-purpose argument that indicates what to verify
'         - for a link, or button, it's the text of the control
'         - for a list, it's the name of the control
'         - for a frame, it's the name of the frame
Public Function Verify (objtype, text)
   rval = false
 localDesc = ""
 estr = ""
 If thirdlevel <> "" Then
  localDesc = GenerateDescription(level(2))
 Else
  localDesc = GenerateDescription(level(1))
 End If

AutoSync()

Select Case objtype
 Case "Page"
  Execute "rval = " & GenerateDescription(level(1)) & "Exist (0)"
  If rval Then
   Execute "title = " & GenerateDescription(level(1)) & "GetROProperty(" & Quote("title") & ")"
   If title = text Then
    rval = true
   Else
    rval = false
   End If
  End If
 Case "CurrentFrame"
  If thirdlevel <> "" Then
   estr = "rval = " & localDesc
  End If
 Case "Link"
  estr =  "rval = " & localDesc & GenerateObjectDescription("Link", "innertext:=" & text)
 Case "WebButton"
  estr = "rval = " & localDesc & GenerateObjectDescription("WebButton", "value:=" & text)
 Case "WebList"
  estr = "rval = " & localDesc & GenerateObjectDescription("WebList", "name:=" & text)
 Case "WebEdit"
  estr = "rval = " & localDesc & GenerateObjectDescription("WebEdit", "name:=" & text)
 End Select

If estr <> "" Then
  Execute estr + "Exist (0)"
 End If

If rval Then
  Report micPass, objtype & " Verification", "The " & objtype & " " & Quote(text) & " was verified to exist"
 Else
  Report micFail, objtype & "  Verification", "The " & objtype & " " & Quote(text) & " was not found"
 End If

If "True" = rval Then
  rval = True
 Else
  rval = False
 End If

Verify = rval
End Function

' 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."
 ' 扩展对Image类型的按钮的支持
 Case "Image"
  Execute localDesc & GenerateObjectDescription("Image", "alt:=" & text) & "Click"
  Report micPass, "ImageButton Activation", "The ImageButton " & Quote(text) & " was clicked."
 End Select
End Function

' Selects a specific value from a listbox, or combobox
' objname - name of the control -- use Object Spy if you don't know the name property
' text    - the item in the combobox to select
Public Function SelectFromList (objname, text)
 localDesc = ""
 rv = ""
 rval = false
 If thirdlevel <> "" Then
  localDesc = GenerateDescription(level(2))
 Else
  localDesc = GenerateDescription(level(1))
 End If

AutoSync()

localDesc = localdesc & GenerateObjectDescription("WebList", "name:=" & objname)

Execute "cnt = " & localDesc & "GetROProperty(" & Quote("items count") & ")"
 For i = 1 to cnt
  Execute "rv = " & localDesc & "GetItem (" & i & ")"
  If rv = text Then
   rval = true
  End If
 Next

If rval Then
  Execute localDesc & "Select " & Quote(text)
 End If
 If rval Then
  Report micPass, "WebList Selection", "The WebList item " & Quote(text) & " was selected."
 Else
  Report micFail, "WebList Selection", "The WebList item " & Quote(text) & " was NOT found."
 End If

SelectFromList = rval
End Function

' Enters text into an edit field
' objname - name of the control -- use Object Spy if you don't know what it is
' text    - the text to enter into the control
Public Function EnterTextIn (objname, text)
 localDesc = ""
 rval = true
 If thirdlevel <> "" Then
  localDesc = GenerateDescription(level(2))
 Else
  localDesc = GenerateDescription(level(1))
 End If

AutoSync()

localDesc = localdesc & GenerateObjectDescription("WebEdit", "name:=" & objname)
 Execute localDesc & "Set (" & Quote(text) & ")"
 Report micPass, "Enter Text", "Text: " & Quote(text) & " was entered into " & Quote(objname)
 EnterTextIn = rval 
End Function

' Obtains text from a control
' objtype - is the type of control the get the text from
' objname - is the name of the control -- use Object Spy if you don't know the name
' returns - the text of the control
Public Function GetTextFrom (objtype, objname)
 text = ""
 localDesc = ""
 If thirdlevel <> "" Then
  localDesc = GenerateDescription(level(2))
 Else
  localDesc = GenerateDescription(level(1))
 End If

AutoSync()

Select Case objtype
  Case "WebEdit"
   Execute "text = " & localDesc & GenerateObjectDescription("WebEdit", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
  Case "WebList"
   Execute "text = " & localDesc & GenerateObjectDescription("WebList", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
 End Select
 Report micPass, "Capture Text", "Text: " & Quote(text) & " was captured from the control " & Quote(objname)
 GetTextFrom = text
End Function

' Wrapper for the Reporter.Report Event method
' - could be used to create custom reports more easily
' See Reporter.ReportEvent documentation for usage
Public Function Report (status, objtype, text)
 Reporter.Filter = rtEnableAll
 Reporter.ReportEvent status, objtype, text
 Reporter.Filter = rfDisableAll
End Function

转载于:https://www.cnblogs.com/vinyfeng/articles/1338471.html

转:一个简单的基于WEB的QTP自动化测试框架-SAFFRON相关推荐

  1. 一个简单的Java web服务器实现

    前言 一个简单的Java web服务器实现,比较简单,基于java.net.Socket和java.net.ServerSocket实现: 程序执行步骤 创建一个ServerSocket对象: 调用S ...

  2. 通过Dapr实现一个简单的基于.net的微服务电商系统(六)——一步一步教你如何撸Dapr之Actor服务...

    我个人认为Actor应该是Dapr里比较重头的部分也是Dapr一直在讲的所谓"stateful applications"真正具体的一个实现(个人认为),上一章讲到有状态服务可能很 ...

  3. 基于内容推荐python_用 Python 实现一个简单的基于内容的推荐引擎

    原标题:用 Python 实现一个简单的基于内容的推荐引擎 (点击上方公众号,可快速关注) 英文:Chris Clark 译文:伯乐在线专栏作者 - yaoyujia 链接:http://python ...

  4. 一款简单的基于Web的投票工具(PHP+SQLite 实现)

        最近实现了一个简单的投票工具--小兵投票(下文称"本软件").     本软件是一款简单的基于Web的投票工具,使用 PHP+SQLite 实现.      本软件是一款开 ...

  5. 通过Dapr实现一个简单的基于.net的微服务电商系统(十九)——分布式事务之Saga模式...

    目录: 一.通过Dapr实现一个简单的基于.net的微服务电商系统 二.通过Dapr实现一个简单的基于.net的微服务电商系统(二)--通讯框架讲解 三.通过Dapr实现一个简单的基于.net的微服务 ...

  6. 通过Dapr实现一个简单的基于.net的微服务电商系统(十八)——服务保护之多级缓存...

    很久没有更新dapr系列了.今天带来的是一个小的组件集成,通过多级缓存框架来实现对服务的缓存保护,依旧是一个简易的演示以及对其设计原理思路的讲解,欢迎大家转发留言和star 目录: 一.通过Dapr实 ...

  7. 通过Dapr实现一个简单的基于.net的微服务电商系统(十七)——服务保护之动态配置与热重载...

    在上一篇文章里,我们通过注入sentinel component到apigateway实现了对下游服务的保护,不过受限于目前变更component需要人工的重新注入配置以及重启应用更新componen ...

  8. 通过Dapr实现一个简单的基于.net的微服务电商系统(十六)——dapr+sentinel中间件实现服务保护...

    dapr目前更新到了1.2版本,在之前4月份的时候来自阿里的开发工程师发起了一个dapr集成Alibaba Sentinel的提案,很快被社区加入到了1.2的里程碑中并且在1.2 release 相关 ...

  9. 通过Dapr实现一个简单的基于.net的微服务电商系统(十二)——istio+dapr构建多运行时服务网格...

    多运行时是一个非常新的概念.在 2020 年,Bilgin Ibryam 提出了 Multi-Runtime(多运行时)的理念,对基于 Sidecar 模式的各种产品形态进行了实践总结和理论升华.那到 ...

最新文章

  1. mysql的binlog太多太大占用了空间的解决办法
  2. lpi linux认证权威指南 pdf,LPI Linux认证指南读书笔记
  3. 矩阵的逆矩阵怎么求_字节面试原题:求最大子矩阵的大小
  4. JavaScript中函数文档注释
  5. awk 系列Part5:如何使用 awk 复合表达式
  6. 云计算学习(1-1)云计算的定义
  7. Struts第一个案例搭建
  8. 读书 | 一切红利最终都是趋势红利
  9. Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法
  10. IOCP扩展方法AcceptEx, DisconnectEx, GetAcceptExSockaddr用法示例
  11. layer弹窗在键盘按回车将反复刷新_人生减负指南——iPad Pro+妙控键盘上手体验...
  12. 小白都能理解的FTRL
  13. ADS创建自己的Project模板
  14. 获取文件哈希值_迅雷是如何通过磁力链接获取资源的?
  15. Jquery UI常用插件
  16. 帝国cms标签--2
  17. 榆树计算机课,吉林省榆树市弓棚镇武龙中学校七年级信息技术:第九课 Excel表格计算 教案+课件 (2份打包)...
  18. 创建两个文本框,一个按钮。第 1 个文本框绑定任意键事件,敲击键盘任意可显示字符,在交互窗口中显示该字符;
  19. php中电话号码输入框,php中固定电话号码和手机号码正则表达式验证
  20. 小米盒子3S刷国际版

热门文章

  1. 反射setaccessible_反射技术
  2. c语言中手机系统,一种手机课堂C语言编程系统的制作方法
  3. 服务器计费系统安卓,GitHub - NWAFU/dms_client: 服务器计费系统(客户机端):用于统计租户的服务器使用情况...
  4. 操作系统导论部分章节习题
  5. 浏览器安全检查己通过_百度主动推送三项合一功能
  6. 小程序音频播放报10001 解决方案 errCode:10001, errMsg:errCode:602,err:error,not found param
  7. [微信小程序]上传单张和多张图片
  8. 第24课 《前端之路,以不变应万变》
  9. Linux Mint 19 安装Gnome Boxes 新建失败
  10. Phpcms V9手机门户设置教程:怎么用PC V9做手机网站