vb script for qtp guide

Upload: gautamsuresh

Post on 16-Jul-2015

86 views

Category:

Documents


0 download

TRANSCRIPT

VB Scripting - Important Terminology Before actually looking into VB scripting it is good to understand some terminology and also what they mean. We should know some thing about programming, scripting, compiler, scripting to have a better understanding.Lets look at what Programming and Scripting meansProgramming: Programming is used for application development and involves writing of code and should have in depth knowledge of a programming language. Scripting: Scripting on the other hand does not involve in writing the entire code for the application but involves in writing some lines of code or instructions so as to validate the application developed. To put in simple language Scripting is used for validation.

What is Compiler and InterpreterCompiler: Programming uses compiler to compiles the entire code in one go creates an executable file. This executable file will be used for execution. advantage of compiler is that it has a faster execution rate compared to interpreter. The big disadvantage of compiler is that there is no way you change your code in the executable file and you have to go back to the code make changes and again create an executable file. and The the can and

Interpreter: Scripting uses Interpreter to read the code one line at a time and executes one line at a time. The advantage is that if the interpreter encounters an error it stops executing the remaining lines and you have the chance to correct it and re run it. At any point of time you can interrupt wile running and change the code and continue running it. The biggest disadvantage is that each time the code is executed it has to read each line and is time consuming and is also slow.

What is VB Script? 1. VB Script is a scripting language. 2. A scripting language is a lightweight programming language. 3. VB Script is a subset of Microsoft's programming language Visual Basic. 4. VB Script is not case sensitive but as a part of best coding standards we use appropriate casing.How to create and run a VB script?It is very simple to create a VB script. Just write the code in a Microsoft Note pad and save the file by giving the file name with an .vbs extension. For example in the File name text box while saving you can enter this file name example1.vbs. In the Save as File choose the option All Files other wise it will save as a text file. As soon as you save your file as a VB script file you can run the script by double clicking on it.

Msgbox Function in VB Scripting is a very basic function and Lets start with a simple VB Script to display a message for the user using the Msgbox Function and try to understand how it works.

MsgboxStep 1: Open your note pad and type in this statement.

Msgbox This is First VB ScriptStep 2: Click File > Save As and in the Save window

1. Enter the fine name as sample.vbs 2. Choose All Files option for the Save as Type 3. Click Save button Step 3: On the desktop or the location you have given you will see a file with this icon

Step 4: Double clicking on this file your message box will be displayed with the predefined message you gave This is First VB Script

Lets understand what Msgbox function doesMsgbox is used for displaying the output. It is an inbuilt VB function used to output (display) a value to the user. This value can be a constant or a variable. Msgbox contains a OK button. Until the user clicks the OK button the execution will not move on to the next line.

Input box function in VB Scripting is used to get information from the user and then that value is stored in a variable.Type in the statement below in your note pad and save as sample2.

UserInput = Inputbox(Please Enter your Name)When you execute the file by double clicking it is displayed like this.

UserInput in the above statement is a variable and whatever the user enters in the text box is stored in the variable called UserInput. If we want to view the data entered on the screen then we need to call this variable in the Msgbox function and the variable value is displayed for the user. In order to do this modify your code like this

UserInput = Inputbox(Please Enter your Name) Msgbox "UserInput"

When you execute the file again with this changes what happens. First you are prompted to enter a value and clicking OK button will display the message box with the value entered. To put in as a definition Input box is an inbuilt VB Scrip function used for getting an input from the user during runtime. Input box provides a text box and allows the user to type in some data. When the user clicks the OK button this data is assigned to a variable. The default input type for an input box is a string.

VB Scripting has some building blocks and are important to know them and use them in an appropriate manner. Below are given the building blocks used in VB Scripting.1. Variable has something called as data types. 2. Operators like =,+,-,* etc.. 3. Branching statements to make decisions (If then else conditions) 4. Looping Constructs (For loop, While loop) 5. Functions and Procedures 6. String, Conversion, Date and Time 7. File System Object

What is a Variable?

Variable in VB Script is a place holder in the memory used to store some data or information.

The information assigned in the variable is a reserved section in the computers memory for storing data. The Memory used is temporary. The information stored in the memory is not permanent.

Rules for VBScript Variable usageA variable in the VB script cannot start with a numeric value (Ex 1stvalue). It can be a combination of Alphabets and Numbers together (Value1) or separated by a underscore (Value_1). There is no way you can use arithmetic operators and also space is not allowed because if space is given then VB script will recognize them as two different variables. Below are the basic rules followed while creating a Variable.

It must always begin with a letter Should not contain a period (.) or arithmetic operators (+, -, *, /, ^) The max length of the variable is 256 characters

Types of Variable DeclarationIn VB Script Variable Declaration happens in two ways:

1.ImplicitDeclaration. 2. Explicit Declaration.

Assigning values to a VariableA value to a variable can be assigned either using Implicit Declaration or Explicit Declaration. Example

