51Testing软件测试论坛

标题: UFT demo(二) [打印本页]

作者: Mario洁    时间: 2018-3-12 16:12
标题: UFT demo(二)
利用自动化框架对demo进行修改,框架分为6个部分,分别为commonlib,testcase,testlog,testo
bject,testpic,testscript

commonlib存放自定义函数

testcase存放测试用例

testlog存在测试日志

testobject存放对象库

testpic存放错误截图

testscript存放测试脚本

目的在于把测试脚本分拆,用函数封装主要功能,框架总体积缩小,方便自动化测试,脚本如下:

外部运行脚本启动UFT,自动执行用例:
  1. 1 Dim qtapp
  2. 2 Set qtapp=CreateObject("quicktest.application")
  3. 3 qtapp.Launch
  4. 4 qtapp.Visible=True
  5. 5 qtapp.Open "C:\AutoTest\testscript\Flight-Demo"
  6. 6 qtapp.Test.Run
  7. 7 qtapp.Quit
  8. 8 Set qtapp = nothing
  9. 复制代码
  10. 控制脚本,脚本主体框架:

  11. 复制代码
  12. 1 Option explicit
  13. 2 '1、目录生成
  14. 3 Dim testpath,testpaths
  15. 4 testpath = Environment.Value("TestDir")
  16. 5 testpaths = split(testpath,"testscript")
  17. 6 ExecuteFile testpaths(0)&"commonlib\createfolder.vbs"
  18. 7 createfolder testpaths(0)&"testlog"
  19. 8 createfolder testpaths(0)&"testpic"
  20. 9
  21. 10 '2、测试用例加载
  22. 11 '判断测试用例是否存在
  23. 12 ExecuteFile testpaths(0)&"commonlib\loadcase.vbs"
  24. 13 loadcase testpaths(0)&"testcase\testCases.xls","Login"
  25. 14
  26. 15 '3、生成日志
  27. 16 ExecuteFile testpaths(0)&"commonlib\logPrint.vbs"
  28. 17 ExecuteFile testpaths(0)&"commonlib\ErrHandle.vbs"
  29. 18
  30. 19 '4、应用程序启动
  31. 20 logPrint("开始进入功能:flight登陆功能")
  32. 21 ExecuteFile testpaths(0)&"commonlib\OpenApp.vbs"
  33. 22 OpenApp "flight4b.exe","C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4b",""
  34. 23
  35. 24 '5、测试对象加载(如果有对象库,则通过loadtsr方法加载)
  36. 25 ExecuteFile testpaths(0)&"commonlib\loadtsr.vbs"
  37. 26 ExecuteFile testpaths(0)&"testscript\login.vbs"
  38. 复制代码
  39. 测试脚本主体:

  40. 复制代码
  41. 1 Dim WarnStaticLogin,WarnStaticPassword,CheckValue,ExpectValue,ActualValue
  42. 2 ExecuteFile testpaths(0)&"commonlib\object.vbs"
  43. 3 ExecuteFile testpaths(0)&"commonlib\function.vbs"
  44. 4
  45. 5
  46. 6 logPrint("执行登陆条件:错误的用户名,正确的密码")
  47. 7 With Dialog(MyDialog)
  48. 8     .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,5)
  49. 9     .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,6)
  50. 10     .WinButton(MyButton).Click
  51. 11 End With
  52. 12 WarnStaticLogin = Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).Static(WarnStatic_login).GetROProperty("regexpwndtitle")
  53. 13 If WarnStaticLogin = "Agent name must be at least 4 characters long." Then
  54. 14     CheckValue = "Agent name must be at least 4 characters long."
  55. 15     Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,8)
  56. 16     ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,7)
  57. 17     ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,8)
  58. 18     If ExpectValue = ActualValue Then
  59. 19         CheckValue = "Pass"
  60. 20             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,9)
  61. 21         else
  62. 22             CheckValue = "Fail"
  63. 23             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,9)
  64. 24     End If
  65. 25     Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click
  66. 26     
  67. 27     ExitTest
  68. 28 End If
  69. 29
  70. 30 logPrint("执行登陆条件:正确的用户名,错误的密码")
  71. 31 wait 5
  72. 32 With Dialog(MyDialog)
  73. 33     .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,5)
  74. 34     .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,6)
  75. 35     .WinButton(MyButton).Click
  76. 36 End With
  77. 37 WarnStaticPassword = Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).Static(WarnStatic_password).GetROProperty("regexpwndtitle")
  78. 38 If WarnStaticPassword = "Password must be at least 4 characters long" Then
  79. 39     CheckValue = "Password must be at least 4 characters long"
  80. 40     Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,8)
  81. 41     ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,7)
  82. 42     ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,8)
  83. 43     If ExpectValue = ActualValue Then
  84. 44         CheckValue = "Pass"
  85. 45             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,9)
  86. 46         else
  87. 47             CheckValue = "Fail"
  88. 48             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,9)
  89. 49         End If
  90. 50     Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click   
  91. 51 else
  92. 52     ExitTest
  93. 53 End If
  94. 54
  95. 55
  96. 56 logPrint("执行登陆条件:正确的用户名、密码")
  97. 57 wait 5
  98. 58 With Dialog(MyDialog)
  99. 59     .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,5)
  100. 60     .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,6)
  101. 61     .WinButton(HelpButton).Click
  102. 62     .Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click
  103. 63     .WinButton(MyButton).Click
  104. 64 End With
  105. 65 If  Window(MyWindow).Exist(5) Then
  106. 66     CheckValue = "成功登录"
  107. 67     Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,8)
  108. 68     ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,7)
  109. 69     ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,8)
  110. 70     If ExpectValue = ActualValue Then
  111. 71         CheckValue = "Pass"
  112. 72             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,9)
  113. 73         else
  114. 74             CheckValue = "Fail"
  115. 75             Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,9)
  116. 76         End if
  117. 77     Window(MyWindow).WinMenu(MyMenu).Select "File;Exit"
  118. 78 else
  119. 79     ExitTest
  120. 80 End If
  121. 81 logPrint("登陆用例执行完毕")
  122. 复制代码
  123. createfolder.vbs创建文件夹:

  124. 复制代码
  125. 1 Option Explicit
  126. 2 Sub createfolder(folderpath)
  127. 3 Dim fso,f
  128. 4 Dim blfolderexist
  129. 5 Set fso = CreateObject("scripting.filesystemobject")
  130. 6 blfolderexist =  fso.FolderExists(folderpath)
  131. 7 If blfolderexist Then
  132. 8     MsgBox folderpath&"已存在"
  133. 9     fso.DeleteFolder(folderpath)
  134. 10 End If
  135. 11 Set f = fso.CreateFolder(folderpath)
  136. 12 Set fso = Nothing
  137. 13 End Sub
  138. 复制代码
  139. ErrHandle.vbs日志:

  140. 复制代码
  141. 1 Public Function ErrHandle()
  142. 2     If Err.Number <> 0 Then
  143. 3             logPrint "Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description
  144. 4             Err.Clear
  145. 5     End If
  146. 6 End Function
  147. 复制代码
  148. loadcase.vbs确定测试用例的存在:

  149. 复制代码
  150. 1 Sub loadcase(casepath,casename)
  151. 2     Dim fso,blcaseexist
  152. 3     Set fso = CreateObject("scripting.filesystemobject")
  153. 4     blcaseexist = fso.FileExists(testpaths(0)&"testcase\testCases.xls")
  154. 5     If blcaseexist Then
  155. 6        DataTable.ImportSheet casepath,"Login","login"
  156. 7     Else
  157. 8        Reporter.ReportEvent micFail,"Login Test","测试用例不存在"
  158. 9        ExitTest
  159. 10     End If
  160. 11     Set fso = nothing
  161. 12 End Sub
  162. 复制代码
  163. loadtsr.vbs调用对象库:

  164. 1 Sub loadtsr(tsrname)
  165. 2     RepositoriesCollection.RemoveAll
  166. 3     RepositoriesCollection.Add tsrname
  167. 4 End Sub
  168. logPrint.vbs日志:

  169. 复制代码
  170. 1 Public Function logPrint(ByVal logMessage)
  171. 2     Dim fso,logFile
  172. 3     Set fso=CreateObject("scripting.filesystemobject")
  173. 4     Set logFile = fso.OpenTextFile(testpaths(0)&"testlog\runtime.log",8,true)
  174. 5     logFile.WriteLine(date() & " " & hour(now) & ":" & minute(now) & ":" & second(now) & ": " & logMessage)
  175. 6     logFile.Close
  176. 7 End Function
  177. 复制代码
  178. OpenApp.vbs打开应用程序,之所以不用systemutil.Run,是为了方便把已打开的程序关闭:

复制代码


作者: Mario洁    时间: 2018-3-12 16:12
  1. 复制代码
  2. 1 Sub OpenApp(AppName,AppPath,AppParam)
  3. 2 Dim gobj,wsh,proc
  4. 3 Dim i
  5. 4 On error resume next
  6. 5 Set gobj=GetObject("winmgmts:\\.\root\cimv2")
  7. 6 Set wsh=CreateObject("wscript.shell")
  8. 7 Set proc=gobj.execquery("select * from win32_process where name="&"'"&AppName&"'")
  9. 8 For each i in proc
  10. 9 systemutil.CloseProcessByName(AppName)
  11. 10 wscript.quit
  12. 11 Next
  13. 12 systemutil.Run AppPath,AppParam
  14. 13 End Sub
  15. 复制代码
  16. function.vbs对excel操作:

  17. 复制代码
  18. 1 Function ReadExcel(filepath,sheetname,x,y)
  19. 2 Dim ObjExcel,SrcExcel,ExcValue
  20. 3 Set ObjExcel = CreateObject("Excel.Application")
  21. 4 ObjExcel.Visible = false
  22. 5 Set SrcExcel = ObjExcel.Workbooks.Open(filepath)
  23. 6 SrcExcel.Worksheets(sheetname).Activate
  24. 7 ExcValue = SrcExcel.Worksheets(sheetname).Cells(x,y).Value
  25. 8 ReadExcel = ExcValue
  26. 9 ObjExcel.DisplayAlerts = false
  27. 10 ObjExcel.Save
  28. 11 ObjExcel.Quit
  29. 12 Set ObjExcel = nothing
  30. 13 Set SrcExcel = nothing
  31. 14 End Function
  32. 15
  33. 16 Function DoExcel(filepath,sheetname,x,y)
  34. 17 Dim ObjExcel,SrcExcel
  35. 18 Set ObjExcel = CreateObject("Excel.Application")
  36. 19 ObjExcel.Visible = false
  37. 20 Set SrcExcel = ObjExcel.Workbooks.Open(filepath)
  38. 21 SrcExcel.Worksheets(sheetname).Activate
  39. 22 SrcExcel.Worksheets(sheetname).Cells(x,y).Value = CheckValue
  40. 23 ObjExcel.DisplayAlerts = false
  41. 24 ObjExcel.Save
  42. 25 ObjExcel.Quit
  43. 26 Set ObjExcel = nothing
  44. 27 Set SrcExcel = nothing
  45. 28 End Function
  46. 复制代码
  47. object.vbs DP编程:

  48. 复制代码
  49. 1 Option Explicit
  50. 2 Dim MyDialog,MyName,MyPwd,MyButton,MyWindow,MyMenu,Flight_Reservations_Dialog,WarnStatic_login,WarnStatic_password,ConfirmButton,CancelButton,HelpButton
  51. 3 Set MyDialog = Description.Create()
  52. 4 MyDialog("text").value="Login"
  53. 5 Set MyName = Description.Create()
  54. 6 MyName("nativeclass").value = "Edit"
  55. 7 MyName("attached text").value = "Agent Name:"
  56. 8 Set MyPwd = Description.Create()
  57. 9 MyPwd("nativeclass").value = "Edit"
  58. 10 MyPwd("attached text").value= "Password:"
  59. 11 Set MyButton = Description.Create()
  60. 12 MyButton("text").value = "OK"
  61. 13 MyButton("nativeclass").value="Button"
  62. 14 Set MyWindow = Description.Create()
  63. 15 MyWindow("regexpwndtitle").value = "Flight Reservation"
  64. 16 Set MyMenu = Description.Create()
  65. 17 MyMenu("menuobjtype").value = "2"
  66. 18 Set Flight_Reservations_Dialog = Description.Create()
  67. 19 Flight_Reservations_Dialog("text").value = "Flight Reservations"
  68. 20 Flight_Reservations_Dialog("regexpwndtitle").value = "Flight Reservations"
  69. 21 Set WarnStatic_login = Description.Create()
  70. 22 WarnStatic_login("nativeclass").value = "Static"
  71. 23 WarnStatic_login("text").value = "Agent name must be at least 4 characters long."
  72. 24 Set WarnStatic_password = Description.Create()
  73. 25 WarnStatic_password("nativeclass").value = "Static"
  74. 26 WarnStatic_password("text").value = "Password must be at least 4 characters long"
  75. 27 Set CancelButton = Description.Create()
  76. 28 CancelButton("regexpwndtitle").value = "Cancel"
  77. 29 Set ConfirmButton = Description.Create()
  78. 30 ConfirmButton("regexpwndtitle").value = "确定"
  79. 31 Set HelpButton = Description.Create()
  80. 32 HelpButton("regexpwndtitle").value = "Help"
复制代码


作者: 梦想家    时间: 2018-5-14 08:48
学习




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