Date post: | 21-Oct-2014 |
Category: |
Technology |
View: | 381 times |
Download: | 1 times |
What is VBScript? VBScript Basics
Data types Variables Constants Conditional constructs Iterative constructs Arrays User defined Procedures Functions and subroutines
Integration of different interfaces Session Wrap-Up
pgorantla.blogspot.com
VBScript Overview
VBScript is a subset of Visual Basic 4.0 language.
Developed by Microsoft to provide more processing power to Web pages.
Used to write both server side and client side scripting.
Supports Internet Explorer and IIS (Internet Information Service).
Is a fast, portable and lightweight scripting language.pgorantla.blogspot.com
VBScript Overview
VBScript has its own collection of
Data types Constants Iterative constructs Conditional constructs Procedures
pgorantla.blogspot.com
VBScript Overview
VBScript supports only one data type called ‘Variant’.
The variant data type is a special kind of data type that can contain different kinds of information.
It is the default data type returned by all functions in VBScript.
A variant behaves as a number when it is used in a numeric context and as a string when used in a string context.
Subtypes of data that a variant can contain: Empty , Boolean, Integer, Long, Single, Byte, Double, Date
Time, string, Object, Error.
pgorantla.blogspot.com
VBScript Overview
Sub-Type Value
Empty When the variable is not initialized
Boolean Either true or false
Integer In the range of -32768 to 32767
Long In the range of -2147483648 to 2147483647
Single Single precision, floating point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Byte In the range of 0 to 255
Double Double precision, floating point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
DateTime Represents a date between Jan 1, 1100 to Dec, 31 9999
String Variable length string that can be up to approximately 2 billion characters in length
Object An OLE Object
Error Contains an error number
pgorantla.blogspot.com
VBScript Overview
Variable is a placeholder that refers to a memory location that stores program information that may change at run time.
Usage: Referred to by its name for accessing the value stored or to modify its value
Declaration: Variables are explicitly declared in the script using:
Dim Statement Public statement Private statement
Multiple variables can be declared by separating each variable name with a comma. Examples
Dim intPassngrCnt 'Declaring variable for storing Passenger Count Dim MyVar, MyNum ' Declare two variables
Naming Convention: Must begin with an alphabetic character Cannot contain an embedded period Must not exceed 255 characters Must be unique in the scope in which it is declared
pgorantla.blogspot.com
Note:• Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level,
variables are available only within the procedure. • Public statement variables are available to all procedures in all scripts.• Private statement variables are available only to the script in which they are declared
VBScript Overview
A constant is a meaningful name that takes the place of a number or a string, and never changes. VBScript in itself has a number of defined intrinsic constants like vbOK, vbCancel, vbTrue, vbFalse and so on.
Declaration: The Const statement is used to create constants. Using the Const statement, you can create string or numeric
constants with meaningful names and assign them literal values Const service_class = “Business” Const No_Passenger = 2
Note that the string literal is enclosed within quotations. Date and time literals are represented by enclosing them in number signs (#) Const dt_today = #27-07-2005#
Constants can be declared as Private or Public. These keywords can be prefixed before the Const keyword to make them act as one. Private Const CnstAirways=”Blue line Airways”
pgorantla.blogspot.com
Note:• Constants are public by default. • Within procedures, constants are always private; their visibility can't be changed.
VBScript Overview
Conditional Constructs execute statements or repeat certain set of statements based on conditions.
The following conditional constructs are available in VBScript If – Then –Else
Used to evaluate whether a condition is true or false and depending on the result, to specify one or more statements to execute.
Usually the condition is an expression that uses a comparison operator to compare one value or variable with another.
Can be nested to as many levels as needed.
Select Case Is an alternative to If Then Else for selectively executing one block of statements from
among multiple blocks of statements. A Select Case structure works with a single test expression that is evaluated once, at the
top of the structure. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed.
Rwerwerwrrwrw Werewrwr Wreew Werwr wer
pgorantla.blogspot.com
Advanced QTP 8.0
Looping allows to run a group of statements repeatedly. The loop is repeated based on a condition. The loop runs as long as the condition is true. The following looping constructs are available in VBScript. Do – Loop
Do – Loop Construct is used to execute a block of statements based on a condition.
The statements are repeated either while a condition is true or until a condition becomes true.
While Keyword can be used to check a condition in a Do – Loop construct.
The condition can be checked before entering into the loop or after the loop has run at least once.
Pre-Check Do-Loop ConstructExample:‘Code startsDim intCntIntcnt=0Do while intCnt < 4
msgbox intCntintCnt=intCnt+1
Loop‘Code ends
This will run for how many times?pgorantla.blogspot.com
VBScript Overview
Post-Check Do-Loop Construct‘Code startsDim intCntIntcnt=0Do
msgbox intCntintCnt=intCnt+1
Loop while intCnt > 4‘Code ends
This will run for how many times? The Do-Loop construct can be terminated before the
condition fails by using the Exit Do statement. The basic difference between a “Do while – Loop” and
“Do - Loop while” is that the previous one gets executed only when the condition in the while statement holds true where as a “Do – Loop while” gets executed atleast once, because the condition in the while statement gets checked at the end of the first iteration.
While – Wend This While – Wend Construct is same as the “Do while –
Loop” construct.pgorantla.blogspot.com
VBScript Overview
For … Next Loop The For-Next loop can be used to run a block of statements a specific number of times. For loops use a counter variable whose value is increased or decreased with each repetition of the
loop. The Next statement by default increments the counter variable by 1.
Example #1:‘Code StartsDim intCntFor intCnt=1 to 5 ‘Will initialise the intCnt to 1 & then proceed with the loop condition
checking.Msgbox intCnt
Next ‘Control shifts back to For statement from here with increment of intCnt by 1.
‘Code endsOutput – 1, 2, 3, 4, 5
Example #2:‘Code StartsDim intCntFor intCnt=1 to 5 Step 2 ‘Will initialise the intCnt to 1 & then proceed with the loop
condition checking.Msgbox intCnt
Next ‘Control shifts back to For statement from here with increment of intCnt by 2.
‘Code endsOutput – 1, 3, 5
The For-Next statement can be terminated before the counter reaches its end value by using the Exit For statement.
pgorantla.blogspot.com
VBScript Overview
An array is a contiguous area in the memory referred to by a common name.
It is a series of variables having the same data type. Usage:
Arrays are used to store related data values. An array is made up of two parts, the array name and the array subscript. The subscript indicates the highest index value for the elements within the array. Each element of an array has a unique identifying index number by which it can be
referenced. VBScript creates zero based arrays where the first element of the array has an index value of
zero. Declaration:
The Dim statement is used to declare arrays. The syntax for declaring an array is as follows: Dim ArrayName(subscriptvalue)
Example: Dim arrStuMarks(6)
This example represents an array which will hold 6 elements. The first element will be represented as arrStuMarks(0)….and so and the last element will be
represented as arrStuMarks(5). Dim arrrMultiDimension(4)(3)
This example represents an arrau which will hold 12 elements in all, namely 4 rows constituting of 3 elements each per row.
So the first element of the first row will be arrrMultiDimension(0)(0), first element of the second row will be represented by arrrMultiDimension(1)(0) and the first element of the last row will be represented by arrrMultiDimension(3)(0).
pgorantla.blogspot.com
VBScript Overview
Assigning Values to Arrays:arrStuMarks(0)=1 ‘Assigning value to the first element of the arrStuMarks ArrayarrStuMarks(3)=2 ‘Assigning value to the fourth element of the arrStuMarks ArrayarrrMultiDimension(0)(1)=14 ‘Assigning value to the second element of the first row
of the arrrMultiDimension Array. Scope:
Local Arrays A local array is available only within the function or procedure, where it is declared.
Global Arrays A global array is an array that can be used by all functions and procedures. It is declared at
the beginning of the VBScript code. Types:
VBScript provides flexibility for declaring arrays as static or dynamic. Static Arrays
A static array has a specific number of elements. The size of a static array cannot be altered at run time.
Dynamic Arrays A dynamic array can be resized at any time. Dynamic arrays are useful when size of the array cannot be determined. The array size can be changed at run time.
pgorantla.blogspot.com
‘Code StartsDim arrSum(3) ‘Static arrayarrSum(0)=2 ‘Assigning valuesarrSum(1)=3‘Code Ends
‘Code StartsDim arrSum() ‘Dynamic array‘Some more code in betweenReDim arrSum(25) ‘declaring dimension to the dynamic arrayarrSum(0)=2 ‘Assigning valuesarrSum(24)=3‘Code Ends
VBScript Overview
Procedures are set of executable statements, which has been given a name. In VBScript, there are two types of procedures:
Sub Procedures Function Procedures
Sub Procedures: A Series of VBScript statements, enclosed by Sub and End Sub statements. Do not return a value.
Example:‘Code startsPromptUsrName ‘Call to the procedureSub PromptUsrName ( ) ‘Procedure definition
Dim strNamestrName= inputbox ("Please enter the user name", "Test Project")Msgbox “Parameter entered by you is ” & strName
End Sub ‘Procedure definition ends‘Code ends
Function Procedures: A series of VBScript statements enclosed by the Function and End Function statements. Can return value to the calling function.
Example:‘Code startsDim intAreaintArea=CalcArea(10,20) ‘Calling function CalcAreaMsgbox “The area of the rectangle is “ & intAreaFunction CalcArea(intheight, intwidth) ‘Function definition
‘Formula for calculating Area and then returning value to the calling statementCalcArea=intheight * intwidth
End function ‘Function ends‘Code ends pgorantla.blogspot.com
VBScript Overview
VBScript facilitates integration of different interfaces with QTP, like if the user wants Output of the QTP test on word document OR the user wants to read an excel sheet which will act as inputs for one of the QTP tests.
VBScript’s object oriented feature strengthens and eases the way to use object model of any package.
Example for using word with QTP
‘Code startsSystemUtil.Run "winword","","C:\Documents and Settings\padmashree.raichur",""Window("Microsoft Word_1").WinObject("Microsoft Word Document").Type _"This is a good document."Window("Microsoft Word_1").WinObject("Standard").Click 67,21Window("Microsoft Word_1").Window("Save As").WinListView("SysListView32").Select
"Good"Window("Microsoft Word_1").Window("Save As").Click 515,325Window("Microsoft Word_1").Window("Save As").Window("Microsoft Office Word").Click
251,125Window("Microsoft Word_1").close‘Code ends
The above mentioned program creates a new word document and writes the text “This is a good document” and renames it as “good.doc” and then closes the document.
pgorantla.blogspot.com
VBScript Overview
Case Study: We have some scenarios in the excel sheets (Test plans)
which are to be tested. Also each and every Scenario is having a respective QTP
test scripts already pre-recorded and stored in a particular location.
Now what we need to do is think of a way where in we could run the testscripts of QTP from the excel sheet itself.
Solution: Create macros in Excel. Add a Command button to each scenario on the excel
sheet and call the respective macro thru the same. In the macros, write the code to call and run the QTP
script. The user will then accordingly put a pass/fail against the
respective testcase in the excel sheet.
pgorantla.blogspot.com
VBScript Overview
pgorantla.blogspot.com
Q & A….
VBScript Overview
Basics of VBScript include: Data types Variables Constants Operators Procedures Iterative constructs Conditional Constructs
VBScript has only one data type called a variant. Variables are declared using the Dim statement,
the Public statement, and the Private statement. Constants can be created by using the Const
statement. Constants are a number or a string whose value never changes.
Procedures are of two types: Sub Function
pgorantla.blogspot.com
VBScript Overview
VBScript has the following conditional constructs: If – Then – Else statement Select Case Construct
VBScript has the following iterative constructs: Do Loop While-Wend Loop For-Next
The Exit For statement is used to terminate the For-Next Loop.
The Exit Do statement is used to terminate the Do-Loop. The basic difference between a “Do while – Loop” and
“Do - Loop while” is that the previous one gets executed only when the condition in the while statement holds true where as a “Do – Loop while” gets executed atleast once, because the condition in the while statement gets checked at the end of the first iteration.
Different interfaces like Excel, word can be interfaced with QTP and vice-a-versa using VBScript and the respective object model of the corresponding interfaces.
pgorantla.blogspot.com
VBScript Overview