vbs基本编程
2018-01-17 15:30
134 查看
【常量变量的定义】
【字符串】
【转换函数】
【时间日期 函数】
【循环语句】
【选择分支语句】
【数组】
【函数封装】
【fso】
【新建excel】
【fso与excel综合(增强鲁棒性)】
【qtp对象导入导出(初始化)】
【vbs控制qtp运行脚本(自动化对象模型AOM)】
'常量变量的定义 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
相关文章推荐
- ◎Vbs脚本编程简明教程之八—FSO中文件夹的基本操作
- ◎Vbs脚本编程简明教程之十一 ——FSO中文件的基本操作
- ◎Vbs脚本编程简明教程之十一 ――FSO中文件的基本操作
- Vbs脚本编程简明教程之十一 ——FSO中文件的基本操作
- Unix网络编程 卷1 第8章:基本UDP套接字编程(UDP回射客户/服务器程序: main/dg_echo/dg_cli)
- [编程实例]vbs脚本删除桌面上的快捷方式腾讯QQ
- Java数据库编程基本配置
- C编程(二)基本输入输出函数用法
- MIC编程的基本知识
- JNI编程指南-第三章 基本类型、字符串、数组
- UNP总结 Chapter 4 基本TCP套接字编程
- linux多线程基本编程
- socket 网络编程基本的概念
- Unix网络编程 卷1 第8章: 基本UDP套接字编程(概述及recvfrom/sendto函数)
- GDI+编程10个基本技巧!
- vbs编程
- Win32编程基本概念
- Unity3D Shader的几种基本编程方式
- 基本的编程原则SOLID
- VC++ gdi+编程的基本代码