Saturday, July 28, 2012

How to activate, minimize, maximize a browser using QTP

You can use below code to active, minimize, maximize a browser. I put all these three methods in a single function, but you need to tweek this code as per your needs.

Function WinActivate(Object)
   Dim hWnd
    hWnd = Object.GetROProperty("hwnd") 'First get the window handle
    On Error Resume Next
    Window("hwnd:=" & hWnd).Activate 'Put fouse on the window
    If Err.Number <> 0 Then
            hWnd=Browser("hwnd:=" & hWnd).Object.hWnd 'now get the browser handle
            Window("hwnd:=" & hWnd).Activate 
            'Window("hwnd:=" & hWnd).minimize 'To minimize the browser
            'Window("hwnd:=" & hWnd).maximize
            Err.Clear
    End If
    On Error Goto 0
End Function

RegisterUserFunc "Browser","Activate","WinActivate"
RegisterUserFunc "Browser","Minimize","WinMinimize"
RegisterUserFunc "Browser","Maximize","WinMaximize"

Browser(objBrowser).Activate
Browser(objBrowser).Minimize
Browser(objBrowser).Maximize

Saturday, June 30, 2012

How to verify a Browser Window is minimized

The bad part is  QTP does not support retrieving RO Properties of a browser. If it has supported, then we would have use a method like Browser("Browser Name").GetROProperty("minimized")




















But we can use Extern to use some of the methods from Windows user32.dll
Following are the two ways to verify the window minimized.
Extern.Declare micHwnd, "FindWindow", "user32.dll","FindWindowA", micString,micString 'this method returns a handle
Extern.Declare micLong,"GetWindowMinimizeState", "user32.dll" ,"IsZoomed",micLong 'IsZoomed meaning IsMaximized
hwnd=extern.FindWindow(NULL,"Links - Windows Internet Explorer")
print Extern.GetWindowMinimizeState(hwnd) 'Similar way


Const GA_ROOT = 2
'Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long Extern.Declare micLong, "GetMainWindow", "user32" ,"GetAncestor",micLong, micLong
hwnd = browser("name:=Links.*").GetROProperty("hwnd") 'this returns the handle of the Browser Tab hwnd = Extern.GetMainWindow(hwnd , GA_ROOT) 'This returns the handle of the browser(which means handle of the browser)
msgbox Window("hwnd:=" & hwnd ).GetROProperty("minimized")

How to find and fill a color in Excel Cell

Set xlObj=getobject("","Excel.Application")
Set xlWBObj=xlObj.workbooks.open("c:\delete.xls")
set xlWSObj=xlWBObj.worksheets(1)
xlWSObj.cells(1,1).Font.Color=vbRed  'This statement changes the color of the Text in the specified cell
xlWSObj.cells(2,1).Interior.ColorIndex=4 'This method changes the color of the background of the cell
print  xlWSObj.cells(1,1).Font.Color
print xlWSObj.cells(2,1).Interior.ColorIndex
xlWBObj.save
xlWBObj.close
xlObj.application.quit
Set xlObj=nothing

Monday, April 30, 2012

How to copy content to Clipboard

We can use clipboard using two ways.
One is using Mercury.Clipboard.
Second one is using DotNetFactory.


Using Mercury.Clipboard:
'Creates an instance of Mercury.Clipboard
set objCB=createobject("Mercury.Clipboard")
'Here i am clearing already existing content in clipboard
objCB.Clear()
'Copying some text into Clipboard
objCB.SetText("Hello Uday")
'Retrieving the content from Clipboard
print objCB.GetText

Using DotNetFactory:
'Creates an instance of DotNetFactory Computer Object
Set objDFComputer=DotNetFactory("Microsoft.VisualBasic.Devices.Computer","Microsoft.VisualBasic")
'Here i am clearing already existing content in clipboard
Set objCB=objDFComputer.ClipBoard
objCB.clear()
'Copying some text into Clipboard
objCB.SetText("Hello Uday")
'Retrieving the content from Clipboard
print objCB.GetText

Sunday, April 29, 2012

"Setting" utility object to access registry values

We can obtain and configure all the QTP configurable parameters(all parameters in MicTest) using "Setting" utility object. Ex: BrowserType, DefaultLoadTime etc...
For Ex: If you want to retrieve the default location of saving your tests, which exists in HKLM\Software\Mercury Interactive\QuickTest Professional\MicTest\TestsDirectory, you can use below line of code:
Setting
print Setting("TestsDirectory")

You can also use Setting.Item to retrieve the value of the parameter. For the above, you can also use like:
print Setting.Item("TestsDirectory")

If you want to access a parameter exists in folders, then you can use
Setting("")("Child1")("Child2)("PropertyName")

Here is the Ex. to access the Add-In Manager name for Visual Basic.
print Setting("AddIn Manager")("VisualBasic")("Name")