Thursday, 22 September 2011

Data Driven Testing for Login Operation by fetching Test data directly from a text file

Option Explicit
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\test.txt")
myFile.SkipLine

Do Until myFile.AtEndOfStream=True
       
myLine=myFile.ReadLine
myField=Split(myLine,",")

SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set myField(0)
Dialog("text:=Login").WinEdit("attached text:=Password:").Set myField(1)
Wait (2)
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close
Loop

myFile.Close
Set objFso=Nothing

Reading From Excel File Without Importing The File

path_of_file = "C:\test.xls" 
sheet_ex ="Sheet1"       
row=1                              
column1=1                         


Function valuexcell(epath,esheet,erow,ecolumn)

   Set excelobj = createobject("Excel.Application")
   excelobj.Workbooks.Open epath
   Set mysheet = excelobj.Sheets.Item(esheet)
   value = mysheet.cells(erow,ecolumn)
   excelobj.Application.Quit
   Set excelobj = Nothing
   valuexcell = value


End Function

msgbox valuexcell(path_of_file,sheet_ex,row,column1)

Wednesday, 21 September 2011

File System Operation in QTP

Create a Folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\Testprimer"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)

Delete a Folder 
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder("E:\Testprimer")

Copying Folders
Set oFSO=createobject("Scripting.Filesystemobject")
oFSO.CopyFolder "E:\test", "C:\test1", True

Tuesday, 20 September 2011

Brief on VB Script Variables

A variable is a convenient placeholder that refers to a computer memory location where we can store program information that may change during the time our script is running.
Purpose of Variable:

a) Comparing values
Example:
Dim x,y,a
x=100
y=100
a=x=y
Msgbox a 'It returns True
b) Holding Program Result
Example:
Cost=Tickets*Price
c) Passing parameters
d) To store data that returned by functions
Example:
myDate=Now ‘ It returns current data & time
e) To hold data
Example:
myName=”bibhas”
Declaring Variables
We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.
For example:
Dim city
Dim x
We declare multiple variables by separating each variable name with a comma. For
Example:
Dim x, y, city, bsr
We can also declare a variable implicitly by simply using its name in our script. That is not generally a good practice because we could misspell the variable name in one or more places, causing unexpected results when our 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 our script.
Option Explicit Statement
Forces explicit declaration of all variables in a script.
Option Explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.
Naming Restrictions for Variables
Variable names follow the standard rules for naming anything in VBScript. A variable name:
a) Must begin with an alphabetic character.
Dim abc    'Right
Dim 9ab    'Wrong
Dim ab9    'Right
b) Cannot contain an embedded period.
Dim abc     'Right
Dim ab.c    'worng
Dim ab-c    'wrong
Dim ab c    'wrong
Dim ab_c   'Right
c) Must not exceed 255 characters.
d) Must be unique in the scope in which it is declared.
Scope of Variables
A variable's scope is determined by where we declare it.
When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable.
If we declare a variable outside a procedure, we make it recognizable to all the procedures in our script. This is a script-level variable, and it has script-level scope.
Example:
Dim x,y,z
x=10
y=20
z=x+y
msgbox z    'Returns 30
Function res
   Dim a,b,c
   a=30
   b=40
   c=a+b+y
   msgbox c   ' Returns 90
End Function
Call res
Life Time of Variables
The lifetime of a variable depends on how long it exists.
The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running.
At procedure level, a variable exists only as long as you are in the procedure.
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:
A = 200
City = “Hyderabad”
X=100: Y=200
Scalar Variables and Array Variables
A variable containing a single value is a scalar variable.
A variable containing a series of values, 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.
Example:
Dim A(3)
Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.
We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:
A(0) = 256
A(1) = 324
A(2) = 100
A(3) = 55
Similarly, the data can be retrieved from any element using an index into the particular array element you want.
For example:
SomeVariable = A(4)
Arrays aren't limited to a single dimension. We can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions.
In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:
Dim MyTable(5, 10)
In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.
Dynamic Arrays
We can also declare an array whose size changes during the time our script is running. This is called a dynamic array.
The array is initially declared within a procedure using either the Dim statement or using the ReDim statement.
However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.
For example:
Dim MyArray()
ReDim AnotherArray()
To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.
In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
ReDim MyArray(25)
ReDim Preserve MyArray(30)

How to Debug custom functions in QTP ?

