This article demonstrates a quick tip to activate, minimize or maximize browsers. Unlike a Standard Windows Window object, Browser does not support the Activate, Minimize, Maximize methods. Therefore, we can create our custom function and tie it with the Browser object using RegisterUserFunc.
The only trick is to retrieve the Browser Handle and substitute the handle in the description of a Window object, and use the Window’s Activate method instead.
Activate Browser
Function BrowserActivate(Object) Dim hWnd hWnd = Object.GetROProperty("hwnd") On Error Resume Next Window("hwnd:=" & hWnd).Activate If Err.Number <> 0 Then Window("hwnd:=" & Browser("hwnd:=" & hWnd).Object.hWnd).Activate Err.Clear End If On Error Goto 0 End Function RegisterUserFunc "Browser", "Activate", "BrowserActivate" |
After registering the BrowserActivate function with the Browser object as Activate we can use it just like we would use it for a Window object:
Browser("title:=Relevant Codes.*").Activate |
BrowserActivate can be extended to maximize and minimize a browser window as well. The only extra statement to be included in the function would be the maximize and minimize methods of the window object.
Minimize Browsers
Function BrowserMinimize(Object) Dim hWnd hWnd = Object.GetROProperty("hwnd") On Error Resume Next Window("hwnd:=" & hWnd).Activate If Err.Number <> 0 Then hWnd = Browser("hwnd:=" & hWnd).Object.hWnd Window("hwnd:=" & hWnd).Activate Err.Clear End If Window("hwnd:=" & hWnd).Minimize On Error Goto 0 End Function RegisterUserFunc "Browser", "Minimize", "BrowserMinimize" |
Maximize Browsers
Function BrowserMaximize(Object) Dim hWnd hWnd = Object.GetROProperty("hwnd") On Error Resume Next Window("hwnd:=" & hWnd).Activate If Err.Number <> 0 Then hWnd = Browser("hwnd:=" & hWnd).Object.hWnd Window("hwnd:=" & hWnd).Activate Err.Clear End If Window("hwnd:=" & hWnd).Maximize On Error Goto 0 End Function RegisterUserFunc "Browser", "Maximize", "BrowserMaximize" |
If you would like to use the above 3 methods through a single function or class, they can be coupled together through Execute statements or through If-Then or Switch-Case blocks. Happy reading!
{ 60 comments… read them below or add one }
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.SendKeys “%{ }”
wait(3)
WshShell.SendKeys ” x”
Set WshShell=Nothing
wait(2)
Hi,
Is it possible to use the RegisterUserFunc to add a new method called “Type” which sends input character by character rather than “Set” which sets the entire string at one go?
Something like the WinEdit object’s Type method.
Rubin
hi Anshoo,
your solutions are very much helpful to me !
i have a query… i met with a situation in which there are two buttons say B1 and B2 in a page and clicking on each button they display diffirent links (Something like a dropdown) and each link is identified as a webelement in QTP. Each link upon clicking will open a new window. All the links are dynamic in nature, their object name or innertext or outer text are always changing.Now my question is there are two links under B1 and B2 respectively with the same name as “Style”. The inner text or outer text of both of them are same again as “Style”. Now can you help me out in how to identify the link “Style” under B2 and click it using QTP in descriptive method ??
Vinay, there are several things you can do to work around this situation. I’m going to break it down into a list:
1. Using Index, not sure how reliable it is though in your case
2. Something to consider is working with the developers to add an ‘id’ property to it
3. You could also use visual relational identifiers if you’re on QTP 11.0
4. If each button is in a WebTable, use WebTable methods (ChildItem etc.) to get to the correct object
thanks mate.
it worked for me :)
Hi anshoo,
I am trying to fetch hwnd value of a browser using following code.
Function fnWEB_objBrowserPage()
On error resume next
Set oBrowser = nothing
Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
Hwnd = extern.FindWindow(“IEFrame”,vbNullString) ‘ returns value of hwnd
Set oBrowser = Description.Create
oBrowser (“hwnd”).value = Hwnd
‘Window(“HWND:=”&Hwnd).Activate
Set oBrowser1 = Browser(oBrowser)
Set oBrowser = Browser(oBrowser).Page(“micClass:=Page”)
‘msgbox oBrowser.getROProperty(“title”)
On error GoTo 0
End Function
‘End of code
extern.FindWindow(“IEFrame”,vbNullString) returns ‘native’ value of an open browser
however when i use this property, QTP is not able to identify the browser using hwnd property since it checks for the identification hwnd property.
it is working fine with IE6 but not working for IE7 and above.
using object spy, i have observed that the native HWND property value and identification HWND property value is different
Is there any solution for this ??
Regards,
Maulik
Maulik, try this:
Function fnWEB_objBrowserPage() On error resume next Set oBrowser = nothing Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString Hwnd = extern.FindWindow("IEFrame",vbNullString) ' returns value of hwnd Set oBrowser = Description.Create oBrowser ("hwnd").value = Hwnd nativeHWND = Browser("hwnd:=" & Hwnd).Object.hWnd Window("HWND:="& nativeHWND).Activate Set oBrowser1 = Browser(oBrowser) Set oBrowser = Browser(oBrowser).Page("micClass:=Page") 'msgbox oBrowser.getROProperty("title") On error GoTo 0 End FunctionHi Anshoo,
I want to Connect IBM DB2 database. I used the following lines to connect. when i run the below code i get a Error message “[[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Line (6): “cnMidas.Open”. ”
Set cn = CreateObject(“ADODB.Connection”)
connectionstr =”Driver={IBM DB2 ODBC DRIVER};Database=myDatabase;Hostname=myserver Address;Port=1234;Protocol=TCPIP;Uid=myuserID;Pwd=mypassword;”
cn.connectionstring = connectionstr
cn.Open
If cn.state=1 then
msgbox “Pass”
Else
msgbox “Fail”
End If
Can you please help me regarding this issue?
System and Tools details
OS = Window XP , QTP Version 10.0 , IE7
Hi Anshoo,
I am not able to execute successfully
I am using this code ,
Function BrowserActivate(Object)
Dim hWnd
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd
On Error Resume Next
Window(“hwnd:=” & hWnd).Activate
If Err.Number 0 Then
Window(“hwnd:=” & Browser(“hwnd:=” & hWnd).Object.hWnd).Activate
Err.Clear
End If
On Error Goto 0
End Function
RegisterUserFunc “Browser”, “Activate”, “BrowserActivate”
Function BrowserMinimize(Object)
Dim hWnd
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd
On Error Resume Next
Window(“hwnd:=” & hWnd).Activate
If Err.Number 0 Then
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd
Window(“hwnd:=” & hWnd).Activate
Err.Clear
End If
Window(“hwnd:=” & hWnd).Minimize
On Error Goto 0
End Function
RegisterUserFunc “Browser”, “Minimize”, “BrowserMinimize”
Set obj = Description.create()
obj(“micclass”).value = “Browser”
obj(“name”).value = “QTP Browser Methods.*” ‘ : .Activate .Maximize .Minimize — Relevant Codes (by Anshoo Arora)”
obj(“title”).value = “QTP Browser Methods.*” ‘: .Activate .Maximize .Minimize — Relevant Codes (by Anshoo Arora) – Windows Internet Explorer”
Call BrowserMinimize(obj)
I am getting this error
“Cannot identify the object “[ Browser ]” (of class Browser). Verify that this object’s properties match an object currently displayed in your application.
Line (26): “hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd”. “
Imtiaz, not sure what went wrong there but please try this as an example:
Function BrowserActivate(Object) Dim hWnd hWnd = Object.GetROProperty("hwnd") On Error Resume Next Window("hwnd:=" & hWnd).Activate If Err.Number <> 0 Then Window("hwnd:=" & Browser("hwnd:=" & hWnd).Object.hWnd).Activate Err.Clear End If On Error Goto 0 End Function RegisterUserFunc "Browser", "Activate", "BrowserActivate" Function BrowserMinimize(Object) Dim hWnd hWnd = Object.GetROProperty("hwnd") On Error Resume Next Window("hwnd:=" & hWnd).Activate If Err.Number <> 0 Then hWnd = Browser("hwnd:=" & hWnd).Object.hWnd Window("hwnd:=" & hWnd).Activate Err.Clear End If Window("hwnd:=" & hWnd).Minimize On Error Goto 0 End Function RegisterUserFunc "Browser", "Minimize", "BrowserMinimize" 'code SystemUtil.Run "iexplore.exe", "http://google.com", , , 3 With Browser("title:=Google") If .Exist(15) Then .Activate .Minimize End If End WithHi Anshoo,
Im Automating putty using QTP. here i need to run some jobs using Unix commands for 2 different environment and compare the 2 resulting records. is this possible with QTP??
Can any one please tell me what addins are required for Autosys tool. i used Java Addin but only the window and java table is getting identified. please help me
Could u please any one post the script for icicibank login ??
I want to close all opened windows(not browsers) from QTP. I used the below code to run but my system is getting retarted. Can you please tell me why is this happening and correct it if its wrong.
SystemUtil.CloseProcessByName(“explorer.exe”)
You’re shutting down “explorer.exe” – also known as Windows Explorer. The behavior you see is the desired or correct behavior. You need to give the name of the Process that you would like to close. Other technique is using CloseProcessByWndTitle – Close Process by Window Title.
Hi Anshoo,
i want a QTP script which can open a excel sheet and delete a particulare rows. Say if the Cell value is 0 or some values like “*_ter”. Can you please help me.
Ramesh: Deleting a cell can also be the same as setting the value of the cell to Null (“”). Please see here: http://relevantcodes.com/excelutil-class-library-excel-utility-methods/
Hi,
I have a Browser object that I have been using QTP to record against
Browser(“Certificate Error: Navigation_2″).Page(“Page_3″).Frame(“Frame_2″)
I need to do use the following function “.Type “CityName” + micDwn” – however, the Type function is not available on Browser objects, only on Windows objects.
Is there a way to convert my browser object to a windows object so I can use Type ?
Thanks,
Daniel
Daniel: The Type method cannot be converted to be used in the Web Environment. You can use SendKeys as a replacement:
CreateObject("WScript.Shell").SendKeys "value"Hi anshoo,
im a regular reader of this site and thought of asking the below issue
while loading the script in to QTP, QTP gets closed Automatically. can you please tell me y this is happening.
im using QTP version 9.2
Can you make sure this is not because of a script issue? Are you performing a custom (yet illegal) DOM operation? If not either of these, can you reinstall QTP and see if that helps?
This is really a good information and I have used this to take screen shot of the application.
If I didnt activate the browser the TSL script screen shot is getting displayed
Loved your solution for these three unsupported methods by the Browser objects!!
While playing with the code (QTP10, IE8, Win7) I figured out some things and added some extra validations for those who like to report everything like me :)
msgbox BrowserMaximize(oBR)
Function BrowserMaximize(oBrowser)
Dim hWnd, oWindow, bMaximizable, bMaximized
If oBrowser.Exist(0) = False then Exit Function
hWnd = oBrowser.GetROProperty(“hwnd”) ‘the returned value corresponds to the “page” hwnd
Set oWindow = Window(“hwnd:=” & hWnd) ‘You can verify this by:
oWindow.Highlight ‘highlighting the object window that has this hwnd property, it will highlight only the page section
‘and if you perform action oWindow.Activate at this time you will always get an error since _
‘this object cannot be activated the same way a window is activated :P
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd ‘now let’s get the window hWnd with the object’s native properties
On Error Resume Next ‘this should not be needed now
Set oWindow = Window(“hwnd:=” & hWnd) ‘set the oWindow to the window with the new hWnd
oWindow.Highlight ‘when you highlight this object you can see it highlights the whole window :)
bMaximizable = oWindow.GetROProperty(“maximizable”) ‘you can read this property to check if the window is maximizable
bMaximized = oWindow.GetROProperty(“maximized”) ‘and you can read this one to check if the window is already maximized :)
If bMaximizable Then
If bMaximized = False Then
If Err.Number 0 Then
msgbox “there was an error” ‘this message box should not appear
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd ‘so this line is not needed anymore
Window(“hwnd:=” & hWnd).Activate ‘not needed
Err.Clear ‘ :)
End If
oWindow.Maximize ‘what you wanted to do, this line should not return any error :)
If err.number 0 Then ‘in case, only in case there was an unexpected error…
msgbox “there was an unexpected error: ” & err.description
BrowserMaximize = False
End If
On Error Goto 0
Else
Msgbox “browser window already maximized :)”
BrowserMaximize = True
End If
Else
Msgbox “Browser window cannot be maximized. Please verify” ‘of course you may want to remove all msgbox and highlight lines away :)
BrowserMaximize = False
End If
End Function
Regards,
Israel
Neat solution Israel and I really like the use of the two properties ‘maximizable’ and ‘maximized’ :)
Hi Ashoo,
I am getting a error message as “The operation cannot be performed” and getting failed and warning as “Object’s physical description:hwnd = 529660″
I am using IE7 .
Could you please help to solve this problem.
Imtiaz,
I have not been able to reproduce this issue on IE7. What procedure produces this error? Can you please share the code you’ve used?
vry useful info.
Hi anshoo many many thanks for ur reply.
Here RegisterUserFunc is it the built in function of qtp ?
Ravi,
Yes, RegisterUserFunc is a QTP specific function.
Hi Anshoo ,
i just want to know about few things that u have used in the coding… what is
On Error Goto 0 : ? and wat exactly it will do and wat is the use of it.
RegisterUserFunc : is it the function and u r passing the parametrs to it ? pls explain
Thanks in advance
Ravi
1. On Error Goto 0 is the exit statement of On Error Resume Next. Its like For..Next, or IF-Then-End If.
2. RegisterUserFunc from QTP help:
Enables you to add new methods to test object classes or change the behavior of an existing test object method during a run session.
Thanks Anshoo. It was really helpful.
I tryies the above code but it is not working , there is no error while executing the code but Browser is not maximizing nor minimizing.
Please help me .. to mini and maxi the Browser , i am using IE 7 version
hi
in the above conversations , i have seen a code
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd
could you highlite some information on { .object.hwnd} in the above line of code. i mean how exatcly this .object works
Thanks in advance
Vasudev
When you see
.Objectbeing used, it means, the code is working with the native properties instead of TO properties. In the object spy, there are radio buttons for Test Object properties as well as Native properties. The use of .Object points to the latter.what is hwnd in programming
Hi edwina,
A hWnd is the abbreviation of “Window Handle” which is an unique identifier. The browser or windows are assigning unique value to this property. So, we can use this property for the browser identification.
Thanks,
Sivaram S
how u will open a page using browser navigation
how do u capture the browser object when it happens to be the parent object….i used the followin code and it errors out…
Set oBrowser = Description.Create()
oBrowser(“micclass”).value = “Browser”
oBrowser(“name”).Value = “Edit Study.*”
oBrowser(“title”).Value = “Edit Study.*”
hWnd = oBrowser.GetROProperty(“hwnd”)…..it errors out over here…
Hi Sunny,
You cannot directly use the description object like that. It should be this instead:
Set oBrowser = Description.Create() oBrowser("micclass").value = "Browser" oBrowser("name").Value = "Edit Study.*" oBrowser("title").Value = "Edit Study.*" hWnd = Browser(oBrowser).GetROProperty("hwnd")You are doing gr8 job , Thank you!!!!
Hi Good Effort , Thank you
Hey , If any one have idea please tell me “Why” QTP Does not support
tab browsing ?
Deep,
What version of QTP are you using?
hWnd = Browser(“hwnd:=” & hWnd).Object.hWnd
Here, handle of the run-time object’s window ‘hWnd’ is used to identify Browser object.
But, I did not get the idea of getting the same value.
Could you please explain me about this?
This is a workaround for IE7+ versions because of tabbed browsing. If you have IE7 or greater, use your Object Spy to spy on the Browser object and you will find 2 different hWnd values – Test and Native. Thus, the Test time hWnd is used to retrieve the Native hWnd.
Hey anshoo
r u really doing a grt job
keep it on ….I am a beginner in QTP so some of ur codes are bouncing over my head :)
plzz can u suggest me some good sites where I can get every tit bit idea abt QTP and after tht i will go thru ur codes I will get tht completely
I have included a few websites in the Links section of Relevant Codes. They are all excellent resources which will certainly be helpful to you :)
Thanks for the useful info.
http://qualitypointtech.net/ebook/index.php
:)
Thank you…:-)
Yes Anshoo
I am using QTP 9.0
Does the version matters for Tab
Is there any version which supports Tab in browser.
Yes Anshoo
I am using QTP 9.0
Does the version matters for Tab
Is there any version which supports Tab in browser.
Deep,
I think tabbed browsing was supported after version 9.5..
Now really i got a better idea abt Test object properties and native properties
Thanks
thanks anshoo. i will get back to u with more doubts.
Hi ANSHOO,
i would like to know abt the RegisterUserFunc parameters.
i have seen few examples they are writing the [ RegisterUserFunc " " , " ", " " ] statement with 3 parameters but nt mentioning the syntax more clearly . i would like to know with example can u tell me in detail.
Ravi:
RegisterUserFuncsyntax:From help:
TOClass: The test object class for which you want to register the method.
MethodName: The method you want to register. The method name cannot contain spaces.
FunctionName: The name of your user-defined function. The function can be located in your action or in any library file associated with your test or component.
SetAsDefault: Optional. Indicates whether the registered function is used as the default operation for the test object.
Hi Anshoo ,
thank u soo much .. actually am in to development background but these very much willing and eager to learn qtp. so i found relavent codes site which gives me more technical stuff than other sites.
once again i thank u vey much.
You should also visit AdvancedQTP. Lots of technical information there.
Hi anshoo,
yes i visited advanced qtp.. and i like the qtp videos site http://motevich.blogspot.com/