QTP VB Script Built in Functions

Types of Vbscript Functions
o Conversions (25)
o Dates/Times (19)
o Formatting Strings (4)
o Input/Output (3)
o Math (9)
o Miscellaneous (3)
o Rounding (5)
o Strings (30)
o Variants (8)


Important Functions

1) Abs Function
Returns the absolute value of a number.
Dim num
num=abs(-50.33)
msgbox num

2) Array Function
Returns a variant containing an Array
Dim A
A=Array("hyderabad","chennai","mumbai")
msgbox A(0)
ReDim A(5)
A(4)="nellore"
msgbox A(4)

3) Asc Function
Returns the ANSI character code corresponding to the first letter in a string.
Dim num
num=Asc("A")
msgbox num

* It returns the value 65 *

4) Chr Function
Returns the character associated with the specified ANSI character code.
Dim char
Char=Chr(65)
msgbox char

* It returns A *

5) CInt Function
Returns an expression that has been converted to a Variant of subtype Integer.
Dim num
num=123.45
myInt=CInt(num)
msgbox MyInt

6) Date Function

Returns the Current System Date.

Dim mydate
mydate=Date
msgbox mydate

7) Day Function


Ex1) Dim myday
myday=Day("17,December,2009")
msgbox myday

Ex2) Dim myday
mydate=date
myday=Day(Mydate)
msgbox myday

8) DateDiff Function
Returns the number of intervals between two dates.
Dim myday
mydate=#02-17-2009#
x=Datediff("d",mydate,Now)
msgbox x

9) Hour Function

Returns a whole number between 0 and 23, inclusive, representing the hour of the day.
Dim mytime, Myhour
mytime=Now
myhour=hour (mytime)
msgbox myhour

10) Join Function
Returns a string created by joining a number of substrings contained in an array.
Dim mystring, myarray(3)
myarray(0)="Chandra "
myarray(1)="Mohan "
myarray(2)="Reddy"
mystring=Join(MyArray)
msgbox mystring

11) Eval Function

Evaluates an expression and returns the result.

12) Time Function
Returns a Variant of subtype Date indicating the current system time.
Dim mytime
mytime=Time
msgbox mytime

13) VarType Function
Returns a value indicating the subtype of a variable.

Dim MyCheck
MyCheck = VarType(300) ' Returns 2.
Msgbox Mycheck

MyCheck = VarType(#10/19/62#) ' Returns 7.
Msgbox Mycheck

MyCheck = VarType("VBScript") ' Returns 8.
Msgbox Mycheck

14) Left Function

Dim MyString, LeftString
MyString = "VBSCript"
LeftString = Left(MyString, 3) ' LeftString contains "VBS".


14) Right Function


Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr = Right(AnyString, 1) ' Returns "d".
MyStr = Right(AnyString, 6) ' Returns " World".
MyStr = Right(AnyString, 20) ' Returns "Hello World".

15) Len Function


Returns the number of characters in a string or the number of bytes required to store a variable.

Ex 1): Dim Mystring
mystring=Len("G.C.Reddy")
msgbox mystring

Ex 2): Dim Mystring
Mystring=Inputbox("Enter a Value")
Mystring=Len(Mystring)
Msgbox Mystring

16) Mid Function
Returns a specified number of characters from a string.
Dim MyVar
MyVar = Mid("VB Script is fun!", 4, 6)
Msgbox MyVar

* It Returns ‘Script’ *

17) Timer Function
Returns the number of seconds that have elapsed since 12:00 AM (midnight).

Function myTime(N)
Dim StartTime, EndTime
StartTime = Timer
For I = 1 To N
Next
EndTime = Timer
myTime= EndTime - StartTime
msgbox myTime
End Function
Call myTime(2000)

17) isNumeric Function


Dim MyVar, MyCheck
MyVar = 53
MyCheck = IsNumeric(MyVar)
msgbox MyCheck

MyVar = "459.95"
MyCheck = IsNumeric(MyVar)
msgbox MyCheck

MyVar = "45 Help"
MyCheck = IsNumeric(MyVar)
msgbox MyCheck

* It Returns True/False like Result *

18) Inputbox Function
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.

Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input)

19) Msgbox Function

Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.
Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox Example")
VBScript syntax rules and guidelines

21.1 Case-sensitivity:


By default, VBScript is not case sensitive and does not differentiate between upper case and lower-case spelling of words, for example, in variables, object and method names, or constants.

For example, the two statements below are identical in VBScript:

Browser("Mercury").Page("Find a Flight:").WebList("toDay").Select "31"
browser("mercury").page("find a flight:").weblist("today").select "31"

21.2 Text strings:

When we enter a value as a text string, we must add quotation marks before and after the string. For example, in the above segment of script, the names of the Web site, Web page, and edit box are all text strings surrounded by quotation marks.

Note that the value 31 is also surrounded by quotation marks, because it is a text string that represents a number and not a numeric value.

In the following example, only the property name (first argument) is a text string and is in quotation marks. The second argument (the value of the property) is a variable and therefore does not have quotation marks. The third argument (specifying the timeout) is a numeric value, which also does not need quotation marks.

Browser("Mercury").Page("Find a Flight:").WaitProperty("items count", Total_Items, 2000)

21.3 Variables:

We can specify variables to store strings, integers, arrays and objects. Using variables helps to make our script more readable and flexible

21.4 Parentheses:


To achieve the desired result and to avoid errors, it is important that we use parentheses () correctly in our statements.

21.5 Indentation:

We can indent or outdent our script to reflect the logical structure and nesting of the statements.

21.6 Comments:


We can add comments to our statements using an apostrophe ('), either at the beginning of a separate line, or at the end of a statement. It is recommended that we add comments wherever possible, to make our scripts easier to understand and maintain.

21.7 Spaces:


We can add extra blank spaces to our script to improve clarity. These spaces are ignored by VBScript.

Errors

We have two types Errors in VB Script; they are VBScript Run-time Errors and VBScript Syntax Errors

13.1 VBScript Run-time Errors

VBScript run-time errors are errors that result when our VBScript script attempts to perform an action that the system cannot execute. VBScript run-time errors occur while our script is being executed; when variable expressions are being evaluated, and memory is being dynamic allocated.


13.2 VBScript Syntax Errors


VBScript syntax errors are errors that result when the structure of one of our VBScript statements violates one or more of the grammatical rules of the VBScript scripting language. VBScript syntax errors occur during the program compilation stage, before the program has begun to be executed.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.