Monday, March 30, 2009

VB

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 ThenMsgBox valueElseIf value = 1 ThenMsgBox valueElseIf value = 2 thenMsgbox valueElseMsgbox "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 – Loop

· While – Wend

· For – Next

Do – Loop

Do – Loop statements are 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.
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

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...Loop instead.

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.


If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS Feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

VBScript

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)VB_Script_code, 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:
  1. Dim Statement
  2. Public Statement
  3. 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:
  1. · Must begin with an alphabetic character.
  2. · Cannot contain an embedded period.
  3. · Must not exceed 255 characters.
  4. · 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.

Data Driven Testing

QTP has features to enable users to perform data-driven testing. For example, data can be output to a data table for reuse elsewhere. Data-driven testing is implemented as a Microsoft Excel workbook that can be accessed from within QTP. There are two types of Data Tables available in QTP: the Global data sheet and the local data sheets. The test steps read data from these data tables in order to (for example) drive variable data into the application under test, and verify the expected result.

Wednesday, March 25, 2009

QTPTutorial

1. What is VBScript?

http://automationtoolfortesting.blogspot.com/2007/05/vbscript-in-qtp.html

2. What is difference between VBScript and VBA?

VBScript is a subset of Visual Basic for Applications, but there are still many differences:

VBScript doesn't have a debugger like Visual Basic or you can say that VBScript does not provide any debugging features. You'll resort to using lots of message boxes, instead.

Unlike Visual Basic and Visual Basic for Applications, in which the developer can define the data type of a variable in advance, all variables in VBScript are variants.

There is no integrated development environment for VBScript that parallels the IDE for Visual Basic and Visual Basic for Applications.

Because variables are untyped and code is not compiled, all external objects instantiated in VBScript code are necessarily late-bound. This has a number of implications. First, late binding typically entails a substantial performance penalty in comparison to early binding. Second, while the properties and methods of early-bound objects can be examined in Visual Basic or hosted VBA environments using the Object Browser, this is not the case with late-bound objects. Finally, the help facilities available for early-bound objects in VB and VBA (like Auto List Members and Auto Quick Info) are not available, making syntax errors more likely and ready access to good documentation all the more necessary.

3. What is the difference between function and procedure?

A function, which is called differently from a subroutine, returns a value to the code that calls it, while a procedure (subroutine) does not. Functions must be set equal to a return value:

Variable_1 = My_Function(A, B, C)

A subroutine, on the other hand, isn't set to a variable. It can be called like

My_Subroutine A, B, C
Or
Call My_Subroutine(A, B, C)

4. Is VBScript a case-sensitive or case-insensitive?

VBScript is a case-insensitive programming language; i.e. case is ignored when reading VBScript scripts. So myVar, MyVar, MYvar all refer to the same variable.

5. What are the Data Types supported by VBScript?

VBScript has only one data type called a Variant. There are different categories of information that can be contained in a Variant, called subtypes. These subtypes are:

Empty, Null, Boolean, Byte, Integer, Long Single, Double, Date/Time, Currency , String , Object and Error.

6. By default in VBScript the arguments passed to functions and subroutines are by reference or by value?

By default, arguments are passed to functions and subroutines by reference; that is, the address of the argument is provided to the function or subroutine

7. Which function allows you to instantiate an object given its programmatic identifier or ProgID?

CreateObject function

8. Discuss all the ways you know to create an Array in VBScript?

http://automationtoolfortesting.blogspot.com/2007/05/vbscript-arrays.html

9. What is Dictionary object in VBScript? Explain.

The Dictionary object stores name / value pairs (referred to as the key and item respectively) in an array.

Write the below code in a notepad and save it with a .vbs extension and run it from Command Prompt by just typing the name of the file.

Dim Beers
Set Beers = CreateObject("Scripting.Dictionary")
Beers.Add "a", "Strauss"
Beers.Add "b", "Kingfisher"
Beers.Add "c", "Budweiser"
Msgbox ("The value corresponding to the key 'b' is " & Beers.Item("b"))

The Dictionary object has many properties like Count, Item, CompareMode etc and many methods like Exists, Add, and Keys etc.

10. What is the difference between a dictionary and an array?

A dictionary can't be multidimensional but an array can be.

A dictionary has extra methods to add new items and check for existing items.

When you delete a particular item from a dictionary, all the subsequent items automatically shift up. For example, if you delete the second item in a three-item dictionary, the original third item automatically shifts up into the second-item slot.

11. What is the purpose of On Error Resume Next statement?

