Sunday, February 17, 2013

Relative Path using VBS


Dim ObjQTP
Dim ObjTest
Dim ObjResult

Set ObjQTP=CreateObject("QuickTest.Application") 'Create QTP Object
ObjQTP.Visible=True
If ObjQTP.Launched <> True Then
ObjQTP.Launch 'Launch QTP
End If

ObjQTP.Options.Run.CaptureForTestResults = "OnError" 'Tool -> Options -> Run Tab
ObjQTP.Options.Run.RunMode= "Fast"
ObjQTP.Options.Run.ViewResults = False


Set WshShell = WScript.CreateObject("WScript.Shell")
MsgBox WshShell.CurrentDirectory&"\"&"Test1"
ObjQTP.Open WshShell.CurrentDirectory&"\"&"Test1", True

Thursday, February 14, 2013

Understanding SystemUtil.Run syntax with examples


Understanding SystemUtil.Run syntax with examples


SystemUtil.Run is an inbuilt QTP command that lets you run a file or an application. Mostly SystemUtil.Run command is used to open browser with a specific URL. But there is much more that can be done with it. Here in this article, you’ll see various examples on how to use SystemUtil.Run for different purposes. Let’s first go through the syntax of SystemUtil.Run method -
SystemUtil.Run file, [params], [dir], [op], [mode]
Argument
Description
file
String Value [Mandatory]. The name of the file you want to run.
params
String Value [Optional]. If the specified file argument is an executable file, use the params argument to specify any parameters to be passed to the application.
dir
String Value [Optional]. The default directory of the application or file.
op
String Value [Optional]. The action to be performed – open, edit, explore, find & print.If this argument is blank (“”), the open operation is performed.
Operation
Description
open
Opens the file specified by the file parameter. The file can be an executable file, a document file, or a folder. Non-executable files are open in the associated application.
edit
Launches an editor and opens the document for editing. If the file argument does not specify an editable document file, the statement fails.
explore
Explores the folder specified by the file argument.
find
Initiates a search starting from the specified folder path.
print
Prints the document file specified by the file argument. If the specified file is not a printable document file, the statement fails.
mode
Integer Value [Optional]. Specifies how the application is displayed when it opens. Default : 1
Mode
Description
0
Hides the window and activates another window.
1
Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. Specify this flag when displaying the window for the first time.
2
Activates the window and displays it as a minimized window.
3
Activates the window and displays it as a maximized window.
4
Displays the window in its most recent size and position. The active window remains active.
5
Activates the window and displays it in its current size and position.
6
Minimizes the specified window and activates the next top-level window in the Z order.
7
Displays the window as a minimized window. The active window remains active.
8
Displays the window in its current state. The active window remains active.
9
Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. Specify this flag when restoring a minimized window.
10
Sets the show-state based on the state of the program that started the application.
Examples : SystemUtil.Run
1. Using file argument:
1
2
3
4
SystemUtil.Run "iexplore.exe" 'opens internet explorer with default page
SystemUtil.Run "notepad.exe" 'opens blank notepad window
SystemUtil.Run "notepad" 'opens blank notepad window
SystemUtil.Run "D:\AutomationRepository.txt" 'since this is a non executable file, the file opens in the associated application i.e. notepad in this case
2. Using params argument
1
2
3
'If 'file' argument is an executable file, then 'params' can be used to specify the arguments to the application
SystemUtil.Run "iexplore.exe", "http://www.automationrepository.com" 'opens Automation Repository in Internet Explorer browser
SystemUtil.Run "notepad.exe", "D:\SomeTextFile.txt" 'opens the text file in notepad
3. Using dir argument
1
2
'dir argument can be used to specify the default location of the application
SystemUtil.Run "iexplore.exe", "", "C:\Program Files\Internet Explorer\"
4. Using op argument
1
2
3
4
5
6
7
SystemUtil.Run "notepad.exe", "D:\AutomationRepository.txt", "", "open" 'open a file
SystemUtil.Run "D:\AutomationRepository.txt", "", "", "edit" 'open file in edit mode
'Explore the folder specified by 'file' argument. Refer figure-1
SystemUtil.Run "My Computer", "", "", "explore"
'Initiate a search starting from the specified folder path. Refer figure-2
SystemUtil.Run "D:\", "", "", "find"
SystemUtil.Run "D:\AutomationRepository.txt", "", "", "print" 'prints a document specified by 'file' argument
5.Using mode argument
1
2
3
SystemUtil.Run "notepad.exe", "D:\AutomationRepository.txt", "", "", 0 'the file is opened in hidden mode
SystemUtil.Run "notepad.exe", "D:\AutomationRepository.txt", "", "", 1 'activates and displays the window
SystemUtil.Run "notepad.exe", "D:\AutomationRepository.txt", "", "", 2 'opens the window in minimized state

