Вы находитесь на странице: 1из 29

VBScripting

Variable Variable is a memory location where we can store the values.

Variables are primarily categorized into 2 Types. 1) Scalar Variables ( It Can Hold Only 1 Value ) 2) Array Variables ( It Can Hold Multiple Value ) In VB Script This Variables are Further Divided into 2 types 1) Implicit Variable : If a Variable is used with out declaring , Then it is Called Implicit Variable . Ex: a=10 b=20 c=a+b In the above Program a , b , c are Called Implicit Variable. 2) Explicit Variable : If a variable is declared and utilized then This variables are called Explicit Variable . You Can Declare a Variable Using Dim . Ex 1 : Dim a,b,c

a=10 b=20 c=a+b In the above Program a , b , c are Called Explicit Variable. Note : By Default VB Script Allows both implicit and Explicit Variables Ex 2 : Dim a,b a=10 b=20 c=a+b In the above Program a , b are Called Explicit Variable and c is Called Implicit Variable. Note : There is a draw back with Implicit Variable , i.e. A Spelling Mistake also Consider as a new Variable because of this you may get In Correct Results . Ex 3 : Dim a,b,c a=10 b=20 c=a+d msgbox c

Option Explicit It Instructs VB Scripting Dont Allow Implicit Variable. If Option Explicit statement is specified Then every variable Must be Declared before Using that Variable. Ex 4 : Option Explicit Dim a,b,c a=10 b=20 c=a+d msgbox c You Will get Error in Above Program Array Variable Array Variables are useful to Store Multiple Values. Ex : Dim sname(5) ReDim sname(5) You can not Resize The Array You can Resize The Array

Ex 1: Dim sname(5)

sname(0)="A" sname(1)="B" sname(2)="C" sname(3)="D" sname(4)="E" msgbox sname(1) Dim sname(10) Note : It will Produce a error YBCZ in Dim we Can Not Resize Ex 2: ReDim sname(5) sname(0)="A" sname(1)="B" sname(2)="C" sname(3)="D" sname(4)="E" msgbox sname(1) ReDim sname(10) sname(5)="F" msgbox sname(1)

Note :

It will show Null Value in sname(1) YBCZ There Is no Preserve Ex : ReDim sname(5) sname(0)="A" sname(1)="B" sname(2)="C" sname(3)="D" sname(4)="E" msgbox sname(1) ReDim preserve sname(10) sname(5)="F" msgbox sname(1) Operators
1) Arithmetic 2) Relational 3) Logical 4) Concatenation 5) Assignment

1) Arithmetic + Addition

- Subtraction * Product / Division

2) Relational = <> > < >= <= 3) Logical And Or Not 4) Concatenation & Ex : a=1 msgbox a & "hai" Concatenate Two Different Data Type Values. && || ! Equals to Compare Greater then Less then Greater then or Equals to Less then or Equals to

5) Assignment Used to Assign Value to variable and variable to variable a = 10 a=b

VB Script Constant A Constant Value Can not be Changed in the same Action Ex : const a=10 msgbox a a=20 Note : It will Produce a error YBCZ a is constant . Built in Functions in VB Script

Asc Returns the ANSI character code corresponding to the first letter in a string.

Asc(string) The string argument is any valid string expression. If the string contains no characters, a run-time error occurs. Remarks In the following example, Asc returns the ANSI character code of the first letter of each string: Dim MyNumber MyNumber = Asc("A") ' Returns 65. MyNumber = Asc("a") ' Returns 97. MyNumber = Asc("Apple") ' Returns 65. -------------------------------------------------------------------Chr Function Returns the character associated with the specified ANSI character code. Chr(charcode) The charcode argument is a number that identifies a character. Dim MyChar MyChar = Chr(65) ' Returns A. MyChar = Chr(97) ' Returns a. --------------------------------------------------------------------

UCase Function Returns a string that has been converted to uppercase. UCase(string) The string argument is any valid string expression. If string contains Null, Null is returned. Remarks Only lowercase letters are converted to uppercase; all uppercase letters and non-letter characters remain unchanged. The following example uses the UCase function to return an uppercase version of a string: Dim MyWord MyWord = UCase("Hello World") ' Returns "HELLO WORLD". -------------------------------------------------------------------LCase Function Returns a string that has been converted to lowercase. LCase(string) The string argument is any valid string expression. If string contains Null, Null is returned. Remarks

