51Testing软件测试论坛

标题: QTP针对WEB项目自动化开发的简单总结 [打印本页]

作者: 测试工坊    时间: 2012-7-29 18:05
标题: QTP针对WEB项目自动化开发的简单总结
本帖最后由 测试工坊 于 2012-7-29 18:06 编辑

刚做了几个针对Web的自动化测试项目,简单总结一下,仅供缺乏实际项目经验的同僚参考使用。

[attach]80227[/attach]
[attach]80228[/attach]
[attach]80226[/attach]

下面是外部调用的示例,更多需求请参考帮助文件中的自动化对象模型部分。
  1. '--------------------------------------------------------
  2. Const runLevel = n ' which level are you goint to run? set 0 to run all pages
  3. Const endLine = n ' Enter the number of the end line here.
  4. TestLinkFlag = False' This is a falg to determine if the Links should be tested.
  5. '--------------------------------------------------------

  6. currentPath=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
  7. testPath = currentPath & "script\"
  8. excelPath = currentPath & "\sitemap.xlsx"

  9. Public runAction
  10. Public qtApp
  11. Public testArray

  12. Class caseClass
  13.         Public TestName
  14.         Public Priority
  15.         Public ActionName
  16. End Class

  17. SetActionName runLevel,endLine,excelPath

  18. tName = ""


  19. For Each arr In testArray
  20.         If arr.Priority = runLevel Then
  21.                 runAction = runAction&arr.ActionName&"|"
  22.                 If Not inStr(tName,arr.TestName) = 1 Then
  23.                         tName = arr.TestName
  24.                         testString = testString & testPath & tName & "|"
  25.                 End If
  26.         End If
  27. Next

  28. 'If none of the action in the test needs to be executed, then pass this test.
  29. If Len(runAction)> 0 Then runAction = runAction & "init|Finalize|Header|Footer"

  30. runTestPath = Split(testString,"|")

  31. 'Load test
  32. Set fso = CreateObject("Scripting.FileSystemObject")
  33. For Each testPath In runTestPath
  34.         If Len(testPath) > 0 And fso.FolderExists(testPath) Then
  35.                 RunQTPTest testPath
  36.         End If
  37. Next


  38. 'Invoke QTP
  39. Function RunQTPTest(testPath)

  40.         Set qtApp = CreateObject("QuickTest.Application")
  41.         arrTestAddins = qtApp.GetAssociatedAddinsForTest(testPath)
  42.         blnNeedChangeAddins = False
  43.        
  44.         For Each testAddin In arrTestAddins
  45.             If qtApp.Addins(testAddin).Status <> "Active" Then
  46.                 blnNeedChangeAddins = True
  47.                 Exit For
  48.             End If
  49.         Next
  50.        
  51.         If qtApp.Launched And blnNeedChangeAddins Then
  52.                 qtApp.Quit
  53.         End If
  54.        
  55.         If blnNeedChangeAddins Then
  56.             blnActivateOK = qtApp.SetActiveAddins(arrTestAddins, errorDescription)
  57.             If Not blnActivateOK Then
  58.             MsgBox errorDescription
  59.             WScript.Quit
  60.             End If
  61.         End If
  62.        
  63.         If Not qtApp.Launched Then
  64.             qtApp.Launch
  65.         End If
  66.        
  67.         qtApp.Open testPath,True
  68.        
  69.         qtApp.Test.Environment.Value("runAction") = runAction
  70.         qtApp.Test.Environment.Value("LinkFlag") = TestLinkFlag
  71.        
  72.         qtApp.Visible = True
  73.         qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
  74.         qtApp.Options.Run.RunMode = "Fast"
  75.         qtApp.Options.Run.ViewResults = False
  76.        
  77.         Set qtTest = qtApp.Test
  78.         qtTest.Settings.Run.OnError = "NextStep"
  79.        
  80.         qtTest.run
  81.         'qtApp.quit
  82.         Set qtApp = Nothing
  83.         Set qtTest = Nothing
  84. End Function

  85. Public Function SetActionName(runLevel,endLine,excelPath)
  86.         Set objExcel = GetExcelObject(excelPath)
  87.        
  88.         Dim actionArray()
  89.         arrIndex = 0
  90.        
  91.         For i = 2 To endLine
  92.                 ActionName = GetCellValue(objExcel,"Sitemap",i,4)
  93.                 Priority = GetCellValue(objExcel,"Sitemap",i,5)
  94.                 url = GetCellValue(objExcel,"Sitemap",i,3)
  95.                 If GetCellValue(objExcel,"Sitemap",i,1)<> "" Then TestName = GetCellValue(objExcel,"Sitemap",i,1)
  96.                
  97.                 If url <> "" Then
  98.                         ReDim Preserve actionArray(arrIndex)
  99.                         Set actionArray(arrIndex) = New caseClass
  100.                         actionArray(arrIndex).Priority = Priority
  101.                         actionArray(arrIndex).ActionName = ActionName
  102.                         actionArray(arrIndex).TestName = TestName
  103.                         arrIndex = arrIndex + 1
  104.                 End If
  105.         Next
  106.         testArray = actionArray
  107.         objExcel.Save
  108.         objExcel.close
  109.         Set w = CreateObject("Wscript.Shell")
  110.         w.run "Library/GetResultByAction.vbs"
  111.         Set w = Nothing
  112. End Function

  113. Function GetExcelObject(excelPath)
  114.         Set objExcel = CreateObject("Excel.Application")
  115.         Set GetExcelObject = objExcel.Workbooks.Open (excelPath)
  116. End Function

  117. Function GetCellValue(objExcel, sheetName, row, col)
  118.         objExcel.Worksheets(sheetName).Activate
  119.         GetCellValue = objExcel.Worksheets(sheetName).Cells(row, col)
  120. End Function
复制代码

作者: lsekfe    时间: 2012-7-30 09:31
感谢下LZ!
作者: AntonioNikki    时间: 2012-7-30 09:47
感谢分享
作者: 黑羽祭    时间: 2012-7-30 13:22
感谢分享
作者: bslkzhw    时间: 2012-8-2 16:29
非常感谢楼主的简单结构,最近一直苦于怎么实施QTP,怎样把它导入进现在的测试,你的部署让我茅塞顿开。非常感谢
作者: monica.lijing    时间: 2012-8-28 10:26
过来看看,呵呵
作者: louqqson008    时间: 2012-8-28 15:09
看看,学习下




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2