Verify the code works when it is not within a function. If the code does not work when it is not in a function in a QuickTest Professional (QTP) script or within a pure VBS script (while QuickTest Professional is not in use), it will not work when it is placed within a function. The following example shows the step by step process of debugging a simple function.
Example:
Function ex_func(obj, txt)
msgbxo obj.Exist
msgboxx txt
End Function
To debug the above function (which has intentional typos), copy the statements into a QuickTest Professional script:
msgbxo obj.Exist
msgboxx txt
If needed, modify any references to a generic object to an actual object:
msgbxo Browser("Browser").Exist
msgboxx txt
Execute the code; debug as needed:
msgbox Browser("Browser").Exist ' corrected typo: msgbxo -> msgbox
msgbox txt ' corrected typo: msgboxx –> msgbox
Copy the corrected code into the function. Verify it works as expected:
Function ex_func(obj, txt)
msgbox Browser("Browser").Exist
msgbox txt
End Function
If needed, modify references to specific objects back to general references:
Function ex_func(obj, txt)
msgbox obj.Exist
msgbox txt
End Function
Verify the function works as expected.
(Optional) Move the function into a function library.


If function is returning a value, the value should be assigned to the function name. This is a VBScript requirement.
Example:
Function examplefunc
examplefunc = "returned value"
End Function
msgbox examplefunc


If function is saved in an external function library, make sure it associate the function library with the test script.

VB Script Fundamentals

VB Script has several purposes:

Client side scripting in the Web (HTML)(Browser) (IE)
Server side scripting in the Web (ASP) (Web Server)(IIS)
Network Administration (Server OS) (WSH-Windows Script Host)
System Administration (Client OS) (WSH-Windows Script Host)
Test Automation (QTP)

QTP Point of view below are the learning objectives:

- Adding Comments

- Data types
- Declarations (Variables (Scalar and Array),Constants))
- VB Script Operators
    i) Arithmetic Operators (Including Concatination operators)
    ii) Comparison
    iii) Logical
   
- Flow Control Statements (a. Conditional Statements)
        i) If...Then...Else...End If
        ii) Select Case...Case...Case Else...End Select


- Flow Control Statements (b. Loop Statements)
        i) For...Next
        ii) While...Wend
        iii) Do While/Until...Loop
        iV) For Each...Next

- VB Script Procedures(Functions)
        i) Built-in Functions(String, Array, Math,Date & Time, Conversion functions etc...)
        ii) User defined
            1) Sub Procedures
            2) Function Procedures

- Coding Convensions
- File System Operations
- Excel sheet Operations
- database Operations
- Other VB Script Objects
        i) Dictionary Object
        ii) Word
        iii) Internet Explorer Object
        iV) RegExp Object

- Regular expressions
- Error Handling

A Comparision Between Scripting Languages & Programming Languages

Scripting Language:
Scripting Language is a Light weight language, no need to compile them separately, during execution they automatically compile an run.
1) It is an Interpreter based Language
2) Interpreter converts high level instructions into machine language line by line
3) It doesn’t create executable file.
4) No need to compile the program
5) It takes less code for achieving tasks.
6) It greatly reduces development time
7) It reduces maintenance of cost
8) Implicit Support of Data Types.
9) Limited support for User Interface Design
10) Limited or No Support for Graphics Design


Example Scripting Languages are:
Shell, Perl, VB Script, Java Script, Python, Ruby, Rexx, PHP Etc..

Programming Language:
It is a compiler based Language.
Compiler converts the whole program in single short into machine language. 

It Creates .exe file. Need to compile the program
It takes numerous lines of code
It increases development time
It Increases maintenance of cost
Explicit support of Data Types
Rich support for User Interface Design
Rich Support for Graphics Design


Example Programming Languages are:
COBOL, Basic, C, C++, VC++, VB Etc...

Monday, 19 September 2011

Introduction : Keyword-Driven Methodology

By creating our tests with a keyword-driven methodology in mind, our tests become more modular, focusing on the operations to test using both QuickTest built-in keywords and your own user-defined keywords.

Additionally, because it is possible to add objects to the object repository before they exist in an application, it is possible to begin preparing our automated keyword-driven tests even before a software build containing the new objects is available.
 
Setting up Object Repositories
 

In this step, we build one or more object repositories and ensure that all objects have clear names that follow any predetermined naming conventions defined by our organization. 

We can create object repositories using QuickTest functionality to recognize and learn the objects in your application, or we can manually define objects.
By creating and populating shared object repositories that can be associated with multiple actions, we can use the same object repository in multiple tests.
 
Add (learn) objects from our application.

We instruct QuickTest to learn the objects in our application according to filters that we define.
 

Ensure that objects in the object repository have names that are easy for application testers to recognize and that follow any established object naming guidelines.
 

This helps make both test creation and maintenance easier over time.
 Configuring QuickTest According to our Testing Needs
 

After we set up the test automation infrastructure, we need to configure QuickTest to use this infrastructure:
 

Define your global testing preferences.
 

We need to specify configuration settings that affect how we create and run tests in general—these settings are not test-specific.
 
