|
看到大家都在拿微软的计算器做例子,我也分享一下我的例子,恰巧也是Calc的例子,哈哈,只要把计算器添加进对象库就能运行了
'#####################################################################
'### 上海(保密一下)软件有限公司 开发部 ########################
'### 被测对象:Microsoft calc.exe (计算器) ###################
'### 功能:1.随机生成测试操作位数、操作数、操作运算符 ######
'### 2.生成日志文件 ################################
'### 作者:俞戴龙 ########################################
'### 完成日期:2007.8.14 ##################################
'###################################################################
Option Explicit '强制声明变量
Environment.Value("RunTimes") = 20 ' 执行测试次数
Dim fileSpec '测试报告存放位置
fileSpec ="C:\" &"测试报告QTP_"& Date & ".txt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'主函数''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'让QTP运行时保持最小化
Call qtp_small
'获取当前时间
Dim currentTime,currentDate
currentDate = Date
currentTime = Time
'写入测试报告
call QTP_WriteFile(fileSpec,"##############################################")
call QTP_WriteFile(fileSpec,"###### 被测对象:计算器 ###########")
call QTP_WriteFile(fileSpec,"###### 测试者:俞戴龙 ########")
call QTP_WriteFile(fileSpec,"##### 测试日期: " + cstr(currentDate) + cstr(currentTime) + " ####")
call QTP_WriteFile(fileSpec,"##############################################")
call QTP_WriteFile(fileSpec,"")
'执行测试
Dim circle
For circle =1 to Environment.Value("RunTimes")
SystemUtil.Run "C:\WINDOWS\system32\calc.exe","","C:\WINDOWS\system32","open"
Dim len1,len2 'len1第一操作位数,len2第二操作位数
Dim op1,op2 'op1第一操作数,op2第二操作数
Dim operator '运算符
Dim result '预期运算结果
Window("计算器").Activate
'生成第一操作位数
len1 = Get_RandNum(1,5)
Window("计算器").Activate
'生成第二操作位数
len2 = Get_RandNum(1,5)
Window("计算器").Activate
'生成第一操作数
op1 = GetExec(len1)
Window("计算器").Activate
'生成操作运算符
operator = GetOperation()
Window("计算器").Activate
'生成第二操作数
op2 = GetExec(len2)
Window("计算器").Activate
'计算期望值
Select Case operator
Case 0 result = clng(op1) + clng(op2)
Case 1 result = clng(op1) - clng(op2)
Case 2 result = clng(op1) * clng(op2)
Case 3 result = clng(op1) / clng(op2)
End Select
'点击“等于”按键
Window("计算器").Activate
Window("计算器").WinButton("=").Click
'获取实际运算结果
Dim RealValue
RealValue = Window("计算器").WinEdit("Edit").GetROProperty("text")
'写测试日志
If (clng(result) - clng(RealValue) =0 ) Then
call QTP_WriteFile(fileSpec,"测试用例"+ cstr(circle) + ":" + " " + "测试通过")
call QTP_WriteFile(fileSpec,"")
else
call QTP_WriteFile(fileSpec,"测试用例"+ cstr(circle) + ":" + " " + "测试不通过")
call QTP_WriteFile(fileSpec,"当前时间:"&Time)
End If
Dim express '随机生成的计算表达式
Select Case operator
Case 0 express = cstr(op1) + "+" + cstr(op2) + "= ?"
Case 1 express = cstr(op1) + "-" + cstr(op2) + "= ?"
Case 2 express = cstr(op1) + "*" + cstr(op2) + "= ?"
Case 3 express = cstr(op1) + "/" + cstr(op2) + "= ?"
End Select
call QTP_WriteFile(fileSpec,"测试表达式:" + express)
call QTP_WriteFile(fileSpec,"期望值:" + trim(cstr(result)) + ".")
call QTP_WriteFile(fileSpec,"实际值:" + trim(cstr(RealValue)))
call QTP_WriteFile(fileSpec,"" )
'关闭计算器
Window("计算器").Close
Next
'恢复QTP窗口
Call qtp_big
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'主函数结束''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'子函数(子过程)开始'''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'随机生成按键
'输入值:操作位数
'输出值:生成值
Function GetExec( j )
Dim res,m,i
Window("计算器").Activate
For m =1 to j
i =Get_RandNum(0,9)
res = res + cstr(i)
Select Case i
Case 0 Window("计算器").WinButton("0").Click
Case 1 Window("计算器").WinButton("1").Click
Case 2 Window("计算器").WinButton("2").Click
Case 3 Window("计算器").WinButton("3").Click
Case 4 Window("计算器").WinButton("4").Click
Case 5 Window("计算器").WinButton("5").Click
Case 6 Window("计算器").WinButton("6").Click
Case 7 Window("计算器").WinButton("7").Click
Case 8 Window("计算器").WinButton("8").Click
Case 9 Window("计算器").WinButton("9").Click
Case Else
End Select
Next
If res = "" Then
res = 0
End If
GetExec = res
End Function
'随机生成操作符
'输出值:操作代码
Function GetOperation()
Dim oper
oper = Get_RandNum(0,3)
Window("计算器").Activate
Select Case oper
Case 0 Window("计算器").WinButton("+").Click
Case 1 Window("计算器").WinButton("-").Click
Case 2 Window("计算器").WinButton("*").Click
Case 3 Window("计算器").WinButton("/").Click
Case Else
End Select
GetOperation = oper
End Function
'写文件函数(追加)
'输入值:写入内容
Public Function QTP_WriteFile(pathway,words)
Dim fileSystemObj,fileSpec,logFile,way
Set fileSystemObj = CreateObject("Scripting.FileSystemObject")
fileSpec = pathway
Set logFile = fileSystemObj.OpenTextFile(fileSpec, 8, true)
logFile.WriteLine (CStr(words))
logFile.Close
Set logFile = Nothing
End Function
'恢复QTP窗口
Public Sub QTP_Big()
Dim objQTPWin
Set objQTPWin = GetObject("" , "QuickTest.Application")
objQTPWin.WindowState = "Restored"
Set objQTPWin = Nothing
End Sub
'让QTP运行时保持最小化
Public Sub QTP_Small()
Dim objQTPWin
Set objQTPWin = GetObject("" , "QuickTest.Application")
objQTPWin.WindowState = "Minimized"
Set objQTPWin = Nothing
End Su
[ 本帖最后由 yuandjing 于 2007-12-24 13:08 编辑 ] |
|