Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

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

ByRef and ByVal in Functions and Subroutines - Do I need to even bother !!!!


ByRef and ByVal are two keywords that you can prefix before a variable in the function or subroutine definition. I am sure most of us would not even have bothered to know more about this mainly because we do not need to. However let me still explain what these keywords mean with an example. Below is a subroutine which would return the sum of two numbers.



By default, all arguments passed to a function are passed ByRef or By Reference. So in the above example, we need not explicitly put ByRef before the variable. People familiar with C programming would already understand this concept where you would pass a pointer to a variable. So what is By Reference. Each variable is allocated an address in the memory during execution. When we say that a variable is passed By Reference what this means is that the address of both the variables are the same. So in this case, the address of intSum and intFnSum are both the same or rather they both point to the same memory location. So any change made to one variable would be reflected in the other as well. This is the concept of ByRef. Now want to see what would happen in the other case !!!!



Ok...so what happened !!!! Didnt get the expected answer, right ? Yes, thats because in this case intFnSum and intSum have two different memory locations and so the change made in one variable is not reflected in the other. Thats the concept of ByVal.
As I said, we probably would not use this feature much in QTP but even then, you never know when something could be useful, right !!!!!

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

Functions and Subroutines - Modularizing code in QTP


When you see job requirements, one of the most common thing that I see as a condition nowadays when recruiters look for QTP Automation Engineers is that the candidate must not be doing just playback and record in QTP. Well, the fact is that people who have just started working on QTP would most likely just have used the playback and record feature but once you get experienced, they would have started writing more code and when you start writing more code, you would definitely have started to write functions and subroutines as well. This technique of using functions and subroutines is actually what we call code modularization. This is not specific to QTP, its more of a general programming concept where we divide our program into repeatable and logical units so that our test becomes more organizedunderstandable, readable and maintanable (is there such a word...h-m-m...you got what I meant anyway !!).
Too much of theory, right ? So, as usual, lets get into an example. But before that lets talk about the one difference between functions and subroutines. Functions can return a value but a subroutine cannot. I am sure that this is what you have read everywhere but I will show you what this really means. Lets first write a simple function to add two numbers..

















Line 1 : This makes sure that we dont mistype any variable names
Lines 4 - 8 : Function Definition
Line 12 : Function is called and its return value is stored

Couple of things to note over here is that

  • A function returns a value by assigning the value to the function name. So Line 6 is where the function is storing the return value.


  • Another thing is that this assignment of (function name)=(value) need not be the last line in the function. Aha.....never thought of that , eh ? Yes, its possible. Let me show you another piece of code.





























OK... what did we just do above ? Lets say that I never expect the function AddNumbers to return -1 and I have so many lines of code before the final addition. For some reason, for any reason, for whatever reason, lets say the function just terminated in between, the Line 23 would actually get the value -1 if at least the first line of the function had been executed. So all that I wanted to show was that (function name)=(value) need not be the last line in the function. The code I showed above is just a technique I use, not that its the best technique but it definitely is a good one !!! But ideally we would not have any statements after this because we would generally assume that the function did its job once the value is returned.

  • Third thing to note is that we need not declare the variables or the function arguments intA and intB in the function. So our Option Explicit will not complain.


  • Another point to note is that it is not necessary that a function has to return a value. Which means even the below is possible





















But then if you dont want your function to return anything, then why even use a function !!!! Thats where we can make use of subroutines. However, in the beginning, I mentioned that functions can return a value whereas a subroutine cannot. What this means and what this only means is that if AddNumbers was a subroutine, you could never write Line 14 as above. However, you can write a subroutine also to obtain the sum, and in this case, the only thing is that you would have to pass an additional argument.


















OK, so we did the trick with a subroutine as well. I intentionally passed intSum to the subroutine and had the function argument as intFnSum just to show that we do not need to pass the same argument name. It could be just anything.
Notice the call to the subroutine here. We did not use any parantheses here as this is the VB script syntax to call a subroutine. However if you want to use it , there is still a way. Replace Line 12 above with

Call AddNumbers (208, 392, intSum)

Beginners to VB script would be thinking that a function can only return one value. By definition, even though this is true, dont restrict your mind to think that its possible only to return one value. You can make use of multiple arguments as shown above to get multiple values. Yes, just a question of logical programming. I guess I can keep on writing about functions and their interesting facts but I guess this should be sufficient as far as this topic is concerned. You know what... when you play with it more, you learn more and more interesting facts !!!!
As you would have noticed, I wrote the function definition in my action itself. This makes the function available only within the action. What if I want to make this function available publicly i.e. to anyone !! Thats where we would make use of Function Libraries which I will cover in another topic.

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)

Visitors

Website Counter