Building our Tests 

We can create tests that are as simple or complex as needed. In general, it is best to create tests and actions that check just one or a few simple functions or complete a transaction rather than creating long tests and actions that perform several complex tasks or that perform many tasks.
 

We may perform the following tasks when creating tests and test steps:
 

Create new tests, if needed. To do so, select File > New > Test.
 

Create the required actions.
 

Associate our object repositories with the relevant actions
 This enables us to insert steps that perform operations on those objects.
 

Associate our function libraries with the relevant tests. This enables us to use our special keywords in any of the associated tests.

QTP Basic Commands

QTP Commands are available in 3 ways.

1.Menu options
2.Tool Bar options
3.Short cut keys (for Some important operations only)

File menu: Through file menu user can create, save tests, open existing tests, export tests in zip format.


Command
Function
New > Test
Creates a new test.
New > Business Component
Creates a new business component.
New > Scripted Component
Creates a new scripted component.
New > Application Area
Creates a new application area.
New > Function Library
Creates a new function library.
Open > Test
Opens an existing test.
Open > Business/Scripted Component
Opens an existing business or scripted component.
Open > Application Area
Opens an existing application area.
Open > Function Library
Opens an existing function library.
Close
Closes the active function library.
Close All Function Libraries
Closes all open function libraries.
Quality Center Connection
Opens the Quality Center Connection dialog box, enabling you to connect to a Quality Center project.
Quality Center Version Control
Provides a sub-menu of options for managing versions of QuickTest assets in Quality Center. The sub-menu is available only when you are connected to version-control enabled Quality Center project.
Save
Saves the active document.
Save As
Opens the relevant Save dialog box so you can save the open document.
Save Test with Resources
Saves a standalone copy of the current test together with its resource files.
Save All
Saves all open documents.
Enable Editing
Makes read-only function libraries editable.
Export Test to Zip File
Creates a zip file of the active document.
Import Test from Zip File
Imports a document from a zip file.
Convert to Scripted Component
Converts a business component to a scripted component.
Print
Prints the active document.
Print Preview
Displays the Keyword View as it will look when printed and enables you to modify the page setup.
Settings
Opens the Settings dialog box, enabling you to define settings for the open document. (Not relevant for function libraries)
Process Guidance Management
Opens the Process Guidance Management dialog box, enabling you to manage the list of processes that are available in QuickTest.
Associate Library '<Function Library Name>' with '<Document Name>'
Associates the active function library with the open document. (Available only from function libraries)
Recent Files
Lists the recently viewed files.
Exit
Closes the QuickTest session.


Edit Menu: It provides editing options and renaming, deleting and splitting actions.



Command
Function
Undo
Reverses the last command or deletes the last entry you typed.
Redo
Reverses the most recent operation of the Undo command.
Cut
Removes the selection from your document.
Copy
Copies the selection from your document.
Paste
Pastes the selection to your document.
Delete
Deletes the selection from your document.
Copy Documentation to Clipboard
Copies the content of the Documentation column of the Keyword View, enabling you to paste it in an external application.
Action > Split Action
Separates an action into two sibling actions or into parent-child nested actions.
Action > Rename Action
Changes the name of an action.
Action > Delete Action
Enables you to remove the selected call to the action, or delete the action and its calls from the active test.
Action > Action Properties
Enables you to specify options, parameters, and associated object repositories for a stored action.
Action > Action Call Properties
Enables you to specify the number of run iterations according to the number of rows in the Data Table, and to define the values of input parameters and the storage location of output parameters.
Step Properties > Comment Properties
Opens the Comment Properties dialog box for a comment step. Available only when the selected step is a comment.
Step Properties > Object Properties
Opens the Object Properties dialog box for a selected object. Available only when the selected step contains a test object.
Step Properties > Checkpoint Properties
Opens the relevant Checkpoint Properties dialog box for a selected object. Available only when the selected step is a checkpoint step.
Step Properties > Output Value Properties
Opens the relevant Output Value Properties dialog box for a selected object. Available only when the selected step is an output value step.
Step Properties > Report Properties
Displays the Report Properties dialog box for a report step. Available only when the selected step is a Reporter.ReportEvent step.
Find
Searches for a specified string.
Replace
Searches and replaces a specified string.
Go To
Moves the cursor to a particular line in the test.
Bookmarks
Creates bookmarks in your script for easy navigation.
Advanced > Comment Block
Comments out the current row, or selected rows.
Advanced > Uncomment Block
Removes the comment formatting from the current or selected rows.
Advanced > Indent
Indents the step according to the tab spacing defined in the Editor Options dialog box.
Advanced > Outdent
Outdents the step (reduces the indentation) according to the tab spacing defined in the Editor Options dialog box.
Advanced > Go to Function Definition
Navigates to the definition of the selected function.
Advanced > Complete Word
Completes the word when you type the beginning of a VBScript method or object.
Advanced > Argument Info
Displays the syntax of a method.
Advanced > Apply "With" to Script
Generates With statements for the action displayed in the Expert View, and enables IntelliSense within With statements.
Advanced > Remove "With" Statements
Converts any With statements in the action displayed in the Expert View to regular (single-line) VBScript statements.
Optional Step
Inserts an optional step (a step that is not required to successfully complete a run session).


