51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2202|回复: 6
打印 上一主题 下一主题

[原创] QTP针对WEB项目自动化开发的简单总结

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2012-7-29 18:05:37 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
本帖最后由 测试工坊 于 2012-7-29 18:06 编辑

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





下面是外部调用的示例,更多需求请参考帮助文件中的自动化对象模型部分。
  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
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

7#
发表于 2012-8-28 15:09:57 | 只看该作者
看看,学习下
回复 支持 反对

使用道具 举报

该用户从未签到

6#
发表于 2012-8-28 10:26:33 | 只看该作者
过来看看,呵呵
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2012-8-2 16:29:58 | 只看该作者
非常感谢楼主的简单结构,最近一直苦于怎么实施QTP,怎样把它导入进现在的测试,你的部署让我茅塞顿开。非常感谢
回复 支持 反对

使用道具 举报

  • TA的每日心情
    无聊
    2018-9-27 10:05
  • 签到天数: 36 天

    连续签到: 1 天

    [LV.5]测试团长

    4#
    发表于 2012-7-30 13:22:25 | 只看该作者
    感谢分享
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
    发表于 2012-7-30 09:47:29 | 只看该作者
    感谢分享
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    3 天前
  • 签到天数: 1025 天

    连续签到: 4 天

    [LV.10]测试总司令

    2#
    发表于 2012-7-30 09:31:57 | 只看该作者
    感谢下LZ!
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-29 07:29 , Processed in 0.101013 second(s), 31 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表