How To Run QTP/UFT Script Using Batch File

While performing automation testing and working with QTP/UFT tool, I came across a situation where I needed to run QTP/UFT script using batch file. This means, it is not required to open QTP/UFT tool manually at all, we just needs to run batch file and it will execute UFT script automatically. So the question is which UFT driver file it will run? Yes we are not going to open QTP/UFT tool, but using batch file we are going to open the UFT driver file and allow to run UFT script automatically.
There are few information mentioned below. These information will help to understand better before we start doing the setup and run UFT script using batch file.
What is batch or .bat file?
Batch file or .bat file is nothing but it is an executable file which executes the code specified in the text file. When you double click on it, UFT script starts executing which is invoked by batch file.
Why to use .bat file to run QTP/UFT script?
It depends on your requirements. Here batch file is used to run specified script and invoke a DOS command just by double clicking on it. Another use of batch file is to execute QTP/UFT script from remote system. Also you can call the batch file in scheduler while scheduling any UFT script to run at specified time.
How to setup and run QTP script using batch or .bat file?
Please follow the steps given below to setup a batch file to executed QTP/UFT script automatically-
- Open notepad and save as .bat file.
- Open another notepad and save file as .vbs file.
- Right click on MyTest.bat file and click on Edit.
- Copy the code mentioned below and paste in the file. Replace the cscript.exe path.
cd c:\windows\syswow64 cscript.exe "Add path of vbs file" exit
Example:
cd c:\windows\syswow64 cscript.exe "C:\Users\Naveen\Desktop\MyVbsTest.vbs" exit
- Close the file.
- Right click on MyVbsTest.vbs click on Edit.
- Make sure UFT is installed in your system and test has been created.
- Copy the code mentioned below and paste in the file. Replace the testPath with your QTP/UFT driver path.
testPath = "Replace with your QTP/UFT driver path"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
DoesFolderExist = objFSO.FolderExists(testPath)
Set objFSO = Nothing
If DoesFolderExist Then
Dim qtApp
Dim qtTest
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
qtApp.Open testPath, False
Set qtTest = qtApp.Test
qtTest.Run
qtTest.Close
qtApp.Quit
Else
End If
Example: testPath = "C:\Users\Naveen\Documents\Unified Functional Testing\NaveenUFTTest" Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") DoesFolderExist = objFSO.FolderExists(testPath) Set objFSO = Nothing If DoesFolderExist Then Dim qtApp Dim qtTest Set qtApp = CreateObject("QuickTest.Application") qtApp.Launch qtApp.Visible = True qtApp.Open testPath, False Set qtTest = qtApp.Test qtTest.Run qtTest.Close qtApp.Quit Else End If
- Save and close the file.
You are done with the setup!!!
How to run script?
Double click on MyTest.bat file. Once you double click on MyTest.bat file, QTP/UFT script will start executing automatically. A command prompt window will open which leads to open your QTP/UFT script.
Leave a Reply