Monday, January 19, 2009

How to retrieve tooltip in QTP

Downalod the Windows Application

Couple of days ago I was visiting the SQA forum and there someone have posted the question to retrieve the tooltip of the web button. And so I find out the couple of different way to retrieve the tooptip for web objects.

Retrieve the tooltip of the Web objects:

There isn't any direct property of any of the web object that gives you the tooltip. But if you will see the source code of the object you will find out that it is the title property of th object which appears as the tooltip.

Below are the couple of ways to retrieve the tooltip.


1)Retrive the tooltip from outerhtml property

The title property resides in the OuterHtml property of any object. So what i did is, i made a function to fetch the title property as below.
Example:
This retrives the tooltip of the Edit box appears on the "Google.com" website. The same way you can retrieve the tooltip of web buttons.




SystemUtil.Run "iexplore.exe","Google.com"
outerhtml = Browser("Title:=Google").page("title:=google").webedit("name:=q").GetROProperty("outerhtml")
msgbox Find_ToolTip( outerhtml )

Function Find_ToolTip(outerhtml)
temp = RegExp("title=.*""",outerhtml)
Find_ToolTip = RegExp(""".*""",temp)
End Function


Function RegExp(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
If Matches.count > 0 Then
RegExp = Matches(0).Value
else
REgExp=""
End If

End Function


Yeah yeah yeah... I know this is the tedious way to retrieve the tooltip. So Then something else comes in my mind and I found out the eaistest way to retreive it.

2) Retrieve tooltip using DOM.

You can easily retrieve the title property of the object using DOM.

Example:
msgbox Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Object.title


Retrieve the tooltip of the Windows objects:

Retrieve tooltip of the windows object is easy too if you know it's class name. But you will not be able to retrieve the class name through the Object Spy that comes with the QTP.

The tooltip is windows of type tooltips_class32.

Example:
You need to download the gToolTip_Demo.zip and extract the exe file gTooltip.exe to run this example



Dialog("text:=Tool Tip Demo").WinButton("text:=OK").MouseMove 0,0
msgbox Window("nativeclass:=tooltips_class32").GetROProperty("text")

Dialog("text:=Tool Tip Demo").WinButton("text:=Cancel").MouseMove 0,0
msgbox Window("nativeclass:=tooltips_class32").GetROProperty("text")

Dialog("text:=Tool Tip Demo").WinEdit("attached text:=Password").MouseMove 0,0
msgbox Window("nativeclass:=tooltips_class32").GetROProperty("text")

Dialog("text:=Tool Tip Demo").WinEdit("attached text:=User ID","micClass:=WinEdit").MouseMove 0,0
msgbox Window("nativeclass:=tooltips_class32").GetROProperty("text")


Thursday, November 27, 2008

Search and Highlight the word in Browser

Currently I was working on the project which is a website localized in three different languages. And Client frequently ask as to change the words in the website. He shows as two or three instances but expect the changes to be take place in whole website. Now to manually navigate to the each page one by one and to see whether the old word has been replace or not is a cumbersome process. So I decided to write down the script in QTP. To Search the word in the web page was easy but then I require to highlight the word and take the screenshot of it. And that require to do some sort of goggling and posting the question on forum, but finally I got the answer.

So here is the script that will find the word and if it found will highlight the word and will take the screenshot


For row=1 to DataTable.GetSheet("Action1").GetRowCount()

browser("name:=.*Google.*").Navigate(IP&DataTable("Location",dtLocalSheet))
Call Search("Date")
DataTable.SetCurrentRow(row)

Next

Function Search( Word )

Page_Content = browser("name:=.*Google.*").Page("micClass:=Page").GetROProperty("innertext")
Loc = InStr(1,Page_Content,Word,1)
If Loc > 0Then

'Word Found
DataTable("Result",dtLocalSheet)= "Found"
HighLight (Word)

Else
'Word NOT Found
DataTable("Result",dtLocalSheet) = "NOT Found"

End If
End Function

Function HighLight(word)

Dim objPage
strHighlighttext = word
Set objPage = Browser("name:=.*Google.*").Page("micClass:=Page").Object
strHTML = objPage.body.innerHTML
str = Replace(strHTML,strHighlighttext,"" & strHighlighttext & "",1,1)
objPage.body.innerHTML = str
Browser("name:=.*Google.*").Page("micClass:=Page").CaptureBitmap Environment("TestDir")&"\"&File_Name&".png",true
File_Name = File_Name +1

End Function