QTP-14 VBScript VBS基础
1. 熟练掌握下面的方法:
Strings:
Lcase() & Ucase() ‘大小写 strComp() StrReverse() ‘倒序 Len() Left() Right() Mid() InStr() InStrRev ‘倒序搜索 LTrim(), RTrim(), Trim() Replace() Space() ‘产生空格 String(3,”A”) ‘产生给定字母”AAA” |
Array:
Join Split Filter ‘返回含有特定字符串的数组元素 |
Conversion function
CBool, CByte, CCur, CDate, CInt, CLng, CSng, CStr Asc():Returns the ANSI character code to the first letter in a string Chr():Returns the character associated with the specified ANSI character code. Hex/Oct: converts a number to its Hexadecimal or Octal string |
Date and Time functions:
Date() Time() Now() Day/Month/Year Hour/Minute/Seconds Weekday ‘返回一周的第几天(7天制) DataDiff ‘两个日期相差的天数 Timer ‘计时(一天内) |
Others
Escape/ UnEscape Escape : Encodes a string so it contains only ASCII characters. UnEscape :Decodes a string encoded with the Escape function.
TypeName:Returns Variant subtype information about a variable. TypeName(4) ' Returns "Integer".
VarType: Returns a value indicating the subtype of a variable. MyCheck = VarType(#10/19/62#) ' Returns 7.
GetRef: Returns a reference to a procedure that can be bound to an event. Set object.eventname = GetRef(procname)
CreateObject: Creates and returns a reference to an Automation object. CreateObject(servername.typename [, location])
GetObject: Returns a reference to an Automation object from a file. GetObject([pathname] [, class])
UBound / LBound Returns the largest / smallest available subscript for the indicated dimension of an array.
Eval Evaluates an expression and returns the result. E.g. Eval("2=3")
|
2. How to clear new line, line feeds, and tab etc.whitespace characters in a string?
'This function clear new line, line feeds,tab ect. whitespace characters Function CleanText (s_Text) s_Text = Replace(s_Text, vbCrLf, "") s_Text = Replace(s_Text, vbCr, vbLf) s_Text = Replace(s_Text, Chr(7), "") s_Text = Replace(s_Text, vbCrLf, "") s_Text = Replace(s_Text, vbCrLf, vbLf) s_Text = Replace(s_Text, vbVerticalTab, vbLf) s_Text = Replace(s_Text, vbTab, "") End Function |
3. ExecuteStatement / ExecuteGlobal Statement
Execute one or more specified statements,defined as strings.
ExecuteGlobal: it executes the statement inglobal scope:
Dim x :x = 4 Call Func1
Sub Func1() Dim x : x = 3 Execute "print x" 'x= 3 local ExecuteGlobal "print x" 'x=4 global End Sub |
4. Arrays: fixed length & dynamic
a. Fixed length arrays:
Dix x(2) ‘ 2 dimensional
b. Dynamic Arrays:
Dim x()
ReDim x(3)
用关键字Preserve 来保留数组原来的值。
ReDim x(2,3) ReDim Preserve x(2,5) ‘保留了原来x(2,3)的值 |
Note:4.1 you can reDim as often as we want. But it can destroy the previous values.
4.2 多维数组用关键字Preserve, 只能 ReDim 数组的最后一维。
ReDim Preserve x(3,5) ‘error popup |
5. Optional arguments in VBS
QTP does not support optional parameters butcan be achieved by following ways:
Method 1: Optional arguments using Null/Empty values 'Function checks if param is null or empty Function IsMissing(ByVal param) On Error Resume Next IsMissing = IsNull(param) Or IsEmpty(param)
If err.Number<>0 Then IsMissing = False End If End Function
'this function can have value including Null or Empty Function Method_1(ByVal param1, ByVal param2) If IsMissing(param1) Then param1="default1" If IsMissing(param2) Then param1="default2" End Function |
Method 2: Optional arguments using Array members Function Method_2(Arguments) Dim ArrayArg 'check Arguments if missing 'check Arguments if array If VarType(Arguments) < vbArray Then Arguments = Array(Arguments) 'check if the dimension is missing ArrayArg = Arguments ReDim Preserve ArrayArg(2) '2 dimension is expcted 'then to see each dimension of Array is missing or not End Function
|
Method 3: Optional arguments using an Array of (Key, value)pairs 思路: 把参数和值放到字典中。 Set Params = CreateObject("Scripting.Dictionary") |
Method 4: Optional arguments using an Array of (key:=value) pairs |
Method 5: Optional arguments using an Array of Hybrid (Key, value of Key:=value) paris |