View menu: Through this menu we can launch and close, active screen, Data Table, Debug viewer, information, missing resources etc.

Insert Menu: Through this menu user can inserting check points, out put values, synchronizing points.

In this menu step generator available, using this user can generate recordable and non-recordable scripts.

Through insert menu user can insert VB Script conditional and loop statements and transaction points (Start and End).

Through insert menu user can create new actions, call existing actions and copy existing actions.

Automation Menu:
This menu provides Record, Run options and Run setting options
Through this menu we can start normal recording, analog recording and Low level recording.

Through this menu we can stop recoding, running and also we run tests.


Command
Function
Record
Starts a recording session.
Run
Starts a run session from the beginning or from the line at which the session was paused.
Stop
Stops the recording or run session.
Run Current Action
Runs only the active action.
Run from Step
Starts a run session from the selected step.
Maintenance Run Mode
Starts a run session during which the Maintenance Run Mode wizard opens for steps that failed because an object was not found in the application (if applicable).
Update Run Mode
Starts a run session to update test object descriptions and other options (if applicable).
Analog Recording
Starts recording in Analog Recording mode.
Low Level Recording
Starts recording in Low Level Recording mode.
Record and Run Settings
Opens the Record and Run Settings dialog box, enabling you to define browser preferences for recording and running your test.
Process Guidance List
Lists the processes that are available for the current document type and for the currently loaded QuickTest add-ins, enabling you to open them.
Results
Enables you to view results for a test run session.


Resources Menu:

This menu provides object repository and recovery scenarios options.
Through this menu we can create /modify/delete objects information and we can associate repositories.
Through this menu we can create, modify and delete recovery scenarios.


Command
Function
Object Repository
Opens the Object Repository window, which displays a tree containing all objects in the current test or component.
Object Repository Manager
Opens the Object Repository Manager dialog box, enabling you to open and modify multiple shared object repositories.
Associate Repositories
Opens the Associate Repositories dialog box, enabling you to manage the object repository associations for the test.
Map Repository Parameters
Opens the Map Repository Parameters dialog box, enabling you to map repository parameters, as needed.
Recovery Scenario Manager
Opens the Recovery Scenario Manager dialog box.
Associated Function Libraries
Lists the function libraries associated with the active document, enabling you to open them.


Debug Menu:
This menu provides debug commands for step by step execution.
Through this menu we can insert/remove/break points.

Tools Menu:
This menu provides Tools settings option, view options and object identification configuration.
Through this menu we can set tool options as well as test pane view options.
In this menu object spy option available, through this we can get object’s information.(Properties and values)
In this menu Virtual object option available; through this option we can create virtual objects.


Command
Function
Options
Opens the Options dialog box, enabling you to modify global testing options.
View Options
Opens the Editor Options dialog box, enabling you to customize how tests and function libraries are displayed in the Expert View and Function Library windows.
Check Syntax
Checks the syntax of the active document.
Object Identification
Opens the Object Identification dialog box, enabling you to specify how QuickTest identifies a particular test object.
Object Spy
Opens the Object Spy dialog box, enabling you to view the native properties and operations of any object in an open application, as well as the test object hierarchy, identification properties, and operations of the test object that QuickTest uses to represent that object.
Web Event Recording Configuration
Opens the Web Event Recording Configuration dialog box, enabling you to specify a recording configuration level. (Relevant for tests only)
Data Driver
Opens the Data Driver dialog box, which displays the default Constants list for the action. (Relevant for tests only)
Change Active Screen
Replaces the previously recorded Active Screen with the selected Active Screen. (Relevant for tests only)
Virtual Objects > New Virtual Object
Opens the Virtual Object Wizard, enabling you to teach QuickTest to recognize an area of your application as a standard test object.
Virtual Objects > Virtual Object Manager
Opens the Virtual object Manager, enabling you to manage all of the virtual object collections defined on your computer.
Customize
Opens the Customize dialog box, which enables you to customize toolbars and menus, and create new menus.


Window Menu:
This menu provides QTP tool window style settings.

Help Menu:
This menu provides QTP help as well as VB Script help.
Through this menu we can contact technical support people and we can send feedback.
Through this menu we can check for updates and download or install directly.