Showing posts with label QTP. Show all posts
Showing posts with label QTP. Show all posts

Descriptive Programming OR Regular Expression ? - Part 2

This is the second part to the post "Descriptive Programming OR Regular Expression ?". I recommend you to read the first part before you proceed, so please click here.
In the earlier part, we discussed how to use descriptive programming to make QTP to skip looking at an object in the object repository. Instead, we understood that descriptive programming helps QTP to identify an object in the application dynamically without searching in the object repository. In this part we will see another technique to look at objects when we are not sure of a particular behaviour of an object which would keep changing for every iteration or every time you work on the application. Some examples are new order numbers which are created newly every time or checking for the current date and time which would always change. Lets look at the example we saw in Part 1, where we want QTP to continue with the testing even when the order number changes. Here is where regular expressions are of great help. One of the sites which I often refer to for regular expressions is the regular-expressions.info site.
The way we are going to accomplish our task can be done through two ways.
1. Use regular expressions in our descriptive programming OR
2. Set a particular property value to a regular expression so that it would match the patterns that the particular property can have.

Lets try the first option. Now, we know that the only property which changes in our case is the "text" property of the Dialog object. So lets change the property to a regular expression. We know that the value "Fax Order No." is always constant but its the order number that keeps on changing. So we need to set a pattern such that the property text value should accept anything like Fax Order No.1, Fax Order No.2 or even Fax Order No.100. In a regular expression, to match any number, the pattern would look like [0-9]+. This means that any occurrences of any digits would match this pattern. So lets see how our script is going to look like...
The script in Part 1 would only have worked for Order no. 9 with the modified script but now, we have modified such that lines 7 and 8 of this script would work on any order number. However this script is very small but if we had a very large script, can you anticipate what the problem could be ? Yes, we would have to sit and modify each of the child objects that come after Dialog because we have to use descriptive programming for all the child objects as well. So here is where we are going to use one of the mostly used and effective techniques in QTP. We are going to make the regular expression change in the object repository itself and our program is going to look much neater.
We made the regular expression modification in the object repository and notice that I made one more change. Since this object is now going to be a more generic object, I modified the logical name of this object from Fax Order No. 10 to just Fax Order No. Now lets see how our code is going to look like
Now we dont need to take care of the child objects here because we are no longer using descriptive programming here. This has made our code also look very neat and this is one of the best ways to implement dynamic handling of objects in this particular case. I know this is quite overwhelming in the beginning but descriptive programming and regular expressions are some of the very important features that we make use of in QTP.

Hope this example helped you in understanding this concept. Suggestions are always welcome.

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

Descriptive Programming OR Regular Expression ? - Part 1


Hey, wondering what the title of this thread means ? Whats the relation between Descriptive Programming and Regular Expressions ? OK...we will come into all that. Of course, we are going to have an example, as always, to explain any concept. The example we are going to take here is an application that comes shipped with Quick Test. Its a windows based Flight Resevation application. I thought of taking this example as I could neatly explain descriptive programming and regular expression at the same time.
For those who do not have this application, dont worry, the screen shots below would be sufficient to understand the concept. Alright, so lets get into it. I opened the flight application and logged in. I have started recording only from this point forward.


First I go to File -> Open Order and selected Order 10 and clicked OK.


I then enter a FAX number and click the Send button.


OK..lets stop recording as we just need this much. Lets look at how our script looks like.

So everything looks good. If we try running this script its all fine. Now, lets perform this test on order 9 instead of order 10 so we need to modify line 4, and modify the value of 10 to 9 and then run the test. What just happened !!


OK...so whats the deal. Here is what happened. When we recorded this script, we recorded on order 10, as a result the properties of the Dialog window for Order 10 is what got stored in our object repository. Understand that its not the value "Fax Order No. 10" in Dialog("Fax Order No. 10") that is the problem. Its the properties of the Dialog window which caused the problem for which we need to see the object repository.

"Fax Order No. 10" (the value you see against Name: on the top) in Dialog("Fax Order No. 10") is just a logical name to the object and we could give any value to the logical name. Hope that is clear. If yes, then lets proceed. The object recognition problem happened because the "text" property of the dialog in the object repository did not match the application because now our text property has changed to "Fax Order No. 9". If you use the object spy on the new dialog, you would notice this. So much for the theory, now how do we resolve this issue !! We obvously want to run our tests as we may want to test this on multiple orders.
There are two ways to do this. Descriptive programming and using Regular Expressions or I would also call it "Property parametrization".
First lets look into Descriptive Programming and what it means. Below is a snippet from the Quick Test Help documentation.
"Descriptive Programming instructs QuickTest to perform operations on objects without referring to the object repository or to the object's name. To do this, you provide QuickTest with a list of properties and values that QuickTest can use to identify the object or objects on which you want to perform an operation."
In the case above, the object Dialog("Fax Order No. 10") is the object which changed which we want to work dynamically i.e. on any order. To make our code work on order 9, this is what we will do. We know that its the "text" property of the dialog which changed. So lets modify line 7 above to
Window("Flight Reservation").Dialog("text:=Fax Order No. 9").WinObject("Fax Number:").Click

Now lets rerun the modified test and see what happened. We got an error again.

And this is because
"When using programmatic descriptions from a specific point within a test object hierarchy, you must continue to use programmatic descriptions from that point onward within the same statement. If you specify a test object by its object repository name after other objects in the hierarchy have been specified using programmatic descriptions, QuickTest cannot identify the object." ( as taken from QTP Help documentation)
What this means is that, we used descriptive programming to help identify the Dialog object but we have to continue using descriptive programming from that point onwards i.e. from the Dialog object and all the child objects from that point within that statement. In short, we have to use the same technique to identify the object WinObject and even for the WinButton in Line 10. Before that lets see what properties QTP used to identify the WinObject.

and the WinButton
So we will make use of the "regexpwndclass" property for WinObject and we will use just the "text" property for the WinButton. Our modified code would look like this.

So here, you must understand that from Line 7 onwards, QTP will refer to the object repository only to look for the Window("Flight Reservation") object but from Dialog onwards, it will just search dynamically within the application (without referring to the object repository) to see the objects with the matching properties as specified by us.

Run the test and everything is going to be perfect !!
The second part of regular expressions is covered in Part 2.
Hope this example helped you in understanding this concept. Suggestions are always welcome.



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

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

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)

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