![]() |
|
![]() |
| LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Senior Member Join Date: Sep 2007 Location: Bangalore Age: 27
Posts: 171
Thanks: 33
Thanked 130 Times in 40 Posts
Rep Power: 17 | VB script in QTP What is VBScript? VBScript is a subset of Visual Basic 4.0 language. It was developed by Microsoft to provide more processing power to Web pages. VBScript can be used to write both server side and client side scripting. (If you already know Visual Basic or Visual Basic for Applications (VBA), VBScript will be very familiar. Even if you do not know Visual Basic, once you learn VBScript, you are on your way to programming with the whole family of Visual Basic languages.) Data types 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. It is possible to make numbers behave as strings by enclosing them within quotes. Variables A variable is a placeholder that refers to a memory location that stores program information that may change at run time. A variable is referred to by its name for accessing the value stored or to modify its value. Variable Declaration Variables in VBScript can be declared in three ways: Ø Dim Statement Ø Public Statement Ø Private Statement For example: Dim No_Passenger Multiple variables can be declared by separating each variable name with a comma. For example: Dim Top, Left, Bottom, Right You can also declare a variable implicitly by simply using its name in your script.That is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script. 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. Naming Convention There are standard rules for naming variables in VBScript. A variable name: · 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. Assigning Values to Variables Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example: B = 200 Scalar Variables and Array Variables Much of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 11 elements is declared: Dim A(10) Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array. Constants 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. You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values. For example: Const MyString = "This is my string." Const MyAge = 49 Note that the string literal is enclosed in quotation marks (" "). Also note that constants are public by default. Within procedures, constants are always private; their visibility can't be changed. Next post we will deal with constructs and arrays. (PART 2) This is in continuation from VB Script and QTP - Part1 on our series of posts on VB Script. Here, we will dwell upon conditional constructs, iterative constructs and arrays. Conditional Constructs Conditional Constructs execute statements or repeat certain set of statements based on conditions. The following conditional constructs are available in VBScript Ø · If – Then –Else Ø · Select Case If – Then – Else Construct The If – Then- Else Construct is 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. The If- Then – Else statements can be nested to as many levels as needed. For example: Sub ReportValue(value) If value = 0 Then MsgBox value ElseIf value = 1 Then MsgBox value ElseIf value = 2 then Msgbox value Else Msgbox "Value out of range!" End If You can add as many ElseIf clauses as you need to provide alternative choices. Extensive use of the ElseIf clauses often becomes cumbersome. A better way to choose between several alternatives is the Select Case statement. Select Case Construct The Select-Case structure is an alternative to If Then Else for selectively executing one block of statements from among multiple blocks of statements. The Select Case Construct makes code more efficient and readable. 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. For example: Select Case Document.Form1.CardType.Options(SelectedIndex).Text Case "MasterCard" DisplayMCLogo ValidateMCAccount Case "Visa" DisplayVisaLogo ValidateVisaAccount Case "American Express" DisplayAMEXCOLogo ValidateAMEXCOAccount Case Else DisplayUnknownImage PromptAgain End Select Iterative Constructs 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 – Ø While – Wend Ø For – Next Do – Do – The basic difference between a “Do while – While – Wend The While...Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in while...wend, it is recommended that you use Do... For..Next 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 Step Keyword is used to increase or decrease the counter variable by the value that is specified along with it. The For-Next statement can be terminated before the counter reaches its end value by using the Exit For statement. For example: Dim j, total For j = 2 To 10 Step 2 total = total + j Next MsgBox "The total is " & total Arrays 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. Arrays are used to store related data values. VBScript allows you to store a group of common values together in the same location. These values can be accessed with their reference numbers. 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. Declaring Arrays An array must be declared before it can be used. Depending upon the accessibility, arrays are of two types: · 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. The Dim statement is used to declare arrays. The syntax for declaring an array is as follows: Dim ArrayName(subscriptvalue) Where, ArrayName is the unique name for the array and SubscriptValue is a numeric value that indicates the number of elements in the array dimension within the array. Example: Dim No_Passengers(3) The No_Passengers can store 4 values. Assigning values to the array No_Passengers(0) = 1 No_Passengers(1) = 2 No_Passengers(2) = 3 No_Passengers(3) = 4 Static and Dynamic Arrays: VBScript provides flexibility for declaring arrays as static or dynamic. A static array has a specific number of elements. The size of a static array cannot be altered at run time. 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. Next we will deal with user defined procedures, functions and subroutines. This post is third in the VB Script and QTP series. Here we shall talk about user defined procedures. Procedures are set of executable statements. In VBScript, there are two types of procedures: Ø Sub Procedures Ø Function Procedures Sub Procedures A sub procedure is a series of VBScript statements, enclosed by Sub and End Sub statements which perform actions but do not return a value. A sub procedure can take arguments. If a sub procedure doesn’t receive any arguments, its Sub statement must include an empty parenthesis(). The following Sub procedure uses two intrinsic, or built-in, VBScript functions, MsgBox and InputBox , to prompt a user for information. It then displays the results of a calculation based on that information. The calculation is performed in a Function procedure created using VBScript. The Function procedure is shown after the following discussion. Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Function Procedures A function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A function procedure is similar to a sub procedure but it can return value to the calling function. A function procedure can take arguments (constants, variables or expressions that are passed to it by a calling procedure). If a function procedure has no arguments, it Function statement must include an empty set of parenthesis. A function returns a value by assigning a value to its name in one or more statements of the procedure. Since VBScript has only one base data type, a function always returns a variant. In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box. Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function Tips: Ø To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't. Ø AFunction in your code must always be used on the right side of a variable assignment or in an expression. Ø To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. Ø The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing. Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg Notice that the parentheses are omitted in the call when the Call statement isn't used. Thanks Arun
__________________ Arunsankar Value has a Value-Only if its Value is Valued |
| | |
| The Following 2 Users Say Thank You to aforarun For This Useful Post: | AjayKumar.Kataram (24-11-08),
suresh_edk (23-02-09)
|
| | #2 (permalink) |
| Junior Member Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 | Re: VB script in QTP Thanks for the Information. Can anyone help me out in sending IBM Rational Robot User guide or the link related to it |
| | |
| | #3 (permalink) |
| Senior Member Join Date: Sep 2007 Location: Bangalore Age: 27
Posts: 171
Thanks: 33
Thanked 130 Times in 40 Posts
Rep Power: 17 | Re: VB script in QTP
__________________ Arunsankar Value has a Value-Only if its Value is Valued |
| | |
| The Following User Says Thank You to aforarun For This Useful Post: | AjayKumar.Kataram (24-11-08)
|
| | #4 (permalink) | |
| Junior Member Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 | Quote: Hi, Thanks for the link,but im not able to open.I'l chk again and get back to u | |
| | |
| | #5 (permalink) | |
| Junior Member Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 1 | Quote: I have one doubt. What is the command used in VB script to Insert Rows in Excel For ex:If we enter no.of rows as 2,that many rows should get added up in excel Kindly send me a sample code/guide on this | |
| | |
| | #6 (permalink) |
| Senior Member Join Date: Aug 2006 Location: Hyderabad,India Age: 30
Posts: 8,044
Thanks: 2,105
Thanked 400 Times in 296 Posts
Rep Power: 122 | Re: VB script in QTP Useful information........... |
| | |
| | #7 (permalink) |
| Junior Member Join Date: Feb 2009 Age: 25
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1 | Re: VB script in QTP Thanx good one. |
| | |
| | #8 (permalink) |
| Junior Member Join Date: Mar 2009
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1 | Re: VB script in QTP Hi, Can anyone send me any VB script editor on which I can practise n learn VB script. Please if nyone has got the software or ny information do send me the same on vidhu_12@rediffmail.com its very urgent. Thank u :) |
| | |
| | #9 (permalink) | |
| Junior Member Join Date: May 2009 Age: 29
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1 | Re: VB script in QTP Quote:
| |
| | |
| | #10 (permalink) | |
| Junior Member Join Date: Jul 2008 Age: 30
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2 | Re: VB script in QTP Quote:
| |
| | |
![]() |
| Tags |
| qtp , script |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Learn QTP | vinodkumar.qtp | Testing Tools & QA | 5 | 07-11-09 12:35 PM |
| MANUAL TESTING CONCEPTS (pdf) | ramkumarbtech | Testing Tools & QA | 16 | 09-06-09 12:49 PM |
| Exhaustive list of questions on QTP | aforarun | Testing Tools & QA | 3 | 05-05-09 02:40 PM |
| Some Loadrunner FAQs | kiran2710 | Testing Tools & QA | 1 | 25-10-08 12:42 AM |
| How u write vb script in qtp pls give | ShivaniX | QTP | 0 | 19-05-08 08:14 PM |
| More Interview Questions Here... |