Showing posts with label VB Script. Show all posts
Showing posts with label VB Script. Show all posts

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)

Start and Stop Windows Services

Today we will be dealing with something not a QTP specific feature but its more to do with VB script. Lets say we need to start or stop a windows service like DHCP Client, Netlogon etc. This can be automated using VB script and in QTP without the need to open the Services window. Lets see how to do this.
Certain tests might need to be developed by starting or stopping certain services and then validate certain conditions. So this should be very helpful while developing certain tests. The basic code has been taken from MSDN where a very good example has been given.
There are a few things that we would be doing here
Get the WMI (Windows Management Instrumentation or Windows Management Interface) object
Execute a query to get the list of services
Start/Stop the service
Below you can see how we can get the list of all services. Click on the image to see the code.



To start or stop a service all that we need to do is to use the StartService() or StopService() method.  However, for that we would need to modify our query slightly. This is what we would need to do.Click on the image to see the code.





Notice the extra "quotes" used for the service name and that is because in the query we would need to have the filter in quotes.
Hope this helped.

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