How to do Browser Back using RegisterUserFunc in QTP

Public Function MyBack() 
Set WshShell = CreateObject("WScript.Shell") 
WshShell.SendKeys "%{LEFT}" 
Set WshShell = Nothing 
Wait 2 
End Function 

RegisterUserFunc "Browser", "Back", "MyBack", True Browser().Back

 

How to Click on all links of a Page using QTP

'get the number of links
Set oLinks = Description.Create
oLinks("micclass").Value = "Link"
oLinks("html tag").Value = "A"

Set AllLinks = Browser("").Page("").ChildObjects(oLinks)

nLinks =AllLinks.count
For i = 1 to nLinks

oLinks("text").Value = i

'Put  your statements to test the Page here

'...

'...
Browser("").Page("").Link(oLinks).Click
Next

 

How to Compare text files

Set fso=createObject("Scripting.FileSystemObject") 
Set f = fso.OpenTextFile("D:\Ex.txt", 1)

Dim strLine, arrLine, bMatch

if f.AtEndOfStream <> True then   
strLine = f.ReadLine  
arrLine = split(strLine,",") 
end if

Set n = fso.OpenTextFile("D:\sample.txt", 1)

val=4 bMatch = 0

for i= 0 to val-1   
d=n.ReadLine   
if d <> arrLine(i) Then 
bMatch = 1
 Next

If bMatch = 1 then   
Msgbox "Compare - Failed" 
Else   
Msgbox "Compare - Passed" 
End if

How to find which version of vbscript you are using
Find vbscript.dll in c:\windows\system32. Rightclick and open properties window, find the version in Version tab
using Scripting :-

msgbox ScriptEngineMajorVersion &"."& ScriptEngineMinorVersion

How to Customize QTP Result

Function CustomResult(nStatus, sStep, sActual, sExpected)

Set oEventDesc = CreateObject("Scripting.Dictionary")   
oEventDesc("ViewType") = "Sell.Explorer.2"   
oEventDesc("Status") = nStatus   
oEventDesc("EnableFilter") = False   
oEventDesc("NodeName") = sStep

Dim sHTMLInfo   
sHTMLInfo = "<TABLE border='0'>"   
If nStatus = 1 Then    
sHTMLInfo = sHTMLInfo & "<TR><TD><font color='red' size='2'>Actual Result: " & sActual & "</font></TD></TR>"   
Else    
sHTMLInfo = sHTMLInfo & "<TR><TD><font color='green' size='2'>Actual Result: " & sActual & "</font></TD></TR>"   
End If

sHTMLInfo = sHTMLInfo & "<TR><TD><font color='blue' size='2'>Expected Result: " & sExpected & "</font></TD></TR></TABLE>"                      
oEventDesc("StepHtmlInfo") = sHTMLInfo  newEventContext = Reporter.LogEvent ("Replay",oEventDesc,Reporter.GetContext)

End Function

QTP  and Database

Function ConnectDB ( ConnectionString)

dim oConnection  on error Resume next

Set oConnection = CreateObject("ADODB.Connection")  oConnection.Open ConnectionString    If Err.Number <> 0 then   ConnectDB=  Err.Number & " : " & Err.Description   Err.clear   Exit Function  End If

Set ConnectDB = oConnection

End Function
Function getRecordset(strSQL)

Dim oConnection, oRecordSet

Set oConnection = CreateObject("ADODB.Connection")

Set oRecordSet = CreateObject("ADODB.Recordset")

oConnection = getConnection()

oRecordSet.Open strSQL,oConnection,adOpenStatic

Set getRecordset = oRecordSet

End Function
Function CloseConnection ( byRef ConnectSession )

ConnectSession.close

set ConnectSession = Nothing

End Function

 

Function ExecuteQuery(byRef ConnectSession, SQLQuery)

set rs = ConnectSession.Execute( SQL )

set ExecuteQuery = rs

End Function

 

Function getRecordCount(ByRef RecordSet)

Dim Rows  Rows = 0

RecordSet.MoveFirst  Do Until RecordSet.EOF   Rows = Rows+1   RecordSet.MoveNext  Loop  getRecordCount = Rows

End Function

How to Connect Excel using ADODB

Dim cn
Set cn = CreateObject("ADODB.Connection")
cn.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=D:\My QTP\ExcelADO\Test.xls; ReadOnly=False;"
Set rs = cn.Execute ("update [sheet1$] set username = 'saket1' where key = 1")
Set rs = cn.Execute ("select * from [sheet1$] where key = 1")
msgbox rs.Fields("UserName").Value
See also  is your QTP Script execution slower??

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.