Saturday, February 9, 2013


Environment Variables in QTP

        Environment variables are similar to Global variables to store values. Environment variables can be accessed at any place in the script and the values will be the same. Also, we can modify the values at any place and the modified values will be retrieved on further use.
Environment variables are very useful when we want to share a variable across actions in a test.
There are two types of Environment variables.
1. Built-In
2. User Defined

1. Built-In:
     There are some predefined Environment variables provided by QTP. We can use these environment variables without having assigned any values manually. Built-In environment variables contains values such as test path, OS version, Current Action,Current Action Iteration, User name etc.,

For Ex: If you want to get the path where the test is saved then you can use "TestDir" Environment variable.

How to Read environment variable from QTP Script?
Syntax: Environment.value("Variable Name")
Msgbox Environment.value("OS")

This will print the Operating System of the machine on which QTP is installed.

Below are some other Built-In Environment Varaibles:
1. Action Iteration: Indicates which action iteration is currently running. The value will be displayed or valid only during the test run.
2. Action Name: Displays or indicates which action is currently running. The current value is valid only during test run.
3. ControllerHostName: The name of the computer that serves as the controller. Used when Load runner is running. The current value is valid only during test run under Load.
4. GroupName: This is w.r.t. Load Runner. It is the scenario identification number. The current value is valid only during test run under Load.
5. LocalHostname: Gives the Local Host Name on which QTP is Installed. The current value displayed is WINDOWS XP.
6. OS: Displays the operating system on which QTP is installed. The current value os Microsoft Windows XP Work Station.
7. OS Version: Displays the version of the Operating System.
8. ProductDir: Displays the folder path where the product (QTP) is installed.
9. ProductName: Gives the name of the Product.
10. ProductVersion: Gives the product version.
11. ResultDir: Displays the folder path where the current test results are saved.
12. SceanrioId: This is w.r.t. LoadRunner. Displays the scenario identification number.
13. SystemTempDir: Displays the system temporary directory.
14. TestDir: This is the path of the folder in which the test is located.
15. TestIteration: Indicates which test iteration is currently running.
16. TestName: Displays the name of the test.
17. UpdatingActiveScreen: Indicates whether the active screen images and values are being updated.
18. UpdatingCheckPoints: Indicates whether the checkpoint properties are being updated.
19. UpdatingToDescriptions: Indicates whether test object descriptions are being updated.
20. UserName: Displays the windows login user name.
21. VUserId: This is w.r.t. Load Runner. Indicates the virtual user identification number.


2. User Defined
     These are all the environment variables defined by the user. User can store the run time values or any other own values for further access in the script.
     There are two types to User Defined Environment variables.
      a. Internal
      b. External
a. Internal:
     Internal Environment variables are the one created by the user with in the test. These variables can be used only with in the test in which it is created. Internal environment variables can be created in two ways.
   Method 1:
         1. Go to File->setting->environment tab. 
         2. Select variable type as User defined
         3. Click on “+” sign.
         4. Add name for e.g qtp
         5. Add value for e.g. http://testeverything-qtp.blogspot.com
         Click on apply and ok. 

  Method 2:
  Environment.value("<variable name>") = <Value>
  Environment.value("qtp") = "http://testeverything-qtp.blogspot.com"

