Activate, Maximize, Restore and Minimize a Browser - Part 2


Here we are in Part 2 of this discussion. Please make sure you have gone through Part 1 before you continue further.
What we saw earlier was that it was possible to activate, maximize, restore and minimize a Browser. We did this because the Browser object did not have the Activate, Maximize, Minimize and Restore methods. However we could do something more interesting by actually creating these methods for the Browser object. This is accomplished through a utility statement in QTP called as "RegisterUserFunc".

Description:
Enables you to add new methods to test objects or change the behavior of an existing test object method during a run session.

Sometimes its interesting that QTP Help documentation gives detailed description on how a particular utility works but does not give a "practical" example. Lets not get into that.
RegisterUserFunc can actually do two things. Create a totally new method for an object or override an existing method. In our case, we are going to create a new method for the Browser object. So lets see how we are going to do that.



Voila !!!! We have now created a new method called Activate for the browser. You could also put the above function definition and RegisterUserFunc statement within a function library for more efficient handling of your resources. In a similar way, we could also go ahead and create the remaining functions for Maximize, Restore and Minimize and Register all these functions with the Browser object as below

RegisterUserFunc "Browser", "Maximize", "Maximize"
RegisterUserFunc "Browser", "Minimize", "Minimize"
RegisterUserFunc "Browser", "Restore", "Restore"

Well, we just saw a pratical example of RegisterUserFunc and also how to make use of this feature in our Browser object.

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)

Activate, Maximize, Restore and Minimize a Browser - Part 1



Have you ever had the need to activate, maximize or minimze a browser ? I am sure you had...but unfortunately the Browser object does not have these methods. So what do we do. I have faced this situation and so wanted to share what I did to accomplish this.
What we will do here is use two concepts. Descriptive Programming and Registering a function with an object which would make our code very neat.
The very first step with this approach is that we would be identifying a Browser object as a Window instead of a Browser. For the Windows operating system, after all, everything is a window. So we will make use of some properties within the Window object to recognize a browser. I had already covered this in another thread on QTP 10, Vista and IE8 where we do this.
OK. Before that lets understand one thing. Every window has a property called as "handle" and this is unique for each and every window. In QTP, this property is known as "hwnd". So a Browser window can also be identified with a Window Class object if we pass the handle. Open any browser and then spy on it. Get the window handle. Lets say its value is 459420. Now this browser can be identified as Window("hwnd:=459420"). So we are almost there. Since a Window object has the methods Activate, Maximize, Restore and Minimize methods, we could make use of them over here... isnt that interesting !!!!



Our code looks like above. Using the "hwnd" property is the best way to identify the browser because it is unique. However you could also make use of the "nativeclass" and "regexpwndtitle" properties which would be "IEFrame" and "Windows Internet Explorer" respectively.

Window("regexpwndtitle:=Windows Internet Explorer","nativeclass:=IEFrame").Activate

But the above code would not work when you have multiple browsers open. Thats the reason why I mentioned earlier that getting the "handle" of the window is the best technique.
So here we just showed that we could activate, maximize, restore and minimize a Browser as well !!!! Continue to read Part 2 on this topic. Enjoy the journey !!!!!

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