Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
Dim qtTest 'As QuickTest.Test ' Declare a Test object variable
Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
qtApp.Launch ' Start QuickTest
qtApp.Visible = True ' Make the QuickTest application visible
' Set QuickTest run options
qtApp.Options.Run.CaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False
' set run settings for the test
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = projectPath&"\result" '运行结果保存到临时文件夹中
qtTest.Run qtResultsOpt, True
qtTest.Close
Set qtResultsOpt = Nothing ' Release the Run Results Options object
Set qtTest = Nothing ' Release the Test object
qtApp.quit
Set qtApp = Nothing ' Release the Application object
End Function
Function getProjectPath()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(wscript.scriptfullname)
getProjectPath = objFSO.GetParentFolderName(objFile)
Set objFSO = Nothing
Set objFile = Nothing
End Function作者: Jay-Yang84 时间: 2009-4-2 20:37 标题: 回复 1# 的帖子 Driver Action:
Call ImportUtilFun(FrameUtilFolder)
Function ImportUtilFun(ImportFolder)
Set fso = CreateObject("Scripting.FileSystemObject") '加载FrameUtil目录下的所有共享库函数的VBS文件
Set UtilFolder=fso.GetFolder(ImportFolder)
Set UtilFileCollection=UtilFolder.files
For each UtilFile in UtilFileCollection
Executefile UtilFile.path
Next
Set fso = nothing
End Function
Dim fso, ofile
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.fileexists(Environment("Log_Dir")&"\runtime.xml") Then
fso.deletefile Environment("Log_Dir")&"\runtime.xml"
End If作者: Jay-Yang84 时间: 2009-4-2 20:38
xmlLogPrint "<?xml version=""1.0"" encoding=""GB2312""?>"
'******************************************TestProject类,这个类中很多个测试 用例集(TestSets)******************************************************
'******************************************这里用 一个变量testSets表示,testSets是一个结构 ,请看下面注释******************************************
Class TestProject
Public Function StartTestProject (ExcelName) '这里传入一个excel表格的路径。
xmlLogPrint "<Project>"
InitTestProject ExcelName '用传入参数作一些初始化操作
'msgbox testSets.Count
For i =0To testSets.Count -1 '循环执行每个TestSet
Set objTestSet = new TestSet '构造一个TestSet对象
TestSetArrays = testSets.Items
TestSetArray = TestSetArrays(i)
xmlLogPrint "<"&TestSetArray(0)&">"
objTestSet.StartTestSet(TestSetArray) '传入一个test结构,执行TestSet
xmlLogPrint "<"&"/"&TestSetArray(0)&">"
Set objTestSet = nothing
Next
xmlLogPrint "<测试结果统计>"&"Successed"& Environment("numSuccess")&"例,"&"Failed"&Environment("numErrors")&"例!"
xmlLogPrint "</测试结果统计>"
xmlLogPrint "</Project>"
DataTable.DeleteSheet "TestSets"
End Function
Private Function InitTestProject(ExcelName)
Set testSets = CreateObject("Scripting.Dictionary")
TestSetExcelFile = ProjectDir&ExcelName
MyTestSets = "TestSets"
DataTable.AddSheet(MyTestSets)
Err.Clear
On Error Resume Next
DataTable.ImportSheet TestSetExcelFile,1, MyTestSets
If err.number = 0 Then
row_count = DataTable.GetSheet(MyTestSets).GetRowCount
Dim setArray(3)
For i = 1 To row_count
DataTable.GetSheet(MyTestSets).SetCurrentRow(i)
isRun = DataTable.Value("IDX", MyTestSets)
If isRun = "√" Then
testsetName = DataTable.Value("name", MyTestSets)
setArray(0)= testsetName 'testSets结构中包含用例集的name列
setArray(1)= DataTable.Value("table", MyTestSets) 'testSets结构中包含用例集的table列
setArray(2)= DataTable.Value("sheet", MyTestSets) 'testSets结构中包含用例集的table列
If err.number = 0 Then
testSets.Add testsetName, setArray
else
Msgbox "检查TestSets.xls表中的字段IDX, name, table, sheet 字段是否有错."
End If
End If
err.clear
Next
else
Msgbox "检查TestSets.xls表是否有打开"
End If
On Error Goto 0
End Function作者: Jay-Yang84 时间: 2009-4-2 20:39
Private testSets
End Class
'******************************************TestProject类,这个类中很多个测试 用例集(TestSets)******************************************************
'******************************************TestSet类,这个类中很多个业务流程(TestCases)******************************************************
Class TestSet
Public Function StartTestSet(testsetArray)
InitTestSet testsetArray
For i = 0 To testCases.Count -1
TestCaseArrays = testCases.Items
TestCaseArray = TestCaseArrays(i)
If TestCaseArray(3) = "√" Then
xmlLogPrint "<"&TestCaseArray(0)&">"
Set objTestCase = new TestCase
objTestCase.StartTestCase(TestCaseArray)
xmlLogPrint"<"&"/"&TestCaseArray(0)&">"
Set objTestCase = nothing
End If
Next
DataTable.DeleteSheet "TestCases"
End Function
Private Function InitTestSet(testsetArray)
testsetName = testsetArray(0)
testsetTable = testsetArray(1)
testsetSheet = testsetArray(2)
Environment("TestSetsTable") = testsetTable
Set testCases = CreateObject("Scripting.Dictionary")
testDataFolderName = ProjectDir&"testData"'测试数据文件所在的根目录
testsetTable = pathFind(testDataFolderName,testsetTable, "xls")'测试数据文件的绝对路径
MyTestCases = "TestCases"
DataTable.AddSheet(MyTestCases)
On error Resume Next
DataTable.ImportSheet testsetTable,1, MyTestCases'导入TestCases表
If not Err.Number = 0 Then
msgbox "请检查"&testsetTable&"是否存在或已经被打开,检查testsets.xls,中与之对应的table, sheet栏的名字"
else
row_count = DataTable.GetSheet(MyTestCases).GetRowCount
tempcaseName = ""
Dim caseArray(4)
For i = 1 To row_count-1
DataTable.GetSheet(MyTestCases).SetCurrentRow(i)
isRun = DataTable.Value("IDX", MyTestCases)
If isRun = "业务流程" Then
If Not tempcaseName = "" Then
caseArray(2) = i-1 '该case结束时的行数
testCases.Add testcaseName, caseArray
End If
testcaseName = DataTable.Value("bizName", MyTestCases)
caseArray(0) = testcaseName
caseArray(1) = i+1 '该case开始时的行数
tempcaseName = testcaseName
caseArray(3)= DataTable.Value("description", MyTestCases)
If not err.number = 0 Then
msgbox "检查"&testsetTable&"中IDX, bizName, description 中各字段是否有错!"
Err.Clear
End If
End If
Next
caseArray(2) = row_count
testCases.Add tempcaseName, caseArray '*****************************增加是否有重复的判断.
End IF
On error Goto 0
End Function
Private testsetName
Private testsetTable
Private testsetSheet
Private testCases
End Class
'******************************************TestCase类,这个类中很多个Tasks******************************************************
Class TestCase作者: Jay-Yang84 时间: 2009-4-2 20:39
Public Function StartTestCase(TestCaseArray)
InitTestCase TestCaseArray
For i = 0 To testTasks.Count-1
Set objTestTask = new TestTask
TestTaskArrays = testTasks.Items
TestTaskArray = TestTaskArrays(i)
objTestTask.StartTestTask(TestTaskArray)
taskLogPrint TestTaskArray(1)
Set objTestTask = nothing
Next
End Function
Private Function InitTestCase(TestCaseArray)
testcaseName = TestCaseArray(0)
casestartRow = TestCaseArray(1)
caseendRow = TestCaseArray(2)
Set testTasks = CreateObject("Scripting.Dictionary")
'task所在的数据表已经导入,TestCases表
MyTestTasks = "TestCases"
Dim j
j = 0
Dim taskArray(4)
On Error Resume Next
For i = casestartRow To caseendRow
DataTable.GetSheet(MyTestTasks).SetCurrentRow(i)
isRun = DataTable.Value("IDX", MyTestTasks)
If isRun = "√" Then
classoftesttask = DataTable.Value("bizName", MyTestTasks)
testtaskName = DataTable.Value("taskName", MyTestTasks)
tableOfTask = DataTable.Value("bizDataTable", MyTestTasks)
taskFilter = DataTable.Value("filter", MyTestTasks)
taskArray(0) = classoftesttask
taskArray(1) = testtaskName
taskArray(2) = tableOfTask
taskArray(3) = taskFilter
If Err.Number = 0 Then
testTasks.Add j, taskArray
j = j+1
else
msgbox "请检查"&MyTestCases&"表中IDX, bizName, description, filter,taskName各字段有没有错!"
End If
Err.Clear
End If
Next
On Error Goto 0
'******************************************TestTask类******************************************************
Class TestTask
Private Function InitTestTask(TestTaskArray)
classoftesttask = TestTaskArray(0)
testtaskName = TestTaskArray(1)
' msgbox testtaskName
tableOfTask = TestTaskArray(2)
'msgbox tableOfTask
taskFilter = TestTaskArray(3)
' msgbox taskFilter
End Function作者: Jay-Yang84 时间: 2009-4-2 20:39
Private Function ExecuteTask(taskDataSheetName, TaskDataFilePath)
DataTable.AddSheet(taskDataSheetName)
On Error Resume Next
DataTable.ImportSheet TaskDataFilePath,taskDataSheetName,taskDataSheetName
If Err.number = 0 Then
test_data_row_count = DataTable.GetSheet(taskDataSheetName).GetRowCount
' msgbox test_data_row_count
For j=1 To test_data_row_count
DataTable.GetSheet(taskDataSheetName).SetCurrentRow( j )
'msgbox taskDataSheetName
'msgbox taskFilter
If Eval(generateFilterExp(taskDataSheetName,taskFilter)) Then
'Eval(generateFilterExp(Sheet_Name,filterExp)) ,解析测试数据的条件语句,符合此条件的,则执行该行测试数据,否则不执行
str = "obj"&classoftesttask&"."&testtaskName&" "&chr(34)&taskDataSheetName&chr(34)
Execute str
ErrorXmlPrint '如果有错误则输出。
If not err.number = 0 Then
Environment("bResult") = "失败"
End If
Err.Clear
End If
Next
sheetName =taskDataSheetName
DataTable.DeleteSheet sheetName
else
Msgbox "检查"&TaskDataFilePath&"中是否有表"&taskDataSheetName
End if
On Error Goto 0
End Function
Public Function StartTestTask(TestTaskArray)
InitTestTask TestTaskArray
Environment("bResult") = "成功" '默认开始执行每一个的步骤为成功
If Not TestScriptObject.Exists (classoftesttask) then
taskFilePath = pathFind(Environment("testScriptFolderName"),classoftesttask,"vbs")
On Error Resume Next
ExecuteFile taskFilePath 'executefile qtp独有的加载外部的vbs 文件.
If Err.Number = 0Then
TestScriptObject.add classoftesttask,taskFilePath
Execute "Set obj"&classoftesttask&" = new "&classoftesttask
If not Err.Number = 0 Then
msgbox "查看"&taskFilePath&"中是否有"& classoftesttask&"类"
Err.Clear
End If
Else
msgbox "请查看"&taskFilePath&"是否存在或者taskFilePath中有错误!"
End If
On Error Goto 0
End If
'开始执行task
If Not tableOfTask="" Then
tempArray = split(tableOfTask,".")
If UBound(tempArray)=0 Then
TaskDataFilePath = pathFind(Environment("testDataFolderName"), Environment("TestSetsTable"),"xls")
ExecuteTask tempArray(0), TaskDataFilePath
else
'如果是引用的外部Excel的sheet,那么。。。
TaskDataFilePath = pathFind(Environment("testDataFolderName"),tempArray(0),"xls")
ExecuteTask tempArray(1), TaskDataFilePath
End If
Else
'如果Sheet名称为空则不加载
str = "Call obj"&classoftesttask&"."&testtaskName&"()"
On error resume Next
Execute str
ErrorXmlPrint
If not err.number = 0 Then
Environment("bResult") = "失败"
End If
On error Goto 0
End If
'计算成功和失败的用例数
If Environment("bResult") = "成功" Then
Environment("numSuccess") = Environment("numSuccess") +1
else
Environment("numErrors") = Environment("numErrors")+1
End If
Set objtestProject = New TestProject '构造一个自动化测试项目
objtestProject.StartTestProject "testSets.xls"
Set objtestProject = nothing
Set TestScriptObject = nothing作者: Jay-Yang84 时间: 2009-4-2 20:40
DiverUtil.vbs
'################################'
'---------------通用方法--------------------------'
'################################'
Function pathFind( searchingFolder,searchingFileName,fileType)
'根据传入的根目录,查找该目录下的指定名称,以及指定文件类型的文件,并返回其绝对路径。
tempArray = Split(searchingFileName,".")
fileName = tempArray(0)&"."&fileType
Set fso=CreateObject( "Scripting.FileSystemObject" )
Set objFolder = fso.GetFolder( searchingFolder )
Set objFileCollection = objFolder.Files
for each objFile in objFileCollection
If objFile.Name = fileName Then
i=i+1
searchedFilePath = objFile.Path
Exit for
End If
Next
If i=0 then
'遍历子文件夹
Set objSubFoldersCollection = objFolder.SubFolders
For each objInputSubFolder in objSubFoldersCollection
searchedFilePath= pathFind(objInputSubFolder,searchingFileName,fileType)
If searchedFilePath<>"" Then
Exit For
End if
Next
End If
pathFind = searchedFilePath
End Function
Function getParentFolderPath(curPath)
'输入一个路径的字符串,获得其上级目录的字符串,主要目的是根据QTP脚本所在文件夹,找到工程所在的文件夹
tempArray = split(curPath,"\")
tempStr =""
For i=LBound(tempArray) to UBound(tempArray)-1
tempStr = tempStr&tempArray(i)&"\"
Next
getParentFolderPath = tempStr
End Function
Function generateFilterExp(Sheet_Name,filterExp)
''解析条件语句,只支持 >= ,<= , <>, >, <, = 这6种表达式
'对表达式作了处理,支持中文的分号,不区分英文的大小写。
If filterExp<>"" Then
If InStr(filterExp,";")>0 Then
filterExp = Replace(filterExp,";",";")
End If
expressArray = Split(LCase(filterExp),";")
For i=LBound(expressArray) To UBound(expressArray)
If InStr(expressArray(i),">=") Then
tempArray = Split(expressArray(i),">=")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">="&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">="&chr(34)&tempArray(1)&chr(34)
End If
ElseIf InStr(expressArray(i),"<=") Then
tempArray = Split(expressArray(i),"<=")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<="&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<="&chr(34)&tempArray(1)&chr(34)
End If
ElseIf InStr(expressArray(i),"<>") Then
tempArray = Split(expressArray(i),"<>")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&tempArray(1)&chr(34)
End If
ElseIf InStr(expressArray(i),"<") Then
tempArray = Split(expressArray(i),"<")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<"&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<"&chr(34)&tempArray(1)&chr(34)
End If
ElseIf InStr(expressArray(i),">") Then
tempArray = Split(expressArray(i),">")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">"&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&">"&chr(34)&tempArray(1)&chr(34)
End If
ElseIf InStr(expressArray(i),"=") Then
tempArray = Split(expressArray(i),"=")
If i=LBound(expressArray) Then
expressStr = "DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"="&chr(34)&tempArray(1)&chr(34)
else
expressStr = expressStr&" and "&"DataTable("&Chr(34)&tempArray(0)&Chr(34)&","&Chr(34)&Sheet_Name&Chr(34)&")"&"="&chr(34)&tempArray(1)&chr(34)
End If
Else
MsgBox("不支持此表达式")
End If
Next
Else
expressStr = "DataTable( 1 "&","&Chr(34)&Sheet_Name&Chr(34)&")"&"<>"&chr(34)&chr(34)
End If
'logPrint("在generateFilterExp方法中,条件语句解析结果:"&expressStr)
generateFilterExp = expressStr
End Function作者: Jay-Yang84 时间: 2009-4-2 20:41
Function.vbs
Function PressEnter
wait(2)
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "~"
Set WshShell = nothing
End Function作者: Jay-Yang84 时间: 2009-4-2 20:41
Log.vbs
Public Function logPrint(ByVal logMessage)
Dim fso, logFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set logFile = fso.OpenTextFile(Environment("Log_Dir")&"\runtime.log", 8, True) 'Open a file and write to the end of the file and open as Unicode
Public Function ErrorHandle()
If Err.Number <> 0 Then
logPrint "Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description
Err.Clear
End If
End Function
Public Function xmlLogPrint(ByVal logMessage)
Dim fso, xmlfile
Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlfile = fso.OpenTextFile(Environment("Log_Dir")&"\runtime.xml", 8, True)
xmlfile.WriteLine logMessage
xmlfile.Close
Set xmlfile = nothing
Set fso = nothing
End Function
Public Function taskLogPrints (taskname)
Dim fso, xmlfile, str1, str2, str3
str1 = "<task>"
str2 = "<"&"/"&"task>"
Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlfile = fso.OpenTextFile(Environment("Log_Dir")&"\runtime.xml", 8, True)
xmlfile.WriteLine str1&"执行步骤 "&taskname&"成功"&str2
xmlfile.Close
Set xmlfile = nothing
Set fso = Nothing
End Function
Public Function taskLogPrint (taskname)
Dim fso, xmlfile, str1, str2, str3
str1 = "<"
str2 = ">"
str3 = "/"
Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlfile = fso.OpenTextFile(Environment("Log_Dir")&"\runtime.xml", 8, True)
xmlfile.WriteLine str1&taskname&str2&"执行步骤 "&taskname& Environment("bResult")&str1&str3&taskname&str2
xmlfile.Close
Set xmlfile = nothing
Set fso = Nothing
End Function
Public Function ErrorXmlPrint()
Dim fso, xmlfile, str1, str2, str3
strTemp = " 错误描述: " & Err.Description
Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlfile = fso.OpenTextFile(Environment("Log_Dir")&"\runtime.xml", 8, True)
If Err.Number <> 0 then
xmlfile.WriteLine "<Error>"&strTemp&"</Error>"
End IF
xmlfile.Close
Set xmlfile = nothing
Set fso = Nothing
End Function作者: Jay-Yang84 时间: 2009-4-2 20:42
TestScript:
Dome.vbs
Class Demo
Function login(Sheet_Name)
address = DataTable("address", Sheet_Name)
Systemutil.run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
wait(3)
Browser("micclass:=browser").close
msgbox address
End Function
Function logout()
msgbox "logout"
End Function
End Class作者: Jay-Yang84 时间: 2009-4-2 20:45
TestSet.xls: