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)

How to download/upload a file from QTP using FTP ?

Recently I was faced with the task of developing an automated script to compare some reports in Excel. Well I do not want to go into the file comparison part today but I would like to mention the part in my automation where I had to ftp a file from my local machine to a ftp server and download a baseline file from the server to my local machine. My first instinct to automate the part of ftp-ing a file was to use the SystemUtil.Run "cmd" statement and then use a series of Window("ftp").Type commands to execute the statements and put all this in a function. But somehow I didnt like the look of the program. Initially I thought this was OK but then later on I realised that whenever I needed another script to call this function, I even had to have the Object Repository loaded as the Window("ftp") object needed to be present in the calling script. This was not an ideal solution. Thats when I thought that I had to do it without the usage of any objects. OK....so how do we accomplish this.
The command "ftp" has an option "-s" where we input a file which contains a set of ftp commands. So with just one ftp command, we would be able to execute a series of commands. Let me try to make it more clear with a program..

'#################################################################
'# # Function Name: FTPDownload
'# # Parameters: ftp server name, Directory path, File to be downloaded
'#################################################################
Option Explicit
Public Function FTPDownload(serverName, directoryPath, fileName)


Const userName = "ftpuser"
Const password = "ftppassword"
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

Dim fso, fsh
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsh = CreateObject("WScript.Shell") 'the fsh object will run the command
directoryPath = Trim(directoryPath)

fileName = Trim(fileName)

Dim fScript, fTemp, fTempFile, fResult
'build a script file to store all the ftp commands as we will be executing the script file

fScript = fScript & "USER " & userName & vbCRLF
fScript = fScript & password & vbCRLF
fScript = fScript & "lcd " &directoryPath & vbCRLF
fScript = fScript & "cd /etc" & vbCRLF
fScript = fScript & "binary" & vbCRLF
fScript = fScript & "prompt n" & vbCRLF
fScript = fScript & "mget " & Chr(34) & fileName & Chr(34) & vbCRLF
fScript = fScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF
fTemp = fsh.ExpandEnvironmentStrings("%TEMP%")

fTempFile = fTemp & "\" & fso.GetTempName 'returns a random name
fResult = fTemp & "\" & fso.GetTempName

Dim fFTPScript
'Write the input script file for the ftp command to a temporary file.

Set fFTPScript = fso.CreateTextFile(fTempFile, True)
fFTPScript.WriteLine(fScript)
fFTPScript.Close
Set fFTPScript = Nothing
fsh.Run "%comspec% /c FTP -n -s:" & fTempFile & " " & serverName & _ " > " & fResult, 0, TRUE


Dim fFTPResults, sResults
'Check results of transfer.
Set fFTPResults = fso.OpenTextFile(fResult, ForReading, _ FailIfNotExist, OpenAsDefault) sResults = fFTPResults.ReadAll

fFTPResults.Close fso.DeleteFile(fTempFile)
fso.DeleteFile (fResult)
If InStr(sResults, "226 Transfer complete.") > 0 Then 'checks success of transfer
FTPDownload = True 'Function returning True if transfer is successful
ElseIf InStr(sResults, "No file") > 0 Then
FTPDownload = "Error: File Not Found"
ElseIf InStr(sResults, "cannot log in.") > 0 Then
FTPDownload = "Error: Login Failed."
Else FTPDownload = "Error: Unknown."
End If
Set fso = Nothing

Set fsh = Nothing
End Function

Let me explain the script now.... I have created a function here as you can call this function whenever you want to use it.
The first parameter is the name of the server like ftpservername.companyname.com.
The second parameter is the directory path where the file to be downloaded needs to be placed locally. In this example we are trying to copy a file passed through the parameter "fileName" under the /etc directory. Now...what is vbCRLF ? This is a Carriage return-Line Feed conbination. In simple words, its equivalent to typing an "Enter" on the keyboard.
You can see that while using the "mget" command, I am using Chr(34). Chr(34) is the equivalent of quotes("). If your filename contains spaces, you would need to include the filename in quotes. Well...why didnt we just use the " then ? Thats because if you use " instead of Chr(34), the VB interpreter will think that you are going to have one more quote and it would take whatever in between as a string constant and this will result in a syntax error.
Also we are putting the script file and the log file in a temporary location. The ftp file transfer is successful when we see the message "226 Transfer complete". Thats why we are using the Instr() function.
Then we use fsh.Run to run the ftp command and pass the script as an input.
Finally set the objects to "Nothing" to avoid memory leaks.

Well to upload your file, just use "mput" instead of "mget"

You would also have observed the usage of Option Explicit on the top. Its a best practice to always have this statement at the beginning of all your scripts so that you dont make a mistake of mistyping a variable name.

So go ahead with your ftp function and tune it to your needs...
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 (3)

Action Parameters...Where to use ?

Most of you who have worked on QTP would have heard about action parameters. Several times it so happens that we are aware of a lot of functionalities in QTP but dont know exactly where to use them. For eg: you must have heard RegEx, Parameters, Function Libraries, Output Value etc..etc... you might even know how these work but we just might not know where to use them. The key to a successful QTP developer is to know where to use the right feature. Do I use Text check point or do I use Text Output Value. Should I use data table or should I use Action parameters ? Should I use regular expression or should I use descriptive programming ? There would always be multiple approaches to one problem. Do you know the right one ?
What are action parameters ? Before I explain how this works, let me tell a situation where we need to use this. Lets say you have written a script GetOrderNumber which has say an Action named OrderNumber. In this script, we place an order and get the order number. Thats all that this script does. Now, lets say there are other scripts like CancelOrder, UpdateOrder which need this order number. Since these are all different actions, we need a mechanism to pass a value from one action to another i.e. I want to pass the value of Order Number from the script GetOrderName and use it in the script CancelOrder. Here is where Action parameters help.
In GetOrderName, lets say the order number is stored in a variable called orderNumber. Below are the steps that we need to do now
====== GetOrderNumber script ======
1.Right click on OrderNumber(name of the action) and click Action Properties..
2.Click on parameters tab and set the Output parameter names as say orderNoOut.
So now, the GetOrderNumber script is set to store a value in the above variable for another calling action(like CancelOrder) to use.
3.In Action OrderNumber, you must enter
Parameter("orderNoOut")= orderNumber
Now the output parameter variable is set
4.Lets make this action reusable. Now you are ready to use these in another action in another script
====== CancelOrder Script ======
So now all you need to do in this script is to call the action and pass a variable
1.Insert call to existing action and call GetOrderNumber. Your statement would be
RunAction "OrderNumber [GetOrderNumber]",oneIteration,inputOrderNum
So now inputOrderNum will have the value of the output parameter specified in the action GetOrderNumber
2.Now you have the value of the order number which was passed from the script GetOrdernumber in the variable inputOrderNum
Its that simple. So in this way, when we want a particular value from another action, this is a very simple way of doing this. Try experimenting with this and you are surely going to get a hang of how action parameters help..
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 (0)

How to debug a script in QTP?

Its easy to debug a code which we have written, but what if the code is developed by someone else !!! Sometimes I feel that its easier for me to write a totally new code rather than spend the time trying to read someone else's code and understand it. However once we join a company and start working on huge amounts of scripts already developed and written, do we have that liberty to tell them that I will write my own code rather than read someone else's code !!!! No way !!! Its always a best practice to document your code very well. But dont ever go and document each and every line..that makes your code look so ugly. We must document the logical separations in the code. But a lot of people dont get into that habit of documentation. This makes the job of someone else reading the code even more difficult. I have frequently been using the below mentioned techniques to debug and it has proved very efficient (in a while I will tell which is the technique I like the most)

1. Giving MsgBox statements
2. Using Reporter.ReportEvent statements
3. Using Print statements
4. Using Run from Step
5. Using Watch
1. Giving MsgBox statements
For small pieces of code, its very useful to use MsgBox statements where you can popup a message box to display the value of the variable you want to print. It also helps to use message boxes to know whether we are actually entering a condition statement like If or While. Its a common thing while debugging to "think" that we entered an If statement or a While statement where in reality we would not have entered.
2. Using Reporter.ReportEvent statements
If our script is very large and we want to verify lots of variables or want to display certain useful information which helps in debugging, we can use the Reporter.ReportEvent statement. For informational purpose we will use Reporter.ReportEvent micDone, "Info","Entered loop". If its just an information, we will use micDone instead of micPass or micFail.
3. Using Print statements
QTP has a utility called Print. If we give Print "Message", a log window will popup during your execution but remember that this log window will not hinder your execution like a MsgBox. So if you give multiple print statements, you can see all the messages that you want to print in the log window without interrupting your execution.
4. Using Run from Step
Step by Step execution is a technique when we want to monitor very closely whats happening after each step. In this case, each step will execute only when we manually execute them. Lets say, we want to execute Line 10 in our script and want to know what happens after the execution of this line but dont want Line 11 to execute unless we instruct it to do so... Bring your cursor to Line 10 and click on it... Go to Debug->Run from step. Now you will see the yellow marker on the left side of your statement and QTP will wait for us to execute that statement. Pressing the F11 key will now execute each statement step by step. This way of debugging is very useful as we know exactly what happens after each statement and the execution speed is completely under our control.
5. Using Watch
Watch is a Debug feature which is very powerful and this is the one I prefer the most. This can effectively be used with the above mentioned step by step execution. Lets say while doing the step by step execution, we want to know the value of a particular variable. We can get this value easily by using Watch. Select the variable and press Ctrl+T or Debug->Add to Watch. Now you will see a Debug window below your script and the variable you wanted to "watch" would be added to the list. The value would be listed alongside as per the variable's current value in the execution. So we can keep "watch"ing different variables as we step.
Debugging a program is a skill. Knowing what to watch and how to debug takes some experience. Using the techniques as mentioned above will easily help to find what went wrong when the test didnt execute as expected.
Hope this added some new information to you.
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)

