UFT demo(二)
利用自动化框架对demo进行修改,框架分为6个部分,分别为commonlib,testcase,testlog,testobject,testpic,testscript
commonlib存放自定义函数
testcase存放测试用例
testlog存在测试日志
testobject存放对象库
testpic存放错误截图
testscript存放测试脚本
目的在于把测试脚本分拆,用函数封装主要功能,框架总体积缩小,方便自动化测试,脚本如下:
外部运行脚本启动UFT,自动执行用例:
1 Dim qtapp
2 Set qtapp=CreateObject("quicktest.application")
3 qtapp.Launch
4 qtapp.Visible=True
5 qtapp.Open "C:\AutoTest\testscript\Flight-Demo"
6 qtapp.Test.Run
7 qtapp.Quit
8 Set qtapp = nothing
复制代码
控制脚本,脚本主体框架:
复制代码
1 Option explicit
2 '1、目录生成
3 Dim testpath,testpaths
4 testpath = Environment.Value("TestDir")
5 testpaths = split(testpath,"testscript")
6 ExecuteFile testpaths(0)&"commonlib\createfolder.vbs"
7 createfolder testpaths(0)&"testlog"
8 createfolder testpaths(0)&"testpic"
9
10 '2、测试用例加载
11 '判断测试用例是否存在
12 ExecuteFile testpaths(0)&"commonlib\loadcase.vbs"
13 loadcase testpaths(0)&"testcase\testCases.xls","Login"
14
15 '3、生成日志
16 ExecuteFile testpaths(0)&"commonlib\logPrint.vbs"
17 ExecuteFile testpaths(0)&"commonlib\ErrHandle.vbs"
18
19 '4、应用程序启动
20 logPrint("开始进入功能:flight登陆功能")
21 ExecuteFile testpaths(0)&"commonlib\OpenApp.vbs"
22 OpenApp "flight4b.exe","C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4b",""
23
24 '5、测试对象加载(如果有对象库,则通过loadtsr方法加载)
25 ExecuteFile testpaths(0)&"commonlib\loadtsr.vbs"
26 ExecuteFile testpaths(0)&"testscript\login.vbs"
复制代码
测试脚本主体:
复制代码
1 Dim WarnStaticLogin,WarnStaticPassword,CheckValue,ExpectValue,ActualValue
2 ExecuteFile testpaths(0)&"commonlib\object.vbs"
3 ExecuteFile testpaths(0)&"commonlib\function.vbs"
4
5
6 logPrint("执行登陆条件:错误的用户名,正确的密码")
7 With Dialog(MyDialog)
8 .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,5)
9 .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,6)
10 .WinButton(MyButton).Click
11 End With
12 WarnStaticLogin = Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).Static(WarnStatic_login).GetROProperty("regexpwndtitle")
13 If WarnStaticLogin = "Agent name must be at least 4 characters long." Then
14 CheckValue = "Agent name must be at least 4 characters long."
15 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,8)
16 ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,7)
17 ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",4,8)
18 If ExpectValue = ActualValue Then
19 CheckValue = "Pass"
20 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,9)
21 else
22 CheckValue = "Fail"
23 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",4,9)
24 End If
25 Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click
26
27 ExitTest
28 End If
29
30 logPrint("执行登陆条件:正确的用户名,错误的密码")
31 wait 5
32 With Dialog(MyDialog)
33 .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,5)
34 .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,6)
35 .WinButton(MyButton).Click
36 End With
37 WarnStaticPassword = Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).Static(WarnStatic_password).GetROProperty("regexpwndtitle")
38 If WarnStaticPassword = "Password must be at least 4 characters long" Then
39 CheckValue = "Password must be at least 4 characters long"
40 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,8)
41 ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,7)
42 ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",5,8)
43 If ExpectValue = ActualValue Then
44 CheckValue = "Pass"
45 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,9)
46 else
47 CheckValue = "Fail"
48 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",5,9)
49 End If
50 Dialog(MyDialog).Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click
51 else
52 ExitTest
53 End If
54
55
56 logPrint("执行登陆条件:正确的用户名、密码")
57 wait 5
58 With Dialog(MyDialog)
59 .WinEdit(MyName).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,5)
60 .WinEdit(MyPwd).Set ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,6)
61 .WinButton(HelpButton).Click
62 .Dialog(Flight_Reservations_Dialog).WinButton(ConfirmButton).Click
63 .WinButton(MyButton).Click
64 End With
65 IfWindow(MyWindow).Exist(5) Then
66 CheckValue = "成功登录"
67 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,8)
68 ExpectValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,7)
69 ActualValue = ReadExcel(testpaths(0)&"testcase\testCases.xls","Login",3,8)
70 If ExpectValue = ActualValue Then
71 CheckValue = "Pass"
72 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,9)
73 else
74 CheckValue = "Fail"
75 Call DoExcel (testpaths(0)&"testcase\testCases.xls","Login",3,9)
76 End if
77 Window(MyWindow).WinMenu(MyMenu).Select "File;Exit"
78 else
79 ExitTest
80 End If
81 logPrint("登陆用例执行完毕")
复制代码
createfolder.vbs创建文件夹:
复制代码
1 Option Explicit
2 Sub createfolder(folderpath)
3 Dim fso,f
4 Dim blfolderexist
5 Set fso = CreateObject("scripting.filesystemobject")
6 blfolderexist =fso.FolderExists(folderpath)
7 If blfolderexist Then
8 MsgBox folderpath&"已存在"
9 fso.DeleteFolder(folderpath)
10 End If
11 Set f = fso.CreateFolder(folderpath)
12 Set fso = Nothing
13 End Sub
复制代码
ErrHandle.vbs日志:
复制代码
1 Public Function ErrHandle()
2 If Err.Number <> 0 Then
3 logPrint "Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description
4 Err.Clear
5 End If
6 End Function
复制代码
loadcase.vbs确定测试用例的存在:
复制代码
1 Sub loadcase(casepath,casename)
2 Dim fso,blcaseexist
3 Set fso = CreateObject("scripting.filesystemobject")
4 blcaseexist = fso.FileExists(testpaths(0)&"testcase\testCases.xls")
5 If blcaseexist Then
6 DataTable.ImportSheet casepath,"Login","login"
7 Else
8 Reporter.ReportEvent micFail,"Login Test","测试用例不存在"
9 ExitTest
10 End If
11 Set fso = nothing
12 End Sub
复制代码
loadtsr.vbs调用对象库:
1 Sub loadtsr(tsrname)
2 RepositoriesCollection.RemoveAll
3 RepositoriesCollection.Add tsrname
4 End Sub
logPrint.vbs日志:
复制代码
1 Public Function logPrint(ByVal logMessage)
2 Dim fso,logFile
3 Set fso=CreateObject("scripting.filesystemobject")
4 Set logFile = fso.OpenTextFile(testpaths(0)&"testlog\runtime.log",8,true)
5 logFile.WriteLine(date() & " " & hour(now) & ":" & minute(now) & ":" & second(now) & ": " & logMessage)
6 logFile.Close
7 End Function
复制代码
OpenApp.vbs打开应用程序,之所以不用systemutil.Run,是为了方便把已打开的程序关闭:
复制代码
1 Sub OpenApp(AppName,AppPath,AppParam)
2 Dim gobj,wsh,proc
3 Dim i
4 On error resume next
5 Set gobj=GetObject("winmgmts:\\.\root\cimv2")
6 Set wsh=CreateObject("wscript.shell")
7 Set proc=gobj.execquery("select * from win32_process where name="&"'"&AppName&"'")
8 For each i in proc
9 systemutil.CloseProcessByName(AppName)
10 wscript.quit
11 Next
12 systemutil.Run AppPath,AppParam
13 End Sub
复制代码
function.vbs对excel操作:
复制代码
1 Function ReadExcel(filepath,sheetname,x,y)
2 Dim ObjExcel,SrcExcel,ExcValue
3 Set ObjExcel = CreateObject("Excel.Application")
4 ObjExcel.Visible = false
5 Set SrcExcel = ObjExcel.Workbooks.Open(filepath)
6 SrcExcel.Worksheets(sheetname).Activate
7 ExcValue = SrcExcel.Worksheets(sheetname).Cells(x,y).Value
8 ReadExcel = ExcValue
9 ObjExcel.DisplayAlerts = false
10 ObjExcel.Save
11 ObjExcel.Quit
12 Set ObjExcel = nothing
13 Set SrcExcel = nothing
14 End Function
15
16 Function DoExcel(filepath,sheetname,x,y)
17 Dim ObjExcel,SrcExcel
18 Set ObjExcel = CreateObject("Excel.Application")
19 ObjExcel.Visible = false
20 Set SrcExcel = ObjExcel.Workbooks.Open(filepath)
21 SrcExcel.Worksheets(sheetname).Activate
22 SrcExcel.Worksheets(sheetname).Cells(x,y).Value = CheckValue
23 ObjExcel.DisplayAlerts = false
24 ObjExcel.Save
25 ObjExcel.Quit
26 Set ObjExcel = nothing
27 Set SrcExcel = nothing
28 End Function
复制代码
object.vbs DP编程:
复制代码
1 Option Explicit
2 Dim MyDialog,MyName,MyPwd,MyButton,MyWindow,MyMenu,Flight_Reservations_Dialog,WarnStatic_login,WarnStatic_password,ConfirmButton,CancelButton,HelpButton
3 Set MyDialog = Description.Create()
4 MyDialog("text").value="Login"
5 Set MyName = Description.Create()
6 MyName("nativeclass").value = "Edit"
7 MyName("attached text").value = "Agent Name:"
8 Set MyPwd = Description.Create()
9 MyPwd("nativeclass").value = "Edit"
10 MyPwd("attached text").value= "Password:"
11 Set MyButton = Description.Create()
12 MyButton("text").value = "OK"
13 MyButton("nativeclass").value="Button"
14 Set MyWindow = Description.Create()
15 MyWindow("regexpwndtitle").value = "Flight Reservation"
16 Set MyMenu = Description.Create()
17 MyMenu("menuobjtype").value = "2"
18 Set Flight_Reservations_Dialog = Description.Create()
19 Flight_Reservations_Dialog("text").value = "Flight Reservations"
20 Flight_Reservations_Dialog("regexpwndtitle").value = "Flight Reservations"
21 Set WarnStatic_login = Description.Create()
22 WarnStatic_login("nativeclass").value = "Static"
23 WarnStatic_login("text").value = "Agent name must be at least 4 characters long."
24 Set WarnStatic_password = Description.Create()
25 WarnStatic_password("nativeclass").value = "Static"
26 WarnStatic_password("text").value = "Password must be at least 4 characters long"
27 Set CancelButton = Description.Create()
28 CancelButton("regexpwndtitle").value = "Cancel"
29 Set ConfirmButton = Description.Create()
30 ConfirmButton("regexpwndtitle").value = "确定"
31 Set HelpButton = Description.Create()
32 HelpButton("regexpwndtitle").value = "Help"
学习
页:
[1]