Execution flow of Function Libraries

Function Libraries - An area where most interviewers focus on. The reason for this is that if you have worked on QTP, you just cannot miss using function libraries. If you didnt, then there definitely is something wrong in your framework. Functions and reusable actions help you achieve code modularization which is a very important aspect in test automation development.

I was not thinking of explaining the function libraries and how to associate them but rather on something very interesting related to the execution flow. You can already get a lot of information on what a function library is and how to work with them. So whats the fun if I was going to repeat the same thing, right !!!!! Thats the reason why I did not want to concentrate on that topic, however if you do want me to post a thread on the details, please do let me know.

OK..so what was I meaning by execution flow. Ideally and practically, a function library would contain function definitions, external function declarations and/or global constant declarations. However you must understand that theoretically and technically, it could actually contain any valid VB script syntax. So, to show the execution flow, I am going to do something here which we would not normally have in a function library but this is just to explain one particular concept here.

First I am going to create a function library called as Lib1 and this contains just the statement
MsgBox "Within Function Library 1"

This is what I said above that we wont put a MsgBox in a function library and this is purely for illustration purpose. Next I am going to create a test and associate this function library within that test.



















My test contains nothing but another MsgBox statement






So we are ready... Lets run the script now and see what happens...
We first got








And then...








So what does this mean. When an action is run, the first thing that happens is that function library is executed first and only then the code within the action is being executed. So far so good but lets try to make it more interesting. Lets create another function library called Lib2 and associate that too with this test. Lib2 contains another MsgBox statement
MsgBox "Within Function Library 2"


 
















Now lets run our action and see what we get but before that I want you to anticipate what the outcome would be and then check the result. Which MsgBox came first ???








And then








And then








I am sure most of you would have expected the Function Library 1 MsgBox to appear first, right ? But no, the order of execution of the Function libraries are Bottom-Up and NOT Top-Down. Well, you can change the order of the function libraries and see what happens. But dont you think this is sort of wierd !!! From a user point of view, it looks like the library on the top would be executed first. So I am not sure if this is a bug in QTP or is this as per design.

Well, it doesnt really matter so long as we know what the order of execution is. I hope you all enjoyed this interesting fact and understood the execution flow of function libraries. See you soon !!!



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

The Power of Extern


One of the very powerful tools in QTP - the Extern object. Thinking why this is so powerful !!!! The Extern object is one of the techinques of "extending" the capabilities of QTP. Understand that QTP, by itself, has numerous functionalities like Data Tables, Checkpoints, Synchronization points etc..etc.. But have you not ever come across a situation where you found that QTP could not do something on your test application !! I am sure you would have !!!!

OK...lets try this... Lets open the windows explorer...





How do we get the column width for the "Name", "Date modified" or "Type" column. QTP will be able to identify the area marked in red and so you can perform all the available methods QTP gives to you for the WinListView but is there a method to actually get the width of a column ? H-m-m not really... and that is where the Power of Extern comes into the picture. But first what is this Extern Object ???


The Extern object enables you to declare calls to "external" functions from an "external" dll or dynamic-link library.


OK...that would have been a pretty complex statement for beginners so lets try to simplify this statement.... DLL is a Microsoft implementation of a concept called as shared libraries. I dont want to deviate too much from our current topic but as of now just understand this, DLLs would export some functions which we will be able to use later on. Some of these functions can be called from the user level and others only at the kernel level. Different programming languages provide different techniques for achieving this and in QTP it is through the Extern object. I guess that should be sufficient for now and when we see the example below, we are going to get a real good idea of this concept.

So how are we going to find the width of the columns above. We know that QTP's WinListView object does not have any capability of its own to give us this information but there is someone else - user32.dll. This is a file residing in C:\Windows\System32 and it has a function called as SendMessage which we are now going to use in QTP.... now its getting interesting right... alright so lets get into it.

We first need to declare the function because QTP does not know about this function yet and this is how we are going to declare it

Extern.Declare micLResult, "SendMessage", "user32.dll", "SendMessage", micHwnd, micUInteger, micWParam, micLParam


Now you must be wondering how I knew the prototype of this function, right ? Just go to MSDN... SendMessage function description

At this point, QTP knows that there is a function called as SendMessage and will now allow us to use this. However SendMessage is not just for getting the column width, it has numerous other uses which you can research on your own time but what I am going to show is just the mechanism to get the column width.

The first argument to the SendMessage function is the handle to the object and in this case its the WinListView object.

The second argument is the message to be sent and in this case it should be LVM_GETCOLUMNWIDTH. As of now, just understand that the decimal value for this is 4125.

The third argument is the index of the column which starts at 0.

The fourth argument should always be 0.

Dont get too overwhelmed at this stage !! We are not trying to explain what SendMessage is but rather the Power of Extern object which helps in using external functions. A developer in your team could provide you a dll as well and you can make use of the functions in that dll if you know the function prototype. OK... so lets move ahead..

We are all set now to get the column width and here is how our program is going to look like.



The third argument is the column index. So in this example, it would give the column width of the first column which is "Name". If you change it to 1, it would give the column width of the second column which is "Date modified".

Well, we just experienced the Power of the Extern object which "extends" the capability of QTP. We were able to do something which the WinListView object was not capable of. A very powerful tool which you would definitely need as you do more advanced programming.


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)

Visitors

Website Counter