vbscript 脚本学习笔记不断更新
1.一个 1~4 随机返回值的例子
function rand(k,n)
Randomize
n=int((k-1)*rnd+1)
rand=n
end function
Dim n
n=rand(4,n)
msgbox n
-------------------------------------------------------------------
2. Reporter用法
Argument |
Type |
Description |
---|---|---|
EventStatus
|
Number or pre-defined constant
|
Status of the report step:
0 or
micPass: Causes the status of this step to be passed and sends the specified message to the report.
1 or
micFail: Causes the status of this step to be failed and sends the specified message to the report. When this step runs, the test fails.
2 or
micDone: Sends a message to the report without affecting the pass/fail status of the test.
3 or
micWarning: Sends a warning message to the report, but does not cause the test to stop running, and does not affect the pass/fail status of the test.
|
ReportStepName
|
String
|
Name of the intended step in the report (object name).
|
Details
|
String
|
Description of the report event. The string will be displayed in the step details frame in the report.
|
Reporter
|
N/A
|
Not in use.
|
Reporter.ReportEvent 1, "Custom Step", "The user-defined step failed."
or
Reporter.ReportEvent micFail, "Custom Step", "The user-defined step failed."
---------------------------------------------------------------------------------
3. 导入excel表,取1,2列的值并打印出来。
Datatable.ImportSheet "D:\test\USER_INFO.xls",1,"Action1"
Dim i,RowCount,a,b '定义两个变量
i=0
RowCount=datatable.GetSheet("Action1").GetRowCount '设置RowCount等于51sheet中的行数
Do while i i=i+1
Datatable.getsheet("Action1")
Datatable.setcurrentrow(i)
Datatable.GetCurrentRow
a=DataTable.GetSheet("Action1").GetParameter(1).value
b=DataTable.GetSheet("Action1").GetParameter(2).value
msgbox a & b
loop
4. 用户登录验证的测试用例设计脚本
Datatable.ImportSheet "D:\test\USER_INFO.xls",1,"Action1"
Dim i,RowCount,a,b '定义两个变量
i=0
RowCount=datatable.GetSheet("Action1").GetRowCount '设置RowCount等于51sheet中的行数
Do while i<rowcount
i=i+1
Datatable.getsheet("Action1")
Datatable.setcurrentrow(i)
Datatable.GetCurrentRow
a=DataTable.GetSheet("Action1").GetParameter(1).value
b=DataTable.GetSheet("Action1").GetParameter(2).value
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set a
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").Set b
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click
If Browser("Welcome: Mercury Tours").Page("Sign-on: Mercury Tours").WebEdit("userName").Exist Then
Reporter.ReportEvent micPass, "登录验证"+CStr(i), "帐号 "&a&" 密码 "+b+"登录失败"
else
Reporter.ReportEvent micFaill, "登录验证"+CStr(i),"帐号 "+a+" 密码 "+b+"登录成功"
End If
Browser("Welcome: Mercury Tours").Page("Sign-on: Mercury Tours").Link("Home").Click
loop
5.获取磁盘大可用可用空间和剩余空间的vbscript脚本
Dim fso,drv,s
Set fso=createobject("scripting.filesystemobject") '创建filesystemobject 对象
Set drv=fso.getdrive(fso.getdrivename("c:\")) '用get方法获取磁盘对象
s=s&drv.volumename '取磁盘卷标名
s=s&"total space:" & formatnumber(drv.totalsize / 1024,0)&"kb" '获取磁盘总空间大小
s=s& "free space:"&formatnumber(drv.freespace / 1024,0) &"kb" '获取磁盘剩余空间大小
msgbox s
6.一个iputbox的应用
Dim Input
Input = InputBox(
"Enter your name")
MsgBox ("You entered: " & Input)
7.一个iputbox、if条件语句、数据类型转化的综合应用
Dim i
i=inputbox("输入数字")
if(cint(i)=0) then
msgbox "0"
elseif (cint(i)=1) then
msgbox "1"
elseif (cint(i)=2) then
msgbox "2"
else
msgbox "数据在不可接受的范围内"
end if
8. select case 条件语句的应用实例
Dim a
a=inputbox("输入数值:")
Select Case a
Case "0"
msgbox "0"
Case "1"
msgbox "1"
Case "2"
msgbox "2"
Case else
msgbox "输入数据不在可以接受的范围内"
End Select
9.do loop 循环语句
Dim counter,num
counter=0
num=10
Do until num=0
num=num-1
counter=counter+1
If num<8 Then
Exit do
End If
Loop
msgbox "循环了"&counter&"次"
9.1 for循环
连续10次messagebox输出i的值
Dim i
for i=1 to 10
msgbox i
next
10.Vbscript中的Function与Sub
Sub和Function都是过程,但是Sub不能返回值,而Function可以返回值。
'定义addmethod
Function addmethod(num)
addmethod=num+100
End Function
'调用addmethod函数并获取返回值
msgbox addmethod(100)
------------------------------------
'定义addmethod 子过程
Sub addmethod(num)
tmp=num+100
msgbox tmp
End Sub
'调用名为addmethod的子过程
Call addmethod(100)
11.关于Vbscript 中数组的应用实例
一、简单实例
Dim a(10)
a(0)="nihao"
a(1)="henhao"
msgbox a(1)
二、不能确定数组时,redim语句动态创建数组
Dim a()
Dim num
num=inputbox("输入数组的大小:")
ReDim a(num)
a(0)="10"
a(1)="11"
msgbox ubound(a) '获取数组的范围 可用UBound函数来获得数组的大小
12.Environment对象的使用例子:
一、Built_In 内建环境变量
'获取操作系统的名称
OS=Environment.Value("OS")
'获取操作系统的版本
OSVersion=Environment.Value("OSVersion")
TestName=Environment.Value("TestName")
Reporter.ReportEvent micdone,"Environment","运行测试脚本" &TestName& "的操作系统为:" & OS & "版本为:"& OSVersion
二、User_define 类型环境变量
'设置环境变量的值myVariable的值为10
Environment.Value("myVariable")=10
myvalue=Environment.Value("myVariable")
msgbox myvalue