These Environment variables also can be accessed as below
Msgbox Environment.value("qtp")
"http://testeverything-qtp.blogspot.com"  will be displayed in the msgbox.

b. External:
External environment variables that you pre-defined in the active external
environment variables file.These external file contains the name & value pair in XML format. You can create as many files as you want and select an appropriate file for each test. Note that external environment 
variable values are designated as read-only within the test.

To create an external environment variables file:
 1 Open any text editor.
 2 Type [Environment] on the first line.
 3 Type one variable-value pair on each line in the format—variable=value.
 4 Save the file in a location that is accessible from the QuickTest machine. The 
file must be a text file, but you can use any file name extension (usually, it will be saved in XML Format).
For example:
[Environment]
MyParam1=10
MyParam2=20
MyParam3=http://testeverything-qtp.blogspot.com

To select the active external environment-variables file:
 1 Choose Test > Settings to open the Test Settings dialog box.
 2 Click the Environment tab.
 3 Select the Load variables and values from external file (reloaded each test run) check box.
 4 Use the browse button or enter the full path of the external environment-variables file you want to use with your test.

Open QTP and Connect to Quality Center and run QC script:


Dim qtApp ' Declare the Application object variable

'Create the QTP Application object
Set qtApp = CreateObject("QuickTest.Application") 

'If QTP is notopen then open it
If  qtApp.launched <> True then 
qtApp.Launch 
End If 

'Make the QuickTest application visible
qtApp.Visible = True
If Not qtApp.TDConnection.IsConnected Then

' Make changes in a test on Quality Center with version control
qtApp.TDConnection.Connect "QC URL","DOMAIN Name","Project Name","User Name","Password",False

'QC URL = QC Server path
'DOMAIN Name = Domain name that contains QC project
'Project Name =Project Name in QC you want to connect to
'Username = Username to connect to Project
'Password = Password to connect to project
'False or True = Whether ‘password is entered in encrypted or normal. 
'Value is True for encrypted and FALSE for normal

'Example : qtApp.TDConnection.Connect
'"http://200.168.1.1:8080/qcbin","Default","proj1","qtpworld","qtp",false

End If

'Make Sure about your script path  and script name in QC
qtApp.Open "[QualityCenter] Subject\QCScriptPath\ScriptName", False
qtApp.Test.Run
qtApp.TDConnection.Disconnect

'Close QTP
qtApp.quit

'Release Object
Set qtApp = Nothing

4 Different Ways to Associate Function Libraries to your QTP Scripts

1. Using AOM (Automation Object Model)
QTP AOM is a mechanism using which you can control various QTP operations using another application. Using QTP Automation Object Model, you can write a code which would open a QTP test and associate a function library to that test.

'Open a test and associate a function library to the test
objQTP.Open "C:\Automation\SampleTest", False, False
Set objLib = objQTP.Test.Settings.Resources.Libraries
'If the library is not already associated with the test case, associate it..
If objLib.Find("C:\SampleFunctionLibrary.vbs") = -1 Then ' If library is not already added
objLib.Add "C:\SampleFunctionLibrary.vbs", 1 ' Associate the library to the test case
End

2. Using ExecuteFile Method

ExecuteFile statement executes all the VBScript statements in a specified file. After the file has been executed, all the functions, subroutines and other elements from the file are available to the action as global entities. Simply put, once the file is executed, its functions can be used by the action. You can use the below mentioned logic to use ExecuteFile method to associate function libraries to your script.

'Action begins
ExecuteFile "C:\YourFunctionLibrary.vbs"
'Other logic for your action would come here
'.....

3. Using LoadFunctionLibrary Method

LoadFunctionLibrary, a new method introduced in QTP 11 allows you to load a function library when a step runs. You can load multiple function libraries from a single line by using a comma delimiter.

