|
本帖最后由 测试工坊 于 2012-7-29 18:06 编辑
刚做了几个针对Web的自动化测试项目,简单总结一下,仅供缺乏实际项目经验的同僚参考使用。
下面是外部调用的示例,更多需求请参考帮助文件中的自动化对象模型部分。- '--------------------------------------------------------
- Const runLevel = n ' which level are you goint to run? set 0 to run all pages
- Const endLine = n ' Enter the number of the end line here.
- TestLinkFlag = False' This is a falg to determine if the Links should be tested.
- '--------------------------------------------------------
- currentPath=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
- testPath = currentPath & "script\"
- excelPath = currentPath & "\sitemap.xlsx"
- Public runAction
- Public qtApp
- Public testArray
- Class caseClass
- Public TestName
- Public Priority
- Public ActionName
- End Class
- SetActionName runLevel,endLine,excelPath
- tName = ""
- For Each arr In testArray
- If arr.Priority = runLevel Then
- runAction = runAction&arr.ActionName&"|"
- If Not inStr(tName,arr.TestName) = 1 Then
- tName = arr.TestName
- testString = testString & testPath & tName & "|"
- End If
- End If
- Next
- 'If none of the action in the test needs to be executed, then pass this test.
- If Len(runAction)> 0 Then runAction = runAction & "init|Finalize|Header|Footer"
- runTestPath = Split(testString,"|")
- 'Load test
- Set fso = CreateObject("Scripting.FileSystemObject")
- For Each testPath In runTestPath
- If Len(testPath) > 0 And fso.FolderExists(testPath) Then
- RunQTPTest testPath
- End If
- Next
- 'Invoke QTP
- Function RunQTPTest(testPath)
- Set qtApp = CreateObject("QuickTest.Application")
- arrTestAddins = qtApp.GetAssociatedAddinsForTest(testPath)
- blnNeedChangeAddins = False
-
- For Each testAddin In arrTestAddins
- If qtApp.Addins(testAddin).Status <> "Active" Then
- blnNeedChangeAddins = True
- Exit For
- End If
- Next
-
- If qtApp.Launched And blnNeedChangeAddins Then
- qtApp.Quit
- End If
-
- If blnNeedChangeAddins Then
- blnActivateOK = qtApp.SetActiveAddins(arrTestAddins, errorDescription)
- If Not blnActivateOK Then
- MsgBox errorDescription
- WScript.Quit
- End If
- End If
-
- If Not qtApp.Launched Then
- qtApp.Launch
- End If
-
- qtApp.Open testPath,True
-
- qtApp.Test.Environment.Value("runAction") = runAction
- qtApp.Test.Environment.Value("LinkFlag") = TestLinkFlag
-
- qtApp.Visible = True
- qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
- qtApp.Options.Run.RunMode = "Fast"
- qtApp.Options.Run.ViewResults = False
-
- Set qtTest = qtApp.Test
- qtTest.Settings.Run.OnError = "NextStep"
-
- qtTest.run
- 'qtApp.quit
- Set qtApp = Nothing
- Set qtTest = Nothing
- End Function
- Public Function SetActionName(runLevel,endLine,excelPath)
- Set objExcel = GetExcelObject(excelPath)
-
- Dim actionArray()
- arrIndex = 0
-
- For i = 2 To endLine
- ActionName = GetCellValue(objExcel,"Sitemap",i,4)
- Priority = GetCellValue(objExcel,"Sitemap",i,5)
- url = GetCellValue(objExcel,"Sitemap",i,3)
- If GetCellValue(objExcel,"Sitemap",i,1)<> "" Then TestName = GetCellValue(objExcel,"Sitemap",i,1)
-
- If url <> "" Then
- ReDim Preserve actionArray(arrIndex)
- Set actionArray(arrIndex) = New caseClass
- actionArray(arrIndex).Priority = Priority
- actionArray(arrIndex).ActionName = ActionName
- actionArray(arrIndex).TestName = TestName
- arrIndex = arrIndex + 1
- End If
- Next
- testArray = actionArray
- objExcel.Save
- objExcel.close
- Set w = CreateObject("Wscript.Shell")
- w.run "Library/GetResultByAction.vbs"
- Set w = Nothing
- End Function
- Function GetExcelObject(excelPath)
- Set objExcel = CreateObject("Excel.Application")
- Set GetExcelObject = objExcel.Workbooks.Open (excelPath)
- End Function
- Function GetCellValue(objExcel, sheetName, row, col)
- objExcel.Worksheets(sheetName).Activate
- GetCellValue = objExcel.Worksheets(sheetName).Cells(row, col)
- End Function
复制代码 |
|