Execute QTP from Command Line (DOS prompt) - But Why ?

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.

I dont want to have any sort of copyright or copyleft in this site but if you are reusing this code in any other site, I would appreciate you to provide this page as a link so that others get a more descriptive explanation of this concept. Your suggestion and comments are always welcome. Cheers !!!
As always,
Your friend in need,

George, Reju

Read Users' Comments (4)

4 Response to "Execute QTP from Command Line (DOS prompt) - But Why ?"

  1. Anonymous Says:
    October 25, 2009 at 8:22 PM

    This posting is really helpful. Concept is very clear and easy to understand.

    Thanks,
    Bindu

  2. REJU GEORGE says:
    October 26, 2009 at 5:57 AM

    Good to know. Keep tuned in. There are more to come.

    - George, Reju -

  3. Anonymous Says:
    December 29, 2009 at 3:36 AM

    Great Post Reju!!!!....

    Thank you,
    Anil

  4. REJU GEORGE says:
    December 29, 2009 at 6:34 AM

    Thank you Anil. Will keep adding more.

    - George, Reju -

Post a Comment

Visitors

Website Counter