'Some code from the action
'.....
LoadFunctionLibrary "C:\YourFunctionLibrary_1.vbs", "C:\YourFunctionLibrary_2.vbs"
'Other logic for your action would come here
'.....


4. Using ‘File > Settings > Resources > Associate Function Library’ option from the Menu bar

This is one of the most common methods used to associate a function library to a test case. To use this method, select File > Settings option from the Menu bar. This will display the ‘Test Settings’ window. Click on Resources from the left hand side pane. From the right hand side pane, click on the ‘+’ button and select the function library that needs to be associated with the test case.
LoadAndRunAction Call one test from another test >>>>>>>>



LoadAndRunAction test_path, action_in_test
VBS RUN File   >>>>>>>>>>





Dim ObjQTP
Dim ObjTest
Dim ObjResult

Set ObjQTP=CreateObject("QuickTest.Application") 'Create QTP Object

ObjQTP.Visible=True 
If ObjQTP.Launched <> True Then 
ObjQTP.Launch 'Launch QTP
End If

ObjQTP.Options.Run.CaptureForTestResults = "OnError" 'Tool -> Options -> Run Tab
ObjQTP.Options.Run.RunMode= "Fast"
ObjQTP.Options.Run.ViewResult = False

ObjQTP.Open "", True 'Open the test in read only mode

Set ObjTest=ObjQTP.Test

ObjTest.Settings.Run.OnError = "Next Step" 'File -> Settings -> Run

Set ObjResult = CreateObject("QuickTest.RunResultsOptions") 'Create the Run Results option object
ObjResult.ResultLocation = "" 'Set the result Location

ObjTest.Run ObjResult 'Run the test