Only uppercase letters are converted to lowercase; all lowercase letters and non-letter characters remain unchanged. The following example uses the LCase function to convert uppercase letters to lowercase: Dim MyString Dim LCaseString MyString = "VBSCript" LCaseString = LCase(MyString) ' LCaseString contains "vbscript". -------------------------------------------------------------------Len Function Returns the number of characters in a string . Arguments string Any valid string expression. If string contains Null, Null is returned. varname Any valid variable name. If varname contains Null, Null is returned. Remarks The following example uses the Len function to return the number of characters in a string: Dim MyString MyString = Len("VBSCRIPT") ' MyString contains 8.

-------------------------------------------------------------------LTrim; RTrim; and Trim Functions Returns a copy of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim). LTrim(string) RTrim(string) Trim(string) The string argument is any valid string expression. If string contains Null, Null is returned. Remarks The following example uses the LTrim, RTrim, and Trim functions to trim leading spaces, trailing spaces, and both leading and trailing spaces, respectively: Dim MyVar MyVar = LTrim(" vbscript ") ' MyVar contains "vbscript ". MyVar = RTrim(" vbscript ") ' MyVar contains " vbscript". MyVar = Trim(" vbscript ") ' MyVar contains "vbscript". -------------------------------------------------------------------Left Function Returns a specified number of characters from the left side of a string.

Left(string, length) Arguments string String expression from which the leftmost characters are returned. If string contains Null, Null is returned. length Numeric expression indicating how many characters to return. If 0, a zerolength string("") is returned. If greater than or equal to the number of characters in string, the entire string is returned. Remarks To determine the number of characters in string, use the Len function. The following example uses the Left function to return the first three characters of MyString: Dim MyString, LeftString MyString = "VBSCript" LeftString = Left(MyString, 3) ' LeftString contains "VBS". -------------------------------------------------------------------Right Function Returns a specified number of characters from the right side of a string. Right(string, length) Arguments

string String expression from which the rightmost characters are returned. If string contains Null, Null is returned. length Numeric expression indicating how many characters to return. If 0, a zerolength string is returned. If greater than or equal to the number of characters in string, the entire string is returned. Remarks To determine the number of characters in string, use the Len function. The following example uses the Right function to return a specified number of characters from the right side of a string: 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". -------------------------------------------------------------------Mid Function Returns a specified number of characters from a string. Mid(string, start[, length]) Arguments

string String expression from which characters are returned. If string contains Null, Null is returned. start Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string (""). length Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned. Remarks To determine the number of characters in string, use the Len function. The following example uses the Mid function to return six characters, beginning with the fourth character, in a string: Dim MyVar MyVar = Mid("VB Script is fun!", 4, 6) ' MyVar contains "Script". -------------------------------------------------------------------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.

Remarks If the user clicks OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string (""). The following example uses the InputBox function to display an input box and assign the string to the variable Input: Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input) -------------------------------------------------------------------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. MsgBox(prompt) msgbox Hai -------------------------------------------------------------------Print It Prints the Specified String or Variable Value in a Print log with out Stopping the Execution Ex

a=10 Print "Welcome" print "Hai" print a -------------------------------------------------------------------DatePart Function Returns the specified part of a given date. Settings The interval argument can have the following values:
Setting yyyy q m y d w ww h n s Year Quarter Month Day of year Day Weekday Week of year Hour Minute Second Description

Ex : x = DatePart("d", Now() ) msgbox x --------------------------------------------------------------------

Date Function Returns the current system date. Remarks The following example uses the Date function to return the current system date: Dim MyDate MyDate = Date ' MyDate contains the current system date. -------------------------------------------------------------------Time Function Returns a Variant of subtype Date indicating the current system time. Remarks The following example uses the Time function to return the current system time: Dim MyTime MyTime = Time ' Return current system time. -------------------------------------------------------------------Now Returns the current date and time according to the setting of your computer's system date and time. Now

Remarks The following example uses the Now function to return the current date and time: Dim MyVar MyVar = Now ' MyVar contains the current date and time. -------------------------------------------------------------------DateAdd Function Returns a date to which a specified time interval has been added. DateAdd(interval, number, date) The interval argument can have the following values:
Setting yyyy q m y d w ww h n s Year Quarter Month Day of year Day Weekday Week of year Hour Minute Second Description

Ex : x = DateAdd("m", 1, date())

msgbox x -------------------------------------------------------------------DateDiff Function Returns the number of intervals between two dates. DateDiff(interval, date1, date2) The DateDiff function syntax has these parts: The interval argument can have the following values:
Setting yyyy q m y d w ww h n s Description Year Quarter Month Day of year Day Weekday Week of year Hour Minute Second

Ex : x=DateDiff("d", "01/01/09",Date()) msgbox x --------------------------------------------------------------------

