Reusing Objects after Page Refreshes via Init
QTP web test objects go out of sync whenever the webpage Refreshes/Reloads. This means that if you held a reference to a web-object, you couldn’t could on it to work throughout your script.
'By using a .Init command after the page loads, the web-object resyncs, and the script will not break.
''Set oBrowser = Browser("version:=inter.*")
oBrowser.Navigate ""http://www.google.com"
Set oWebEdit = oBrowser.WebEdit("name:=q", "index:=0")
oWebEdit.Set "software inquisition"
oWebEdit.submit ''Page reloads
oBrowser.sync
'This resyncs oWebEdit
oWebEdit.init
'Now the next line will work
oWebEdit.Set "Bonnaroo"
--------------------------------------------------
Changing Time-out Options on Remote Machine [RDC]
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Control Panel\Desktop]
"ScreenSaverIsSecure"="0"
"ScreenSaveActive"="0"
"ScreenSaveTimeOut"="999999999"
--------------------------------------------------
Quick way to generate Unique Temporary files at Runtime
Many a times we need to create all sorts of temporary files with unique names; and after the test is over they are not useful anymore. But to do this we usually ending up writing a large code for creating a new folder, then doing a lots of checks, and then finally creating a file, and then using the FilePath where ever it is that we wanted!
A quicker way of doing this and avoiding all the creation and checks for new folders is by using the Current Report Path of the test during Runtime itself, and using the DotNETFactory utility
sFilePath = Reporter.ReportPath & "\" & DotNETFactory.CreateInstance("System.DateTime").Now.ToString("ddMMyyHHMMss") & ".png"
--------------------------------------------------
Variations of SendKeys Method
This code works and has been verified!
'Create DeviceReplay object
Set objSendKey = CreateObject("Mercury.DeviceReplay")
'Focusing on the Object by clicking it
Browser("Google").Page("Google").WebElement("WebElement").Click
'Call the required function
objSendKey.SendString("Hi, Your text goes here!!!")
'Release the object
Set objSendKey = Nothing
--------------------------------------------------
How to Clear or Delete text from Edit Box
When the Set method is used, it first clears the text in the EditBox and then writes new text in the EditBox
This may also be possible by the following code:
Browser("B").Page("P").WebEdit("E").Object.Clear
What should we do if we have to just Type next to next and not overwrite the text already present
--------------------------------------------------
Keyboard Status
The Devices.Keyboard class Provides properties for accessing the current state of the keyboard, such as what keys are currently pressed, and provides a method to send keystrokes to the active window.
How can we know if CAPS-LOCK already pressed?
Before we use the numpad keys we want to verify that if NUM-LOCK already pressed.
Set Keyboard = DotNetFactory.CreateInstance( "Microsoft.VisualBasic.Devices.Keyboard", "Microsoft.VisualBasic" )
Print CBool( Keyboard.AltKeyDown )
Print CBool( Keyboard.CapsLock )
Print CBool( Keyboard.CtrlKeyDown )
Print CBool( Keyboard.NumLock )
Print CBool( Keyboard.ScrollLock )
Print CBool( Keyboard.ShiftKeyDown )
Source: http://www.advancedqtp.com/2008/04/keyboard-status/
--------------------------------------------------
Explanation of a Click
Click is not a physical click, but its just a 'Return' Event, which is why even when we see that the button has been clicked, the button would not have been actually clicked, because qtp would have sent a Return which did not get registered.
So, we may be able to correct this by changing the Replay Type
--------------------------------------------------
Use OLE Objects to get number of pages in PDF
Function GetNumPagesInPDF(FileName)
Dim oPDFDoc
Set oPDFDoc = CreateObject( "AcroExch.PDDoc" )
If oPDFDoc.Open( FileName ) Then
GetNumPagesInPDF = oPDFDoc.GetNumPages()
Set oPDFDoc = Nothing
Else
GetNumPagesInPDF = -1
End If
End Function
numPages = GetNumPagesInPDF("C:\Program Files\Mercury\QuickTest Professional\help\QTUsersGuide.pdf")
MsgBox "Number of pages: " & numPages
--------------------------------------------------
Difference between Class Name, micClass, micclass, className
Class Name: When looking through the object spy on any object, we'll see the test-object property "Class Name", which always equals to the QTP-Type of that object. So for example, the Class Name of a browser is "Browser", the Class Name of a WinButton is "WinButton".
However, if you wish to use the "Class Name" property to identify objects with Descriptive Programming syntax, you'd have to use the property micclass.
So for example, this won't work:
Browser("Class Name:=Browser")
But this will:
Browser("micclass:=Browser")"
So, this takes case of Class Name and micclass, what about the plain old Class and className properties? These properties are usually unique to Web objects.
className is a Run-Time object property of Web objects. You can test it yourself: build a symple HTML file, and add class="Something" to one of the HTML nodes. When you'll load that HTML file in a browser, and use the object spy on the relevant web-object, you'll see className="Something" in the Run-Time tab of the object spy.
class is simply the Test-Object property which equals the Run-Time property of className. Meaning that oWebObject.GetROProperty("class") will be the same as oWebObject.Object.className. They represent the same inner data, once through the Run-Time world, and once through the Test-Object world.
--------------------------------------------------
Change the default directory for Tests path for QTP
Open the registry editor (Start -> Run -> type ""regedit"".)
Navigate to the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest
Find the TestsDirectory value. The value contains the path to the directory you want to be the default test script directory. Edit this value to have your desired path
Right click on TestsDirectory
Choose Modify from the menu
Enter the path into the Value data field
Click OK
Repeat steps 2 through 4 for the following key:
HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest
If the specified directory path does not exist, QuickTest Professional will open the "My Documents" directory by default.
You should not place a "\" at the end of the path.
--------------------------------------------------
Get Content from WinList Form
Window("Window").Activate
text= Window("Window").WinList("From").GetContent