自动化测试框架通用函数
自动化测试不仅仅是录制回放,录制回放被自动化测试工具厂商吹得很神奇,却祸害了不少自动化测试初学者,甚至主导自动化测试项目的负责人。
自动化测试需要一个适合项目实际情况的框架,这个框架要解决自动化测试脚本设计模式的问题、脚本管理和维护的问题。
在构建自动化测试框架的过程中,会有一些脚本函数或工具是普遍适用于不同的框架的,TIB自动化测试工作室在研发框架的过程中,汇集和积累了不少这类函数,例如邮件发送函数:
Public Function Bug_SendBySMTP(strFrom,strTo,subject,body,attachment,attr_name)
If (emailAddress="" Or subject="" ) Then
Call logFile("Function SendMailBySMTP","Error","Enter Argument Error")
Exit Function
Else
Set objSendMail = CreateObject("CDONTS.NewMail")
objSendMail.From = strFrom
objSendMail.To = strTo
objSendMail.Subject = subject
objSendMail.Body = body
If attachment<>"" And attr_name<>"" Then
objSendMail.AttachFile attachment,attr_name
End If
objSendMail.Send
Set objSendMail = Nothing
End If
End Function
获取IP地址的函数:
Public Function fnGetIP()
On Error Resume Next
ComputerName="."
Dim objWMIService,colItems,objItem,objAddress
Set objWMIService = GetObject("winmgmts:\\" & ComputerName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
For Each objAddress in objItem.IPAddress
If objAddress <> "" then
fnGetIP = objAddress
Exit Function
End If
Next
Next
On Error GoTo 0
End Function
压缩文件夹的函数:
Function fnZip(sSourceFolder,sTargetZIPFile)
'This function will add all of the files in a source folder to a ZIP file
'using Windows' native folder ZIP capability.
Dim oShellApp, oFSO, iErr, sErrSource, sErrDescription
Set oShellApp = CreateObject("Shell.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")
'The source folder needs to have a \ on the End
If Right(sSourceFolder,1) <> "\" Then sSourceFolder = sSourceFolder & "\"
On Error Resume Next
'If a target ZIP exists already, delete it
If oFSO.FileExists(sTargetZIPFile) Then oFSO.DeleteFile sTargetZIPFile,True
iErr = Err.Number
sErrSource = Err.Source
sErrDescription = Err.Description
On Error GoTo 0
If iErr <> 0 Then
fZip = Array(iErr,sErrSource,sErrDescription)
Exit Function
End If
On Error Resume Next
'Write the fileheader for a blank zipfile.
oFSO.OpenTextFile(sTargetZIPFile, 2, True).Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
iErr = Err.Number
sErrSource = Err.Source
sErrDescription = Err.Description
On Error GoTo 0
If iErr <> 0 Then
fZip = Array(iErr,sErrSource,sErrDescription)
Exit Function
End If
On Error Resume Next
'Start copying files into the zip from the source folder.
oShellApp.NameSpace(sTargetZIPFile).CopyHere oShellApp.NameSpace(sSourceFolder).Items
iErr = Err.Number
sErrSource = Err.Source
sErrDescription = Err.Description
On Error GoTo 0
If iErr <> 0 Then
fZip = Array(iErr,sErrSource,sErrDescription)
Exit Function
End If
fZip = Array(0,"","")
End Function
还有很多框架通用函数没有列举出来,例如读写Excel、操作文件夹和文件、访问数据库、FTP、字符串格式化、日志记录等等,这些框架通用函数在构建自动化测试框架过程中起到了重要的作用。
TIB自动化测试工作室的通用框架中大量使用这些函数,欢迎联系我们咨询框架开发和使用方面的内容。