How to iterate actions ?

I would like to bring about a common confusion which happens among beginners in QTP... the concept of iterations and its settings in QTP. You can change the way QTP iterates an action but wait...... do you want to iterate on the local data table or global data table ? Once you become more used to programming in QTP, you will mostly use DataTable.GetSheet.GetRowCount and then do whatever you want..but for those who dont prefer to do it programmatically, do it by changing the QTP settings...

Remember File->Settings->Run is the setting for the Global Data Table and
Action Call Properties -> Run is the setting for the Local Data Table

Now how to iterate

Not worried about data table?
If you are not concerned about the number of rows in the data table, then use a for loop inside your action
For times = 1 to x
action statements
Next

Depends on Global Data Table?
However if you are wanting it to be based on the no. of rows in the global data table, use the File->Settings->Run Option to run on all rows...

Depends on Local Data Table?
If you are wanting it to be based on the no.of rows in the action's local data table, go to the Action Call Properties and modify the selection in Run

Note:Never forget the DataTable.SetCurrentRow() statement if you want to programmatically achieve this... you can get wierd results otherwise :)

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 (0)

Loading Add-ins through VB script

In my current project we are executing the QTP scripts from Quality Center through a VB Script i.e we dont manually call QTP or QC. We use the QC OTA to achieve this. Today I came across a situation where some of the tests required Power Builder Add-in to be loaded and some the Web Add-in. I was trying to "google" on how to do this programmatically through VB script but unfortunately did not find anything. Finally I found it in the help documentation itself !!!(Remember how I mentioned earlier how powerful the help documentation is !!!!) Ok....so how do we do this...

We are using two methods here to achieve this
GetAssociatedAddinsForTest and SetActiveAddins

Description
GetAssociatedAddinsForTest Returns the collection of associated add-ins for the specified test without opening the test. This method can be called either before or after you start QuickTest (before or after the Application.Launch method).
SetActiveAddins Instructs QuickTest to load the specified add-ins when it opens. You can use this method only before starting QuickTest (before the Application.Launch statement).

Dim qtApp
Dim blnNeedChangeAddins 'check whether the test's associated add-ins are currently loaded
Dim arrTestAddins ' Declare the variable for storing the test's associated add-ins

