VBS 学习

Posted amyhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBS 学习相关的知识,希望对你有一定的参考价值。

VBS其他功能

    1. 获取系统用户名

      DimWshNetwork

      Set WshNetwork =CreateObject("WScript.Network")

      strTaccount = WshNetwork.UserName

      strRoot = "D:"&strTaccount&"DesktopQAAutomation"

    2. 获取系统时间

      year(Now)& Month(Now) & day(Now) & Hour(Now) & Minute(Now) & Second(Now)

      randomize()

      dim Random:Random=int(rnd*999+1)

    3. Split ,Ubound,For each

      Dim MyStrs
      MyStrs=split("a,b,c,d",",")
      msgbox ubound(MyStrs)

      For each Strs In MyStrs

      Print Strs

      Next

      返回一个下标从0到ubound(MyStrs)的数组,数组元素共ubound(a)+1个

    4. 启动应用程序

      a.

      SetStartURL ="https://agent.monthly.qa.motoristsinsurance.com/home/"

      Set IE =CreateObject("InternetExplorer.Application")

      IE.Visible= true

      IE.NavigateStartURL    

      b.

      Dim IE,Chrome, strURL

      IE ="C:Program FilesInternet Exploreriexplore.exe"

      Chrome ="C:Program Files (x86)GoogleChromeApplicationchrome.exe"

      strURL ="https://agent.monthly.qa.motoristsinsurance.com/home/"

      ‘SystemUtil.Run  IE ,strURL ,,,3

      SystemUtil.Run  Chrome ,strURL ,,,3

 

  1. 关闭后台进程

    On ErrorResume Next

       Set objWMIService =GetObject("winmgmts:{impersonationLevel=impersonate}!\. ootcimv2")

       Set arrProcesses =objWMIService.ExecQuery( "select * from win32_process where Name=‘"&strProcName&"‘" )

       For Each proccess In arrProcesses

       proccess.Terminate 0

    Next

  2. 获取系统时间 

    CurrentTime = year(Now)&"-"&Month(Now)&"-"&day(Now)&"-"&Hour(Now)&":"&Minute(Now)&":"&Second(Now)

  3. 后台启动UFT跑vbs脚本

    ‘Create QTP object

    Set QTP = CreateObject("QuickTest.Application")

    QTP.Launch

    QTP.Visible = TRUE

    ‘Open QTP Test

    QTP.Open "\MGPCTFSW01UserFolders$ 165050DesktopPortal_Smoke_1214Portal_MainScript", TRUE

    ‘Set Result location

    Set qtpResultsOpt = CreateObject("QuickTest.RunResultsOptions")

    qtpResultsOpt.ResultsLocation = "\MGPCTFSW01UserFolders$ 165050DesktopPortal_Smoke_1214Result"

    ‘Run QTP test

    QTP.Test.Run qtpResultsOpt

    ‘Close QTP

    QTP.Test.Close

    QTP.Quit

     

    ‘ batch run vbs: CScript "\MGPCTFSW01UserFolders$ 165050DesktopQTP.vbs"

  4. Others

    去掉字符串前面和后面的空格 Trim()

    从字符串中返回指定数目的字符 Mid(string, start[, length])

    从字符串右边返回指定数目的字符 Right(string, length)

    返回指定数目的从字符串左边算起的字符 Left(string, length)

    把变量b转换为字符串 a=CStr(b)

    将一个字符串中的小写字母转化为大写字母 Ucase()

    返回一个字符串在另一个字符串中首次出现的位置 InStr()

  5. Load function

     LoadFunctionLibrary "\MGPCTFSW01UserFolders$"&UserAccount&"DesktopPortal_Smoke_1214FunctionsFunctions.qfl"

     

  6. 键盘输入

     

 

         Dim wshshell

         Set wshshell=createobject("wscript.shell")

         wshshell.sendkeys"{ENTER}"

  

 

 



以上是关于VBS 学习的主要内容,如果未能解决你的问题,请参考以下文章

VBScript基础篇

VBS基础语法讲解

Vbs脚本经典教材

qtp:vbs基础教程

VBS教程:创建txt文件

关于VBS功能上的问题