The On Error Resume Next statement provides you some degree of error handling by preventing program interruptions from runtime errors.

When an error occurs, by using this statement, the line of code containing the error is simply skipped over and the program continues.

On Error Resume Next statement does not correct an error, just ignore it, without even displaying the error message.

The Statement essentially has two ways it is used. First to turn off the script engine error checking:

On Error Resume Next

The second is to turn it on, thereby allowing the script engine to stop execution of a script when an error is encountered. Like so:

On Error Goto 0

12. Which object provide information about a single runtime error in a VBScript?

Err Object

13. How do you declare a class in VBScript?

http://automationtoolfortesting.blogspot.com/2007/06/vbscript-classes.html

14. What's the difference between VBScript and VB.NET?

VBScript is script - a text-based code that gets interpreted by a "host", like the Windows Script Host or IE. VB.Net is a semi-compiled language for writing java-like software.

15. Which command is used for writing text on a page?

Document.Write (text)

16. What aspects of VBScript make it safe so that a Web page using VBScript cannot destroy or corrupt information on a user's computer?

The VBScript code that comes along in a Web page cannot directly access any files on the client's computer. This prevents a Web page from making any modifications to sensitive files on the user's computer. VBScript is also very safe in that it doesn't give programmers the ability to create unrecoverable crashes by virtue of its language syntax.

17. Write the code required within a procedure to create a variable for storing your name with VBScript code. Assign your name to that variable.

Dim Name
Name = "Harry Manis"

18. Assume you have a variable called Age that contains an age provided by the user. Write the code that ensures that the user has entered a number as her age. If the user hasn't entered a number, tell her she needs to enter her age correctly.

If IsNumeric(Age) = False Then
MsgBox "The size should be a number. Please type it again."
End If

19. Write a VBScript procedure that converts feet to inches. Hint: There are 12 inches in a foot.

Option Explicit

Sub feet_to_inch()
Dim Inches, Feet
Feet=inputbox("Enter feet value ")
Inches = Feet * 12
MsgBox "There are " & Inches & " inches in " & Feet & " feet."
End Sub

Call feet_to_inch()

20. Why is the use of Exit Do or Exit For statements within loops discouraged?

These statements are acceptable for specific conditions, but they often make code less readable, which makes it difficult for others who look at your code to know what's really going on. If you make sure the only way out of a loop is to not satisfy the loop's condition, it's much easier to follow your code. Often, breaking out of a loop by force with an exit statement is a sign of a poorly constructed loop with a condition that does not meet your goals. Take a look at the loop structure and condition again to make sure you're not leaving something out.

21. Give an example of if...then...elseif statement.

http://automationtoolfortesting.blogspot.com/2007/05/vbscript-conditional-statements.html

22. What is the use of Property Let and Property Get procedure?

http://automationtoolfortesting.blogspot.com/2007/08/vbscript-property-let-property-get.html

23. Explain about VBScript coding conventions?

http://msdn.microsoft.com/hi-in/library/ektke1b0(en-us,VS.85).aspx

24. What is the difference between VB Debugger and the Script Debugger?

Difference between VB Debugger and the Script Debugger:

No "on the fly" editing
Because the scripting window is read-only, you cannot edit the code during execution, as you can most of the time with VB.

No Instant Watch (Shift-F9)
The VB debugger's instant watch facility, which allows you to highlight a variable in your code, press Shift-F9, and see the value of the variable, is not available in the Script Debugger.

Cannot set watches
Watches do not exist in the Script Debugger.

Cannot set the next statement
Using the VB Debugger, you can place the cursor on a line of code and, by clicking CTRL-F9, have program execution resume at that line. This is particularly useful to backtrack or to re-execute a section of code. Unfortunately, this feature is not available in the Script Debugger.


Also download the above questions, print them and read in your free time.

Subscribe to QTP RSS Feed to get more useful & interesting stuff.










Monday, March 23, 2009

QTP

Quick Test Professional (QTP) is an automated functional Graphical User Interface (GUI) testing tool created by the HP subsidiary Mercury Interactive that allows the automation of user actions on a web or client based computer application. It is primarily used for functionalregression test automation. QTP uses a scripting language built on top of VBScript to specify the test procedure, and to manipulate the objects and controls of the application under test."
Quick Test Professional (QTP) is an automated functional Graphical User Interface (GUI) testing tool created by the HP subsidiary Mercury Interactive that allows the automation of user actions on a web or client based computer application. It is primarily used for functionalregression test automation. QTP uses a scripting language built on top of VBScript to specify the test procedure, and to manipulate the objects and controls of the application under test."