自动化测试代码赏析
上一篇 / 下一篇 2008-09-11 11:43:37 / 个人分类:自动化测试
如果你有兴趣,请把代码分析的结果写下来,因为看别人的代码,是提高自己自身能力的最快途径之一,当下公司招聘很多都需要测试人员懂工具,有的公司在面试过程中还拿出代码让测试人员来读,分析代码的结果是什么。古人说 "熟读唐诗三百首,不会做诗也会吟",希望你也成为一个自动化测试开发高手:
代码 一:
txtSearch = "T"
Set edtxt = CreateObject("Wscrīpt.Shell")
Browser(" ").Page(" ").WebEdit(" ").Click
edtxt.SendKeys(txtSearch)
Set edtxt = Nothing
rowCount = Browser(" ").Page(" ").WebTable(" ").GetROProperty("rows")
For j = 1 to rowCount
strTxt = Browser(" ").Page(" ").WebTable(" ").GetCellData(j,1)
strColor = Browser(" ").Page(" ").WebTable(" ").Object.rows(j-1).cells(1).currentStyle.backgroundColor
If strColor = "#fffde8" Then
reporter.ReportEvent micDone, "Text Highlighted", "The text """&strTxt&""" is highlighted."
strPos = left(strTxt,1)
If (Trim(txtSearch) = Trim(strPos)) Then
reporter.ReportEvent micPass, "Highlighted", "First Letter of highlighted text """&strTxt&""" starts with """&strPos&"""."
Else
reporter.ReportEvent micFail, "Highlighted", "Text """&strTxt&""" doesn't start with letter """&strPos&"""."
End If
End If
Next
代码 二:
Set ōbjDesc = Descrīption.Create()
objDesc("micclass").Value = "WebEdit"
Set ōbjWE = Browser().Page().ChildObjects(objDesc)
objWECount = objWE.Count
For i = 0 to objWECount-1
Set ōbjCP = objWE(i).Object
ChkPointWE = objCP.IsContentEditable
Print objWE(i).GetROProperty("name")
Print ChkPointWE
Next
代码 三:自动化测试框架代码,本框架基于SAFFRON(mercury)框架为基础实现的针对mercury自带的网上售票系统的关键字数据驱动其中SAFFRON(mercury)部分:
框架核心代码:
'************************************************************
' 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 = ""
objectDescrīption = ""
levelsubdescrīptiondelimiter = ","
leveldescdelimiter = "|"
objectdelimiter = "|"
leveldelimiter = "|"
objectsDescrīptiondelimiter = "|"
webLevels = "Browser|Page|Frame"
webLevelsDesc = "micclass:=Browser|micclass:=Page|micclass:=Frame|"
objects = "Link|WebButton|WebList|WebEdit"
objectsDescrīption = "micclass:=Link|micclass:=WebButton|micclass:=WebList|micclass:=WebEdit"
' Generates a generic descrīption 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 GenerateDescrīption (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
GenerateDescrīption = fdesc
End Function
' Generates an object descrīption based upon the object, and objectDescrīption 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 descrīption
Public Function GenerateObjectDescrīption (obj, prop)
i = IndexOf(object, obj)
ndesc = ""
If i <> -1 Then
ndesc = obj & "(" & Quote(objectDescrīption(i)) & "," & Quote(prop) & ")."
End If
GenerateobjectDescrīption = 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 GenerateDescrīption("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 GenerateDescrīption("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)
objectDescrīption = split(objectsDescrīption, objectsDescrīptiondelimiter, -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 = GenerateDescrīption(level(2))
Else
localDesc = GenerateDescrīption(level(1))
End If
AutoSync()
Select Case objtype
Case "Page"
Execute "rval = " & GenerateDescrīption(level(1)) & "Exist (0)"
If rval Then
Execute "title = " & GenerateDescrīption(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 & GenerateObjectDescrīption("Link", "innertext:=" & text)
Case "WebButton"
estr = "rval = " & localDesc & GenerateObjectDescrīption("WebButton", "value:=" & text)
Case "WebList"
estr = "rval = " & localDesc & GenerateObjectDescrīption("WebList", "name:=" & text)
Case "WebEdit"
estr = "rval = " & localDesc & GenerateObjectDescrīption("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 = GenerateDescrīption(level(2))
Else
localDesc = GenerateDescrīption(level(1))
End If
AutoSync()
Select Case objtype
Case "Link"
Execute localDesc & GenerateObjectDescrīption("Link","innertext:=" & text) & "Click"
Report micPass, "Link Activation", "The Link " & Quote(text) & " was clicked."
Case "WebButton"
Execute localDesc & GenerateObjectDescrīption("WebButton", "value:=" & text) & "Click"
Report micPass, "WebButton Activation", "The WebButton " & 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 = GenerateDescrīption(level(2))
Else
localDesc = GenerateDescrīption(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescrīption("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 = GenerateDescrīption(level(2))
Else
localDesc = GenerateDescrīption(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescrīption("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 = GenerateDescrīption(level(2))
Else
localDesc = GenerateDescrīption(level(1))
End If
AutoSync()
Select Case objtype
Case "WebEdit"
Execute "text = " & localDesc & GenerateObjectDescrīption("WebEdit", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
Case "WebList"
Execute "text = " & localDesc & GenerateObjectDescrīption("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
基于此框架的qtp网上售票的系统的框架代码例子想要的人请加:pcl2004_19@hotmail.com
相关阅读:
- 如何卸载RFT6.1.1 (karli539, 2008-8-31)
- 基于GUI的自动化测试工具 (fishy, 2008-9-04)
- 如何学习自动化测试(转) (fiena, 2008-9-07)
- 一个工作任务的解决方法 (pcl2004_27, 2008-9-07)
- Qtp 9.5 温柔破解试验 一步破解成功 (pcl2004_27, 2008-9-08)
- 如何学习自动化测试(转载) (sixsigmay, 2008-9-08)
- 解决RationalRobot获取htmlTableCell数据的技术 (pcl2004_27, 2008-9-09)
- 自动化测试闲言杂语 (caption, 2008-9-10)
- unregister相关 (karli539, 2008-9-10)
- tcl 和 shell 实现自动化启动远程shell (superfang, 2008-9-11)
TAG: 自动化测试
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 185610
- 日志数: 169
- 图片数: 10
- 文件数: 4
- 建立时间: 2006-11-30
- 更新时间: 2012-08-18