|
看了精华区的计算器例子后,自己也写了一个类似的例子,有不足的地方请大虾指教.
'$include "SQAUTIL.SBH"
Declare function Randomlong(min as long,max as long) as long
Function Randomlong(min as long,max as long) as long
dim i,j,k as integer
dim m,tmp as long
m=rnd*25551/139
j=fix(log(min))+1
k=fix(log(max))+1
i=fix((j+k)/5)+3
do while(tmp<min or tmp>max)
randomize timer()+m
tmp=clng(rnd*(10^i))
loop
randomlong=tmp
end function
Sub Main
Dim Result As variant
dim Push_Num(1 to 9) as integer
dim oper (1 to 4) as integer
dim Push_object as string
dim i,j,k as long
dim cal_rec as variant
dim m as integer
'Initially Recorded: 2005-1-5 9:45:35
'Script Name: calu
'描述: 随机的进行运算,然后将结果比较后,在Console显示出来
StartApplication "C:\WINNT\system32\calc.exe"
'将数字PushButton 转换成 ObjectIndex 识别
Push_Num(1)= 7
Push_Num(2)= 11
Push_Num(3)= 15
Push_Num(4)= 6
Push_Num(5)= 10
Push_Num(6)= 14
Push_Num(7)= 5
Push_Num(8)= 9
Push_Num(9)= 13
'将操作符号 转换成 ObjectIndex 识别
oper(1) = 20 '+
oper(2) = 19 '-
oper(3) = 18 '*
oper(4) = 17 '/
for m = 1 to 6 ‘自己决定进行多少次运算
Window SetContext, "Caption=计算器", ""
'随机产生第一个PushBotton加数(或者其他)
i=Randomlong(1,9)
Push_object = "ObjectIndex=" & cstr(Push_Num(i))
PushButton Click, Push_object
'随机产生一个操作符(+ - * /)
j=Randomlong(1,4)
Push_object = "ObjectIndex=" & cstr(oper(j))
PushButton Click, Push_object
'随机产生第二个PushBotton加数(或者其他)
k=Randomlong(1,9)
Push_object = "ObjectIndex=" & cstr(Push_Num(k))
PushButton Click, Push_object
'按=号
PushButton Click, "ObjectIndex=21"
'验证结果
sqaconsolewrite "计算器执行的操作是:" & cstr(i)
select case j
case 1
sqaconsolewrite "+"
result= i + k
case 2
sqaconsolewrite "-"
result = i - k
case 3
sqaconsolewrite "*"
result = i * k
case 4
sqaconsolewrite "/"
result = i / k
end select
sqaconsolewrite cstr(k)
sqaconsolewrite "正确结果应该是:" & cstr(result)
Window SetContext, "Caption=计算器", ""
Label Click, "objectindex=1"
result =sqagetproperty("Type=Label;ObjectIndex=1","Text",cal_rec)
if result = sqaSuccess then
sqaconsolewrite "计算器的结果是" & cal_rec
else
sqaconsolewrite "Failed !! "
end if
next m
End Sub
这个例子比原来帖子上的更加深了一些,在Win2000上调试通过。
如果在XP上可能通不过,将Lable的地方改成EditBox就行了,路径也改一下,改成自己存放calc.exe的地方。
下面是运行结果:
计算器执行的操作是:9
-
9
正确结果应该是:0
计算器的结果是0.
计算器执行的操作是:7
-
8
正确结果应该是:-1
计算器的结果是-1.
计算器执行的操作是:7
/
9
正确结果应该是:.777777777777778
计算器的结果是0.77777777777777777777777777777778
有什么不足之处,希望大虾不吝赐教,大家彼此学习进步,谢谢。 |
|