Value1=20 Jobtype=PermanentFrom the above we can see that = is an assignment operator. The left hand side of the expression is the variable name and the right hand side of the expression is the value assigned to the variable.

Implicit Declaration:

Implicit Declaration is a default declaration type in VB and there is no need for us to declare a variable before assigning a value

to the variable. The declaration and definition happens in one step.Implicit declaration is easy to use. We dont need to keep track of the variables used in the VB script. Example

Int1 = 28 Int2 = 30 Prod = Int1 * Int2 Msgbox ProdLets see how this is stored in the memory.

From the above example Int1 and Int2 are operands and the * is the operator and = is the assignment operator. Int1, Int2, Prod are Implicit Variables and as the script is executed in the memory the variables get created in the memory and the respective values are stored in those locations. With this type of declaration it is easy to create variables as and when required. The one disadvantage of Implicit variables is that if there is a typo error in any line like for example "Prod" is mistyped as "Prd" then when the scrip is executed it creates another variable called "Prd" and the resultant out put is null (Nothing will be displayed). We should be careful in using the Implicit Declarations.

Explicit Declaration

Explicit Declaration is used to avoid the unnecessary confusion created while typing and also to avoid loading the memory with variables because of errors we can use Explicit Declaration. In order to use explicit declaration type we need to declare the variables before the script begins or it should be in the starting of the script.We will use the same example used for Implicit Declaration but will change the script. In order to create Explicit Declaration in the beginning of the code we need to add one more line of code as Option Explicit. Take a look at the code for Explicit Declaration Option Explicit Dim Int1, Int2, Prod Int1 = 28 Int2 = 30 Prod = Int1 * Int2 Msgbox Prod Now lets debug the code. If by mistake instead of Prod you type Prd in Line 6 as Msgbox Prd then what will happen. Since Prd is not declared when you run the script it executes the first five line and when it executes the sixth line it throws error and till it is not rectified the resultant output is not displayed. It will throw error as Variable is undefined: Prd.

This is the advantage of using Explicit Declaration.

What are the Data Types in VB Script?

Data types in VB Script is used in context of Variable. There is only one data type in VB Script called as Variant. The specialty of a Variant is that it can hold different forms of data or information within the variable and there is no need to explicitly define the type of data.A Variant can hold data which contains either a numeric or a string value. The Variant behaves in the way you want i.e. behaves as a numeric value when used in a numeric context and behaves like a string when used in a string context. The only difference is that any data which is enclosed in quotation marks (" ") behaves like a string even though it contains only numbers.

Variant SubtypesVariant can hold any type of data like Integer, String, Long, Single or double etc. The different categories of information/data which a Variant can hold are called as subtypes. Below table gives a clear picture of subtypes of data which a Variant can hold or contain.Subtype Integer Long Single Description The size is of 2 bytes, Value can be from 0 to 255, The Integer range is from -32,768 to 32,767. Long is for numbering system, The Integer range is from -2,147,483,648 to 2,147,483,647. Holds small precision, floating point range is from -3.402823E38 to -1.401298E45 for negative values and for positive values is from 1.401298E-45 to 3.402823E38. Holds big precision, floating point range is from -1.79769313486232E308 to -4.94065645841247E-324 for negative values and for positive values is from 4.94065645841247E-324 to 1.79769313486232E308.

Double

Currency Date (Time) Object Boolean Empty Null Byte

Ranges from -922,337,203,685,477.5808 to 922,337,203,685,477.5807. The range is between January 1, 100 to December 31, 9999. Contains an object. Contains a boolean value either True or False. Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables. Variant intentionally contains no valid data. Contains integer in the range 0 to 255.

You can use the conversion functions to convert the data from one subtype to another subtype which will be discussed in Type Cast lesson.

What is the need for Operators?

Operators in VB Script are to used to perform some operations or do some manipulation on variables or values. Operator is a notation to perform operation and this operation will happen on one or more operands.Operators in VB script can be classified in to mainly four. They are

Arithmetic Operators. Logical Operators. Relational or Comparison Operators. Concatenation Operator.

VB Script Arithmetic OperatorsArithmetic operators are used for arithmetic operations or mathematical operations such as Addition, Subtraction, Multiplication and other kinds of operations. Please find the list of Operators with some examples for easy understanding.Operator + * / \ ^ Mod English Meaning Add Subtract Multiply Floating Point Division Integer Division Exponent Modulus Example 5+2 5-2 5*2 5/2 5\2 5^2 5 Mod 2 Result 7 3 10 2.5 2 25 1

Logical OperatorsLogical operators are used to perform logical operations. They are used to manipulate statements or create logical expressions. The Logical operators are AND, OR and NOT. Please find the list of Operators with some examples for easy understanding.Operator Not Or And English Meaning Inverts Value Either Can Be True Both Must Be True Example Not False True or False True And False Result True True False

Relational or Comparison OperatorsRelational operators are also called as comparative operators like if we want to compare numbers etc. Generally they are used in conditional statements like IF Else or While loops. Please find the list of Operators with some examples for easy understanding.Operator = > < >= 45 20 20 >= 45 20