CInt Function Returns an expression that has been converted to a Variant of subtype Integer. Ex 1: a="10" b="20" msgbox a+b Ex 2: a="10" b="20" msgbox cint(a)+cint(b) -------------------------------------------------------------------CDbl Function Returns an expression that has been converted to a Variant of subtype Double. Ex 1: a="10.5" b="20.10" msgbox cint(a)+cint(b)

Ex 2 : a="10.5" b="20.10" msgbox cdbl(a)+cdbl(b) -------------------------------------------------------------------CDate Function Returns an expression that has been converted to a Variant of subtype Date. Ex : x=cdate("January 26 2009") msgbox x Conditional Statements in VB Script 1 ) If Then Else If condition Then [statements] Else [elsestatements] End If

2 ) Select Case it is used to execute a block of code out of multiple Blocks Select Case testexpression Case expressionlist-n [statements-n] Case expressionlist-n [statements-n] End Select Loops in VB Scripting 1) While Loop While Loop Execute the Specified Block of statements For Multiple Iterations when the Specified Condition Remains True, When Condition Remains False then the Loop will Exit. While < condition > Statements Wend

Ex : Dim i i=1 While i<=10 print i

i=i+1 Wend

2) Do While Ex : Dim i i=50 Do print i i=i+1 Loop While i<=10

3) Exit Do Ex : Dim i i=1 Do print i i=i+1 If i=10 Then Exit Do End If Loop While i<=100

4) For...Next Statement Repeats a group of statements a specified number of times. For counter = start To end [Step step] [statements] [Exit For] [statements] Next Ex 1: For i = 1 to 5 print i Next Ex 2 : For i = 1 to 5 step 2 print i Next Exit For Ex :

For i = 1 to 100 print i If i=10 Then Exit for End If Next User Defined Functions A Function or Procedure will Contain a set of Statements that are Designed to Perform a Specific task . Both Functions and Procedures are Re Usable. Note : A Function Returns a Value Where as A Procedure Does Not Return a Value Ex 1 : (Function with out Arguments ) Function sum() Dim a,b,c a=10 b=20 c=a+b

sum=c End Function x=sum() msgbox x Ex 2 : (Function with Arguments ) Function sum(x,y) Dim a,b,c a=x b=y c=a+b sum=c End Function k=sum(5,2) msgbox k Ex For Procedure : Sub sum(x,y) Dim a,b,c a=x b=y c=a+b msgbox c End Sub

Call sum(5,2) On Error Resume Next VB Script Provided on Error Statement to Enable or Disable Error Handling. 1) On Error Goto 0 It disables the error handling i.e. What Ever the Error Occurred During Test Execution That Error will be populated. ( It is Default in VB Scripting ) 2) On Error Resume Next It Enables the error handling i.e. The Statement where the Error Occurred During Test Execution That Error will be Skipped and QTP Proceeds with next Statement Execution Err.Number It Returns a Corresponding Error Code When a Error is Occur. It Returns 0 , If No Error Occur . Ex 1: On Error Resume Next Dim a , b,c a=10 b20 msgbox Err.Number

c=a+b print c

Ex 2: On Error Resume Next Dim a , b,c a=10 b=20 msgbox Err.Number c=a+b print c Err.Description It Returns a Corresponding Error Description When a Error is Occur. It Returns Null , If No Error Occur . Ex : On Error Resume Next Dim a , b,c a=10 b20 msgbox Err. Description c=a+b print c Ex : Define Error Handling Through Scripting to Handle the Errors That May Occur When Wrong Fly From or Fly to Values Supplied During Insert Order DDT.

Program
On Error Resume Next Window("Flight Reservation").Activate Window("Flight Reservation").WinMenu("Menu").Select "File;New Order" Window("Flight Reservation").ActiveX("MaskEdBox").Type "010110" Window("Flight Reservation").WinComboBox("Fly From:").Select datatable(1,1) If Err.Number <> 0 Then Window("Flight Reservation").WinComboBox("Fly From:").Select(0) Err.Number=0 Reporter.ReportEvent micWarning,"Fly From","Incorrect Data in Fly From" End If

Window("Flight Reservation").WinComboBox("Fly To:").Select datatable(2,1) If Err.Number<>0 Then Window("Flight Reservation").WinComboBox("Fly To:").Select(0) Reporter.ReportEvent micWarning,"Fly To","Incorrect Data in Fly To" End If

Window("Flight Reservation").WinButton("FLIGHT").Click Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click Window("Flight Reservation").WinEdit("Name:").Set "Ramesh" Window("Flight Reservation").WinButton("Insert Order").Click

Вам также может понравиться