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