vbs基本编程

这篇博客详细介绍了VBS(Visual Basic Scripting Edition)的基本编程概念,包括常量和变量的定义、字符串操作、转换函数、时间日期函数的使用,以及循环和选择分支语句的语法。此外,还探讨了数组的运用、函数封装技巧,并讲解了如何利用File System Object(FSO)进行文件操作,创建Excel文件以及增强的鲁棒性处理。最后,讨论了VBS如何控制QuickTest Professional(QTP)进行对象导入导出和运行自动化测试脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【常量变量的定义】

'常量变量的定义
Option Explicit  '强制变量声明,有了此语句必须申明变量再使用
Dim opera,operb,result '一般,也可以不声明直接使用变量
Const vbPI=3.14 '常量 前面加vb或者con一般表示是常数
opera=InputBox("please input a's value")
operb=InputBox("please input b's value")
result=opera+operb
MsgBox opera&"+"&operb&"="&result
MsgBox vbPI

【字符串】

'大小写转换
Dim x
x=LCase("aBc")'abc ,转小写
MsgBox x
x=UCase("aBc")'ABC ,转大写
MsgBox x

'比较字符串StrComp
Dim name,user
name="lisi"
user=InputBox("please input your name")
If StrComp(name,user) Then 
    MsgBox "输入的名字不同"
Else  '两个字符串相同,StrComp返回0,否则返回非0
    MsgBox "名字相同"
End If 

'字符串长度
Dim a
a="abcdef" '6
MsgBox Len(a)

'替换字符串
Dim astr
astr=Replace("hello,lisi","lisi","zhang") 
MsgBox astr 'hello,zhang

'切割
Dim teststr
teststr="2.1.3"
teststrs=Split(teststr,".")
MsgBox teststrs(0) '2
MsgBox teststrs(1) '1
MsgBox teststrs(2) '3

【转换函数】

'转换函数
MsgBox CBool(0) '0转成false,非0转成true
MsgBox CBool(34) 'true
MsgBox CInt(4.5) '转成整数,4.5是4,4.6是5
MsgBox CDate("12/12/2012") '2012-12-12
MsgBox Asc("a") '97
MsgBox Chr(65) 'A

【时间日期 函数】

'日期时间函数
MsgBox Date
MsgBox Time 
MsgBox Now 
MsgBox Day(Date)
MsgBox Month(Date)
MsgBox Year(Date)
MsgBox Hour(Time)
MsgBox Minute(Time)
MsgBox Second(Time)
MsgBox Weekday(Date)

【循环语句】

'从1加到100,结果为5050
Option Explicit 
Dim i,sum
i=0
sum=0
Do While i<101
    sum=sum+i
    i=i+1
    If i>10 Then
        Exit Do 
    End If 
Loop 
MsgBox sum '5050 55
sum=0
i=0
For i=1 To 100 Step 2
    sum=sum+i '1+3+5+9+...
Next 
MsgBox sum '2500

【选择分支语句】

'选择分支语句
'根据学生成绩判断所属等级,90以上优秀,60-89合格,60以下不合格
Dim score
score=InputBox("please input your grade")
If CInt(score)>=90 Then
    MsgBox "优秀"
ElseIf CInt(score)>=60 And CInt(score)<90 Then
    MsgBox "合格"
Else 
    MsgBox "不合格"
End If 

'饮料自动售货机,根据输入的饮料编号,显示对应的饮料名称
Dim drinkid
MsgBox "1.雪碧"&vbCrLf&"2.可乐"&vbCrLf&"3.脉动"&vbCrLf&"请输入饮料编号"
drinkid=InputBox("输入饮料编号")
Select Case drinkid
    Case 1 MsgBox "雪碧"
    Case 2 MsgBox "可乐"
    Case 3 MsgBox "脉动"
    Case Else MsgBox "输入错误"
End Select

【数组】

Dim username(2)
Dim arraybound,arraycount
username(0)="zhangsan"
username(1)="lisi"
username(2)="wangwu"
arraybound=UBound(username)'下标2
arraycount=arraybound+1 '长度3
For i=0 To arraybound
    MsgBox username(i)
Next

【函数封装】

'函数封装,带返回function,不带返回sub
'根据输入的两个数据,选择对应的四则运算
'1 输入+,则进行加法运算
'2 输入-,则进行减法运算
Dim opera,operb,result
MsgBox "+.加法功能"&vbCrLf&"-.减法功能"
oper=InputBox("请选择功能")
opera=InputBox("a")
operb=InputBox("b")
Select Case oper 
    case "+" MsgBox addtest(opera,operb) ''注意:Function调用时带括号
    case "-" jianfa opera,operb '注意:Sub调用时不能使用括号
    case else 
             MsgBox "wrong input"
End Select 

'带返回的加法运算
Function addtest(opera,operb)
    addtest=CInt(opera)+CInt(operb)
End Function 
'不带返回的减法运算
Sub jianfa(opera,operb)
    result=CInt(opera)-CInt(operb)
    MsgBox result 
End Sub 

【fso】

