Monday, May 31, 2010

Overriding GetRowWithCellText Method

GetRowWithCellText method finds the specified text in a webtable and returns the number of the first row found.
If you want to find all matches in a webtable, then we have to override the GetRowWithCellText method using RegisterUserFunc as below,
RegisterUserFunc "WebTable","GetRowWithCellText","NewGetRowWithCellText"

Function NewGetRowWithCellText(obj,val1,val2,val3)
Dim v_st_returnText
Set objRow=obj.Object.getElementsByTagName("TR")
If val2<>0 Then val2=val2-1
If val3<>0 Then val3=val3-1
For i=val3 to objRow.Length-1
If val1=Trim(objRow(i).getElementsByTagName("TD")(val2).InnerText) Then
If v_st_returnText="" Then
v_st_returnText=Cstr( i+1)
Else
v_st_returnText=Cstr( i+1)+","+v_st_returnText
End If
End If
Next
NewGetRowWithCellText=v_st_returnText
End Function

Msgbox Browser("name:=QTP Codes").Page("title:=QTP Codes").WebTable("index:=0").GetRowWithCellText("Cell3",1,1)

Saturday, May 15, 2010

Quitting Excel Workbooks Using Getobject

Let’s consider that we have opened two excel workbooks using CreateObject. If you want to quit those two workbooks, try the below snippet.
Function fnc_CreateExcel
Set objNewExcel=CreateObject("Excel.Application")
objNewExcel.Visible=True
objNewExcel.Workbooks.Add
Set objNewExcel=Nothing
End Function

Call fnc_CreateExcel'Creating Book1
Call fnc_CreateExcel'Creating Book2

Call fnc_Quit_Excel("Book1")'Quitting Book 1
Call fnc_Quit_Excel("Book2")'Quitting Book 2

Function fnc_Quit_Excel(v_St_ExcelName)
Set objExcel=GetObject(v_St_ExcelName).Application'Getting Excel Object using Book name
objExcel.Quit
Set objExcel=Nothing
End Function

Friday, April 30, 2010

Capturing Bitmap When Text Is Mismatched

Whenever a text in a webpage is mismatched, you may use QTP report event to report the failure.
You can try the below snippet along with report event, the code captures and highlights the mismatched text in the webpage.
Set obj=Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("LABEL")(0)
vStrActualText= obj.innerText
vStrExpectedText="The Web"
If vStrActualText<>vStrExpectedText Then
obj.innerHTML=""&vStrActualText&""
Browser("name:=Google").Page("title:=Google").highlight
Browser("name:=Google").Page("title:=Google").CaptureBitmap "C:\TEST.PNG",True
obj.innerHTML=vStrActualText
End If
If your text is mismatched in the webpage, the script captures bitmap of the page.

Saturday, April 17, 2010

Finding Disabled Radio Buttons

The below snippet finds disabled radio button from a webradiogroup using DOM.
Set obj= Browser("name:=Google").Page("title:=Google").Object.getElementsByName("meta")
For i=0 to obj.Length-1
If obj(i).Type="radio" and obj(i).Disabled="True" Then
Msgbox "Radio " &i+1& " is disabled" 'This msgbox indicates the disabled radio button.
End if
Next