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


Thursday, October 23, 2008

QuickTest Professional (QTP) Questions and Answers Part 2


  1. How to close all the Internet expolere windows and their dialog boxes
    SystemUtil.CloseProcessByName("IEXPLORE.exe")

  2. How to open the browser through script?
    a) To open Firefox
    SystemUtil.Run "firefox.exe", URL, , , SHOW_MAXIMIZED

    b) To open Internet Explorer
    SystemUtil.Run "iexplorer.exe", URL, , , SHOW_MAXIMIZED


  3. How to retrive the whole text of the page ?
    var = Browser("title:= title").page("title:=title").Object.documentElement.innertext

    OR

    var = Browser("title:= title").page("title:=title").WebElement("html tag:=BODY").getROProperty("innertext")


  4. How to add array and retrive the Dictionary Object ?
    Function DicDemo
    Dim a, d, i, s, arr(2) ' Create some variables.
    Set d = CreateObject("Scripting.Dictionary")
    arr(0)=0
    arr(1)=1
    arr(2)=2
    d.Add "Arr", arr
    a = d.Items ' Get the items.
    newarr=a(0)
    For i=0 to ubound(newarr)
    msgbox newarr(i)
    Next
    End Function

    Call DicDemo



  5. How to retrive the static text of the Dialog Box
    Dialog("text:=MyWindow").Static(nativeclass:=Static").GetROProperty("text")

    OR

    Dialog("text:=MyWindow").Static(nativeclass:=Static","Index:=0").GetROProperty("text")


  6. How to store the double quoted text in the variable ?
    strname= """Gaurang"""
    msgbox strname

    strname= chr(34)&"Gaurang"&chr(34)
    msgbox strname


  7. How to close all the opened browsers ?
    While Browser("CreationTime:=0").Exist
    Browser("CreationTime:=0").Close
    Wend

    Creation time is a special property associated with the Browser Object. The value of this property show the order in which the browser has opened. The First Browser that opens receives the Value 0. And so when you close the first Browser the value of this property in all the browser decrease by 1.