'用fso创建文件夹
Sub createfolder(folderpath,foldername)
    Dim fso,f
    Set fso=CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(folderpath&foldername) Then
        MsgBox "目录已存在"
    Else 
        MsgBox "目录不存在,现在开始创建..."
        Set f=fso.CreateFolder(folderpath&foldername) '"e:\test"
    End If
    Set fso=Nothing 
End Sub 
'测试创建文件夹
createfolder "e:\","test"

'fso创建文件,写文件
Sub writelog(filepath,logstr)
    Const ForReading=1,ForWriting=2,ForAppdening=8
    Dim fso,f
    Dim teststr
    Set fso=CreateObject("scripting.filesystemobject")
    If fso.FileExists(filepath) Then '"e:\test\test.txt"
        MsgBox "Exist"
    Else 
        MsgBox "Not Exist"
        Set f=fso.CreateTextFile(filepath)
    End If
    '追加模式,如果文件不存在就报错(true表示:不存在就创建文件)
    Set f=fso.OpenTextFile(filepath,ForAppdening,false)
    f.WriteLine logstr '"hello"
    f.Close
    '读记事本文件
    Set f=fso.OpenTextFile(filepath,ForReading,False)
    teststr=f.ReadLine 
    MsgBox teststr
    f.Close
    Set fso=Nothing
End Sub 
'调用函数测试
writelog "e:\test\test.txt",now()&"-----执行自动化测试"
'2018-01-21 14:21:03-----执行自动化测试     

【新建excel】

'创建exl
Sub genexcel(excelpath)
'定义excel对象
    Dim exlapp
    Dim exlworkbook 
    Dim exlworksheet
    Set exlapp=CreateObject("Excel.application")
    Set exlworkbook=exlapp.Workbooks.Add '工作簿
    Set exlworksheet=exlworkbook.Worksheets.Add '工作表
    'exlworksheet.cells(1,1)="Hello,test"
    '保存excel文件,如果原来存在,则使用save方法,如果是新建,用saveas
    exlworkbook.SaveAs excelpath'"e:\test\t.xls"
    exlworkbook.Close   '关闭工作簿对象
    exlapp.Quit '退出excel对象
    Set exlworksheet=Nothing '释放销毁相关资源
    Set exlworkbook=Nothing
    Set exlapp=Nothing 
End Sub 

'调用函数生成exl
'genexcel "e:\test\est.xls"

'uft中调用vbs文件
executefile "C:\Users\Administrator\Desktop\genexcel.vbs"
genexcel "C:\Users\Administrator\Desktop\t.xls"

【fso与excel综合(增强鲁棒性)】

'创建exl
Sub genexcel(excelpath)
'定义excel对象
    Dim exlapp
    Dim exlworkbook 
    Dim exlworksheet
    Dim fso
    Dim exlexist 'excel对象状态标志,看是否存在

    Set exlapp=CreateObject("Excel.application")
    Set fso=CreateObject("scripting.filesystemobject")
    exlexist=fso.FileExists("C:\Users\Administrator\Desktop\t.xls")
    If exlexist Then
        Set exlworkbook=exlapp.Workbooks.Open("C:\Users\Administrator\Desktop\t.xls")'存在就打开
    Else 
        Set exlworkbook=exlapp.Workbooks.Add '不存在就添加工作簿
    End If 

    Set exlworksheet=exlworkbook.Worksheets.Add '工作表
    exlworksheet.cells(1,1)="hello,test"

    '保存excel文件,如果原来存在,则使用save方法,如果是新建,用saveas
    If exlexist Then
        MsgBox "文件存在"
        exlworkbook.Save 
    Else 
        MsgBox "文件不存在"
        exlworkbook.SaveAs excelpath
    End If 

    exlworkbook.Close   '关闭工作簿对象
    exlapp.Quit '退出excel对象
    Set exlworksheet=Nothing '释放销毁相关资源
    Set exlworkbook=Nothing
    Set exlapp=Nothing 
End Sub 

'调用函数生成exl
genexcel "C:\Users\Administrator\Desktop\t.xls"

【qtp对象导入导出(初始化)】

systemutil.Run "D:\UFT12\samples\flight\app\flight4a.exe"
repositoriescollection.RemoveAll
repositoriescollection.Add "F:\qtp12_test\logintsr.tsr" '加载资源对象(实现资源对象共享)
Dialog("Login").WinEdit("Agent Name:").Set "mercury"
Dialog("Login").WinEdit("Password:").SetSecure "5a644c5216a3a90419ffb5e344001f617beb16af"
Dialog("Login").WinButton("OK").Click

【vbs控制qtp运行脚本(自动化对象模型AOM)】

'定义qtapp对象
Dim qtapp
'定义qtapp对象
Set qtapp=CreateObject("quicktest.application")
'启动uft
qtapp.Launch
'设置uft显示类型,若是false,表示uft后台运行,不显示
qtapp.Visible=True
'打开脚本
qtapp.Open "F:\qtp12_test\GUITest10"
'运行脚本
qtapp.Test.Run
'关闭脚本
qtapp.Test.Close
'退出qtapp对象
qtapp.Quit
'释放资源
Set qtapp=Nothing 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值