Set qtApp = CreateObject("QuickTest.Application")
arrTestAddins = qtApp.GetAssociatedAddinsForTest("C:\Tests\Test1") ' returns an array containing the list of addins associated with this test
' Check if all required add-ins are all already loaded

blnNeedChangeAddins = False ' Assume no change is necessary
For Each testAddin In arrTestAddins ' Iterate over the test's associated add-ins list
If qtApp.Addins(testAddin).Status <> "Active" Then ' If an associated add-in is not loaded
blnNeedChangeAddins = True ' Indicate that a change in the loaded add-ins is necessary
Exit For ' Exit the loop
End If
Next
If qtApp.Launched And blnNeedChangeAddins Then

qtApp.Quit ' If a change is necessary, exit QuickTest to modify the loaded add-ins
End If
If blnNeedChangeAddins Then

Dim blnActivateOK
blnActivateOK = qtApp.SetActiveAddins(arrTestAddins, errorDescription) ' Load the add-ins associated with the test and check whether they load successfully.
If Not blnActivateOK Then ' If a problem occurs while loading the add-ins
MsgBox errorDescription ' Show a message containing the error
WScript.Quit ' And end the automation program.
End If
End If

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)

How do I learn QTP ?

The very first question that came to my mind when I decided to be in the role of a Quality Analyst was "What should I be learning to advance my career in the role of a tester?". There are so many tools available out there to test. Functional testing is one of the crucial part of any development life cycle. Ok..so let me start learning a functional testing tool. I picked QTP because a lot of employers are using this tool for their testing. Today automation is having so much importance as repeated testing improves quality and saves time. Obviously Time is Money.
How do I learn QTP ? Before answering this, we need to know whether there are any other pre-requisites that we need to learn. Yes !!! Did I just say yes ??? Thats because I have seen a lot of people learn QTP either through classroom trainings or online trainings but then when they actually start working they get stuck. When I started using QTP, I already had prior programming experience in C, C++ and shell scripts. I have always felt that there are three basic things you need to know if you want to be a good automation specialist on QTP.

1. Familiarity with at least one programming language
2. Familiarity with Object Oriented concepts
3. Habit of "googling" and eagerness to solve a problem on your own

You dont need to be a VB script expert in the beginning. At least , I was not !! But you will eventually be. No doubts on that. I have been beating around the bush and have not yet answered the intention of this post "How do I learn QTP". When I first started, I tried to see if there were any books to buy on QTP. I roamed around in Barnes and Noble, Borders, amazon.com and was surprised initially to find that there were no books to buy on a product that was so popular. Later on I realised that there was a reason why you dont get to buy books.. There is nothing more comprehensive than the Help documentation that comes along with QTP. Take my word for this. This is where I started and I have to say, its an awesome start !!!

Help->Quick Test Professional Tutorial... Yes....this is where I started !!! I took time to go through that entire tutorial and by the end of it I could figure out the "concepts" of this application. The tutorial covers almost all the basics of QTP. But do you become an expert after completing this tutorial. No way !!! Thats where Help->Quick Test Professional Help comes into the picture and this is what I mentioned earlier that you do not need to buy a book when you have this help with you.

VB scripting, Objects, BPT, Add-ins, Debugging ... its all there well documented. But for beginners, dont jump into it too fast.... you need to start with the tutorial...as for the rest, lets take it step by step..

So much for now..... let me stop fooling around and get into some serious business here....

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 (1)comments

Just starting up

Hi friends, Welcome to this site.... Thought of sharing all that I learnt and still continue to learn on HP's Quick Test Professional through this site. Well, is this "Just another QTP site" !! Hopefully not... There are already too many sites out there where you can get most of what you need. Hope to make this a site where its not going to be just a cut/copy/paste of some other site. Intention is to share the real life scenarios and keep adding them here. This site should be like..."I got stuck....where do I go from here...is this a problem you have faced.."

Do you have the willingness to share it to the rest of the world... without expecting anything back in return !!

Anyways stay tuned.. Thanks for visiting !!

Your friend in need

George, Reju

Read Users' Comments (0)

Visitors

Website Counter