'''''''''''''''' MsgBox ObjTest.LastRunResults.Status 'Run the test

ObjTest.Close 'Close the test

Set ObjResult = Nothing
Set ObjTest = Nothing
Set ObjQTP = Nothing

Thursday, February 7, 2013

"RunAction <ActionName>, (Iteration)"

Here, "<ActionName>" is the Action you want to call and
the "(Iteration)" is optional where you can define number
of iterations you want to execute.

Eg:
1. RunAction Action2, OneIteration (For single Iteration)
2. RunAction Action3, AllIterations (For All Iterations)
3. RunAction Action4, 1-3 (For iterations from 1 to 3)


Note: To Call an "Action" multiple times or to Call
an "Action" from Other Tests, make sure the "Action" is
marked as a "Reusable Action".

Wednesday, February 6, 2013

Run file

Dim qtApp
set qtApp=createobject("QuickTest.Application")
qtApp.Launch
qtApp.Visible=True
qtApp.Open "C:\Users\Sathish\Desktop\Test1", False, False


Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = "C:\Users\Sathish\Desktop\result"
Set qtTest = qtApp.Test
qtTest.Run qtResultsOpt

Datatable.ImportSheet  "C:\Users\Sathish\Desktop\drivesheet.xls", 1, Global
'wait(10)
For i=1 to DataTable.GetRowCount
If DataTable.Value("Flag")= "Yes" Then
MsgBox "Row :"&i
End If
Next


Function Call1()
RepositoriesCollection.Add("C:\Users\Sathish\Desktop\or1.tsr")
Dialog("Login").WinEdit("Agent Name:").Set "hkjhkhks"
Dialog("Login").WinEdit("Agent Name:").Type  micTab
Dialog("Login").WinEdit("Password:").SetSecure "5112969c4489964da9cd8c09bbcea478fb79b262"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "111116"
Window("Flight Reservation").ActiveX("MaskEdBox").Type  micTab
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Paris"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Activate "13534   FRA   08:00 AM   PAR   11:30 AM   SR     $159.90"
Window("Flight Reservation").WinEdit("Name:").Set "kshfkshkshk"
Window("Flight Reservation").WinButton("Insert Order").Click

End Function



'Open QTP
Set objQTP = CreateObject("QuickTest.Application")
objQTP
.Launch
objQTP
.Visible = True
'Open a test and associate a function library to the test
objQTP
.Open "C:\Automation\SampleTest", False, False
Set objLib = objQTP.Test.Settings.Resources.Libraries
'If the library is not already associated with the test case, associate it..
If objLib.Find("C:\SampleFunctionLibrary.vbs") = -1 Then ' If library is not already added
  objLib
.Add "C:\SampleFunctionLibrary.vbs", 1 ' Associate the library to the test case
End


LoadFunctionLibrary "C:\YourFunctionLibrary_1.vbs" 'Associate a single function library
LoadFunctionLibrary
"C:\FuncLib_1.vbs", "C:\FuncLib_2.vbs" 'Associate more than 1

Sunday, February 3, 2013

Exit Statement

1. ExitAction
2. ExitOneIteration
3. ExitTest


• ExitAction - Exits the current action, regardless of its iteration attributes.
• ExitActionIteration - Exits the current iteration of the action.
• ExitRun - Exits the test, regardless of its iteration attributes.
• ExitGlobalIteration - Exits the current global iteration.


  1. WSH >>>>>>>>>







  1. ***************************************************  
  2. 'How to popup a message with some wait  
  3. '***************************************************  
  4. Set WshShell = CreateObject( "WScript.Shell")  
  5. WshShell.Popup "message body", 5, "Title"  
  6.    
  7. '***************************************************  
  8. 'How to get the current UserName of logged in  
  9. '***************************************************  
  10. Set WshNetwork = CreateObject( "WScript.Network")  
  11. userName = WshNetwork.username  
  12. Set WshShell = CreateObject( "WScript.Shell")  
  13. WshShell.Popup userName , 50, " userName "  
  14.    
  15. '***************************************************  
  16. 'How to send keyboard inputs to application  
  17. '***************************************************  
  18. set WshShell = CreateObject( "WScript.Shell")  
  19. WshShell.SendKeys "QuickTest Professional"  
  20.    
  21. '***************************************************  
  22. 'How to get the value of the system environment variable  
  23. '***************************************************  
  24. Set WshShell = CreateObject( "WScript.Shell")  
  25. Set WshSysEnv = WshShell.Environment("SYSTEM" )  
  26. WScript.Echo WshSysEnv("NUMBER_ OF_PROCESSORS" )  
  27.    
  28. '***************************************************  
  29. 'How to write and read registry  
  30. '***************************************************  
  31. Set WshShell = CreateObject( "WScript.Shell")  
  32. WshShell.RegWrite "HKCU\Software\ ACME\FortuneTell er\", 1, "REG_BINARY"  
  33. WshShell.RegWrite "HKCU\Software\ ACME\FortuneTell er\MindReader" , "Goocher!""REG_SZ"  
  34. bKey = WshShell.RegRead( "HKCU\Software\ ACME\FortuneTell er\")  
  35. msgbox WshShell.RegRead( "HKCU\Software\ ACME\FortuneTell er\MindReader" )  
  36.    
  37. '***************************************************  
  38. 'How to execute DOS commands  
  39. '***************************************************  
  40. Set oShell = CreateObject ("WSCript.shell" )  
  41. oShell.run "cmd /K CD C:\ & Dir"  
  42. '*************************************************** 



Some time we requre keyboard event in Automation so we can use Send key method
rem **********************************************
rem create shell object
set WshShell = CreateObject(“WScript.Shell”)
SystemUtil.Run “C:\Program Files\HP\QuickTest xcxProfessional\samples\flight\app\flight4a.exe”,”C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe”,”",”"
rem first click in the text box then use send key method
Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Click
rem enter rajiv in agent name text box
WshShell.SendKeys “rajiv”
rem click Password Edit box
Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Click
rem enter check in Password text box
WshShell.SendKeys “check “
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
rem press escape using send key method
WshShell.SendKeys “{ESC}”
WshShell.SendKeys “{ESC}”
rem **********************************************************
Use the SendKeys method to send keystrokes to applications that have no automation interface. Most keyboard characters are represented by a single keystroke. Some keyboard characters are made up of combinations of keystrokes (CTRL+SHIFT+HOME, for example). To send a single keyboard character, send the character itself as the string argument. For example, to send the letter x, send the string argument “x”.
Note To send a space, send the string ” “.
You can use SendKeys to send more than one keystroke at a time. To do this, create a compound string argument that represents a sequence of keystrokes by appending each keystroke in the sequence to the one before it. For example, to send the keystrokes a, b, and c, you would send the string argument “abc”. The SendKeys method uses some characters as modifiers of characters (instead of using their face-values). This set of special characters consists of parentheses, brackets, braces, and the:
  • plus sign       ”+”,
  • caret             ”^”,
  • percent sign “%”,
  • and tilde       ”~”
Send these characters by enclosing them within braces “{}”. For example, to send the plus sign, send the string argument “{+}”. Brackets “[ ]” have no special meaning when used with SendKeys, but you must enclose them within braces to accommodate applications that do give them a special meaning (for dynamic data exchange (DDE) for example).
  • To send bracket characters, send the string argument “{[}" for the left bracket and "{]}” for the right one.
  • To send brace characters, send the string argument “{{}” for the left brace and “{}}” for the right one.
Some keystrokes do not generate characters (such as ENTER and TAB). Some keystrokes represent actions (such as BACKSPACE and BREAK). To send these kinds of keystrokes, send the arguments shown in the following table:
KeyArgument
BACKSPACE{BACKSPACE}, {BS}, or {BKSP}
BREAK{BREAK}
CAPS LOCK{CAPSLOCK}
DEL or DELETE{DELETE} or {DEL}
DOWN ARROW{DOWN}
END{END}
ENTER{ENTER} or ~
ESC{ESC}
HELP{HELP}
HOME{HOME}
INS or INSERT{INSERT} or {INS}
LEFT ARROW{LEFT}
NUM LOCK{NUMLOCK}
PAGE DOWN{PGDN}
PAGE UP{PGUP}
PRINT SCREEN{PRTSC}
RIGHT ARROW{RIGHT}
SCROLL LOCK{SCROLLLOCK}
TAB{TAB}
UP ARROW{UP}
F1{F1}
F2{F2}
F3{F3}
F4{F4}
F5{F5}
F6{F6}
F7{F7}
F8{F8}
F9{F9}
F10{F10}
F11{F11}
F12{F12}
F13{F13}
F14{F14}
F15{F15}
F16{F16}
To send keyboard characters that are comprised of a regular keystroke in combination with a SHIFT, CTRL, or ALT, create a compound string argument that represents the keystroke combination. You do this by preceding the regular keystroke with one or more of the following special characters:
KeySpecial Character
SHIFT+
CTRL^
ALT%
Note When used this way, these special characters are not enclosed within a set of braces.
To specify that a combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, create a compound string argument with the modified keystrokes enclosed in parentheses. For example, to send the keystroke combination that specifies that the SHIFT key is held down while:
  • e and c are pressed, send the string argument “+(ec)”.
  • e is pressed, followed by a lone c (with no SHIFT), send the string argument “+ec”.
You can use the SendKeys method to send a pattern of keystrokes that consists of a single keystroke pressed several times in a row. To do this, create a compound string argument that specifies the keystroke you want to repeat, followed by the number of times you want it repeated. You do this using a compound string argument of the form {keystroke number}. For example, to send the letter “x” ten times, you would send the string argument “{x 10}”. Be sure to include a space between keystroke and number.
Note The only keystroke pattern you can send is the kind that is comprised of a single keystroke pressed several times. For example, you can send “x” ten times, but you cannot do the same for “Ctrl+x”.
Note You cannot send the PRINT SCREEN key {PRTSC} to an application.