也拿计算器Calc做个例子,和大家分享一下我的代码
看到大家都在拿微软的计算器做例子,我也分享一下我的例子,恰巧也是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
Ifres = "" 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 编辑 ] 少了个函数
'随机函数生成
'输入值:生成值范围 i~j
'返回值:随机数
Public Function Get_RandNum(fromNum,toNum)
If (fromNum<0) Or (toNum<0) Then
MsgBox "只接受大于零的输入"
ElseIf fromNum>toNum then
MsgBox "起始值必须小于结束值"
Else
Dim RunTime
Randomize
RunTime = Int((10 * Rnd) + 1)
Dim MyValue,i
For i = 1 To RunTime
Randomize
MyValue = Int(((toNum - fromNum + 1) * Rnd) + (fromNum))
Next
Get_randNum=MyValue
End If
End Function 好主意,我自己也做了一个相同的例子,只是LZ的简化版,
Dim OperandA, OperandB, Operation,Result
sub Standard_Calculation(OperandA, Operation, OperandB)
'First Operand
OperandA = cint(OperandA)
Select Case OperandA
case 0
Window("Calculator").WinButton("Button").Click
case 1
Window("Calculator").WinButton("Button_2").Click
case 2
Window("Calculator").WinButton("Button_3").Click
case 3
Window("Calculator").WinButton("Button_4").Click
case 4
Window("Calculator").WinButton("Button_12").Click
case 5
Window("Calculator").WinButton("Button_13").Click
case 6
Window("Calculator").WinButton("Button_14").Click
case 7
Window("Calculator").WinButton("Button_15").Click
case 8
Window("Calculator").WinButton("Button_16").Click
case 9
Window("Calculator").WinButton("Button_17").Click
End Select
'Operation
Select Case Operation
case "+"
Window("Calculator").WinButton("Button_18").Click
case "-"
Window("Calculator").WinButton("Button_19").Click
case "*"
Window("Calculator").WinButton("Button_20").Click
case "/"
Window("Calculator").WinButton("Button_21").Click
End Select
'Second Operand
OperandB = cint(OperandB)
Select Case OperandB
case 0
Window("Calculator").WinButton("Button").Click
case 1
Window("Calculator").WinButton("Button_2").Click
case 2
Window("Calculator").WinButton("Button_3").Click
case 3
Window("Calculator").WinButton("Button_4").Click
case 4
Window("Calculator").WinButton("Button_12").Click
case 5
Window("Calculator").WinButton("Button_13").Click
case 6
Window("Calculator").WinButton("Button_14").Click
case 7
Window("Calculator").WinButton("Button_15").Click
case 8
Window("Calculator").WinButton("Button_16").Click
case 9
Window("Calculator").WinButton("Button_17").Click
End Select
'Press "= " button
Window("Calculator").WinButton("Button_22").Click
'Get the result from text field
Result =Window("Calculator").Static("15.").GetROProperty("text")
Select Case Result
Case "7. "
msgbox("The answer is correct.")
Case "12. "
msgbox("The answer is correct.")
Case "1. "
msgbox("The answer is correct.")
Case "2. "
msgbox("The answer is correct.")
Case Else
msgbox("The answer is wrong!")
End Select
End sub
Sub Normal_Calculation
Standard_Calculation 3,"+",4
Standard_Calculation 3,"*",4
Standard_Calculation 4, "-",3
Standard_Calculation 4,"/",2
End Sub
Call Normal_Calculation 写的太好了!真羡慕!
我们得向你学习! 写的确实不错, 多谢多谢,拿的它给公司作了个简单的培训 不错的例子,值得参考 现在还了个新工作环境,可能我很长一段时间不会上QTP板块了
希望这段小代码能给初学者带来帮助 不错,学习中,谢谢! 顶起 楼主真是辛苦了啊! 像楼主学习,我也可以拿来练习一下了,谢谢!方法不错:victory: 咋才能学好qtp啊。楼主们
页:
[1]