When I first started working on Quick Test, I never
knew the concept of executing QTP from the DOS prompt or the command
line. Well even if I knew, I would not have understood where to
implement this. I want all my posts in this blog to have practical
applications rather than just explaining the concepts. I will try my
best to achieve this "desire" of mine. Ok...so lets continue on the
topic. I would like to give another term for this execution of QTP from
the command line - Automating the Automation. Well yes, practically thats what this whole thing is. Lets dig more to see what I mean by this.
Usually
we open QTP from our computer, record scripts and then execute them by
clicking on Run or pressing F5 in QTP. So we automated a test
scenario...didnt we ? So what is this "Automating the Automation" ??
Well, we did automate a test scenario but did you ever think that you
still had to have a manual intervention to open the test case and
actually click on the Run button !!!!! Aha....well we are going to
automate even this step.... and here is where executing QTP from command
line is going to help.
Well its true that we
indeed saved a lot of time by automating the tests using QTP and most
companies are OK with the time that you spend on opening the test from
QTP and executing it. But there are cases where you do not even have
that time to wait for manual intervention to execute the automated
tests. I am sure you are thinking now as to what is that case....well
let me explain a situation....
Lets say in the QA phase the below mentioned steps need to be executed in a particular order as part of the testing
Step 1: Execute some SQL queries or SQL scripts
Step 2: Take a backup of the database
Step 3: Execute the automated scripts of QTP
Step 4: Take a backup of the database
Step 5: Execute some more SQL scripts
Step 6: Execute some more automated scripts of QTP
Now
lets say that you want to execute these during the weekend so that by
the time you are back to office on Monday (with the Monday blues ;-) you
are ready with the results. Lets say we are using a scheduler like
Windows Scheduler or Autosys to schedule (automatically execute at a particular time)
these jobs (thats what you usually call them). As of now, just
understand that there are ways the SQL queries and database backups can
be submitted as a job to the scheduler. Now lets learn how do we execute
the QTP scripts. There are two scripts that are going to be involved
here
1. The script that you developed using QTP
2. A VB script (.vbs file) that is going to call the script developed in QTP
And
note this... a lot of people get confused with the VB script file ...
understand that the VB script file is not going to be executed in QTP,
its going to be executed at the command prompt. The reason for the
confusion is that if you use the code that we have in the VB script file
in a QTP script, you will still see that you will be able to open the
test script and execute it...but remember...thats not our purpose !!!
Once we have these two ready, we just need to go to the DOS prompt and execute the following
C:\cscript "VB script file"
Ok..so what is cscript !!!
With
Cscript.exe, you can run scripts by typing the name of a script file at
the command prompt.When you start a script from your desktop or from
the command prompt, the script host reads and passes the specified
script file contents to the registered script engine. The script engine
uses file extensions (that is, .vbs for VBScript and .js for JScript) to
identify the script.
I
am sure you must be thinking that so dont we manually need to type this
command at the dos prompt. No !!! Thats where a scheduler comes into
the picture. You pass this command as a job to the scheduler. So
depending on the time when this job is scheduled, your QTP script is
automatically going to execute. Voila !!!!
Want to go deeper....Ok...lets go for it..
First we need a QTP script. So lets develop one and call it SimpleLoop and here are the contents
Open QTP and enter the below in a new test
'============SimpleLoop Script=============
Option Explicit
Dim curIteration
For curIteration = 1 to 5
Wait(1)
Next
'End of SimpleLoop Script
Next what we need is a VB script file that is going to call the QTP script developed above. Lets call that qtpLaunch.vbs and here are its contents
Open Notepad and type in the following and save the file as "qtpLaunch.vbs"
'============qtpLaunch.vbs==============
Option Explicit
Dim qtApp, Test_Path
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
Test_Path = "C:\SimpleLoop"
qtApp.Open Test_Path,True
Dim qtTest
Set qtTest = qtApp.Test
qtTest.Run
qtTest.Close
qtApp.Quit
Set qtTest = Nothing
Set qtApp = Nothing
'End of qtpLaunch.vbs
We
are ready. So remember what we do next, right !!! Go to the command
prompt and just enter this... You dont need to have QTP open..it will
open by itself...
C:\cscript qtpLaunch.vbs
How
did it go !!!! I am sure it went well.... So as I mentioned above, if
you pass the above command to a scheduler, you have actually "Automated the Automation".
This is just the start....there is much more to this. As we go further,
we will learn that we can connect to Quality Center and execute the
tests from there using Open Test Architecture or OTA. This is also
developed using VB or VB script. I will cover that in another post. A request to my readers : This particular page is viewed numerous times in a day by several people.
Way 2 :-**********************************
'RunThisTest
'by Michael Innes
'November 2012
testResourcePath = "C:\Test Logs and Results\"
'Getting the test path
Dim objArgs
Set objArgs = wscript.Arguments
testPath = objArgs(0)
'Determining that the test does exist
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
DoesFolderExist = objFSO.FolderExists(testPath)
Set objFSO = Nothing
If DoesFolderExist Then
Dim qtApp 'Declare the Application object variable
Dim qtTest 'Declare a Test object variable
Set qtApp = CreateObject("QuickTest.Application") 'Create the Application object
qtApp.Launch 'Start QuickTest
qtApp.Visible = True 'Make the QuickTest application visible
qtApp.Open testPath, False 'Open the test in read-only mode
Set qtTest = qtApp.Test
'Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
'qtResultsOpt.ResultsLocation = testResourcePath ' Specify the location to save the test results.
'qtTest.Run qtResultsOpt,True 'Run the test and wait until end of the test run
qtTest.Run 'Run the test
qtTest.Close 'Close the test
qtApp.Quit
Else
'Couldn't find the test folder. That's bad. Guess we'll have to report on how we couldn't find the test.
'Insert reporting mechanism here.
End If
Note :- To use the code above, execute a command like this: cscript.exe "C:\RunThisTest.vbs" "L:\Test Path\The Test Itself\"
No comments:
Post a Comment