For QTP Scripts & Documents Visit: www.gcreddy.
com
QTP Web Script Examples
'On error resume next
Dim strMainLink, strChildLink, strActualUrl, strTargetUrl
Dim intBCount, objBCount
'___________________________________________________________________
_______________________________________________
'Clear the Cookies
Call ClearCookies
'___________________________________________________________________
_______________________________________________
'select Environment and get related info from spread sheet
Dim strEnvrnName
Dim strServerName
Dim strUrl
ipEnvrnName = "prod" 'InputBox ("Please enter DB Environment (ex:
qap/beta/prod)","Database Environment","prod")
ipEnvrnName = lcase(trim(ipEnvrnName))
If ipEnvrnName = "" Then
msgbox "Enter DB Environment Name"
ExitTest
End If
Call DBEnvrnListFile(ipEnvrnName)
strEnvrnName = Environment.Value("EnvrnName")
strServerName = Environment.Value("ServerName")
strUrl = Environment.Value("Url")
If strEnvrnName = "" Then
For Software Testing Documents visit: ww.gcreddy.net 1
For QTP Scripts & Documents Visit: www.gcreddy.com
msgbox "Please valid DB Environment Name"
ExitTest
End If
'___________________________________________________________________
_______________________________________________
'get login details
Call LoginInfo()
strComputerName = Environment.Value("envComputerName")
strUserName = Environment.Value("envUserName")
strUserDomain = Environment.Value("envUserDomain")
'___________________________________________________________________
________________________________________________
Set objBrwsr=Description.Create
objBrwsr("micclass").value="Browser"
objBrwsr("name").value=".*"
Set objLink=Description.Create
objLink("html tag").value="A"
objLink("micclass").value="Link"
SystemUtil.Run "iexplore.exe",strUrl & "/home-finance/"
Wait(10)
strOutputFilePath = "C:\Move\Retail\TestData\Output\Global\Retail Test Log.xls"
Set objBCount = Desktop.ChildObjects(objBrwsr)
For Software Testing Documents visit: ww.gcreddy.net 2
For QTP Scripts & Documents Visit: www.gcreddy.com
intBCount = objBCount.Count-1
If Browser("creationtime:=" & intBCount).Link("name:=Insurance","index:=0").Exist
= False Then
Reporter.ReportEvent micFail,"HF_Insurance","Link is not available"
ExitTest
End If
'___________________________________________________________________
________________________________________________
'clearing variables
strBreadCrumb="":strMainLink="":strChildLink="":strActualUrl="":strTargetUrl="":st
rStatus=""
intfailCount = 0
'___________________________________________________________________
________________________________________________
strMainLink = Browser("creationtime:=" &
intBCount).Link("name:=Insurance","index:=0").GetROProperty("text")
strChildLink = ""
strActualUrl = Browser("creationtime:=" &
intBCount).Link("name:=Insurance","index:=0").GetROProperty("url")
Browser("creationtime:=" & intBCount).Link("name:=Insurance","index:=0").Click
Wait(5)
strTargetUrl = Browser("creationtime:=" & intBCount).GetROProperty("url")
strBreadCrumb = Browser("creationtime:=" &
intBCount).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
'verify the link
If Instr(strActualUrl,strTargetUrl ) <=0 Then
intfailCount = intfailCount +1
strStatus = "Fail"
For Software Testing Documents visit: ww.gcreddy.net 3
For QTP Scripts & Documents Visit: www.gcreddy.com
LogFile()
End If
'___________________________________________________________________
________________________________________________
Set ObjWE=Description.Create
ObjWE("class").value="hf_Insurance_Outerwraper"
ObjWE("index").value=0
ObjWE("html tag").value="DIV"
ObjWE("micclass").value="WebElement"
If Browser("creationtime:=" & intBCount).WebElement(objWE).Exist Then
intLinksCount1 = Browser("creationtime:=" &
intBCount).WebElement(ObjWE).ChildObjects(ObjLink).Count
For intCount1 = 0 to intLinksCount1 -1
'______________________________________________________________
'clearing variables
strBreadCrumb="":strChildLink="":strActualUrl="":strTargetUrl="":strStatus=""
'______________________________________________________________
Set objBrowser1 = Browser("creationtime:=" &
intBCount).WebElement(ObjWE).ChildObjects(ObjLink)
strChildLink = trim(objBrowser1(intCount1).GetROProperty("text"))
strActualUrl = objBrowser1(intCount1).GetROProperty("url")
strTarget = objBrowser1(intCount1).GetROProperty("target")
If mid(strActualUrl,1,10) <> "javascript"
Then
For Software Testing Documents visit: ww.gcreddy.net 4
For QTP Scripts & Documents Visit: www.gcreddy.com
objBrowser1(intCount1).Click
'if it opens in new browser
If strTarget = "_blank" Then
Set objBCount1 = Desktop.ChildObjects(objBrwsr)
intBCount1 = objBCount1.Count - 1
Set objBCount1 = Nothing
strTargetUrl = Browser("creationtime:=" &
intBCount1).GetROProperty("url")
If Browser("creationtime:=" &
intBCount1).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
&
intBCount1).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" & intBCount1).Close
Else
strTargetUrl = Browser("creationtime:=" &
intBCount).GetROProperty("url")
If Browser("creationtime:=" &
intBCount).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
& intBCount).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" &
intBCount).Back
For Software Testing Documents visit: ww.gcreddy.net 5
For QTP Scripts & Documents Visit: www.gcreddy.com
End If
'verify the link
If Instr(strActualUrl,strTargetUrl) <= 0 Then
intfailCount = intfailCount +1
strStatus = "Fail"
LogFile()
End If
End If
Next
End If
'___________________________________________________________________
________________________________________________
Set ObjWE1=Description.Create
ObjWE1("class").value="hf_Insurance_Outerwraper"
ObjWE1("index").value=1
ObjWE1("html tag").value="DIV"
ObjWE1("micclass").value="WebElement"
If Browser("creationtime:=" & intBCount).WebElement(objWE1).Exist
Then
intLinksCount1 = Browser("creationtime:=" &
intBCount).WebElement(objWE1).ChildObjects(ObjLink).Count
For intCount1 = 0 to intLinksCount1 -1
'______________________________________________________________
'clearing variables
For Software Testing Documents visit: ww.gcreddy.net 6
For QTP Scripts & Documents Visit: www.gcreddy.com
strBreadCrumb="":strChildLink="":strActualUrl="":strTargetUrl="":strStatus=""
'______________________________________________________________
Set objBrowser1 = Browser("creationtime:=" &
intBCount).WebElement(objWE1).ChildObjects(ObjLink)
strChildLink = trim(objBrowser1(intCount1).GetROProperty("text"))
strActualUrl = objBrowser1(intCount1).GetROProperty("url")
strTarget = objBrowser1(intCount1).GetROProperty("target")
If mid(strActualUrl,1,10) <> "javascript"
Then
objBrowser1(intCount1).Click
'if it opens in new browser
If strTarget = "_blank" Then
Set objBCount1 = Desktop.ChildObjects(objBrwsr)
intBCount1 = objBCount1.Count - 1
Set objBCount1 = Nothing
strTargetUrl = Browser("creationtime:=" &
intBCount1).GetROProperty("url")
If Browser("creationtime:=" &
intBCount1).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
&
intBCount1).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" & intBCount1).Close
Else
For Software Testing Documents visit: ww.gcreddy.net 7
For QTP Scripts & Documents Visit: www.gcreddy.com
strTargetUrl = Browser("creationtime:=" &
intBCount).GetROProperty("url")
If Browser("creationtime:=" &
intBCount).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
& intBCount).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" &
intBCount).Back
End If
'verify the link
If Instr(strActualUrl,strTargetUrl) <= 0 Then
intfailCount = intfailCount +1
strStatus = "Fail"
LogFile()
End If
End If
Next
End If
'___________________________________________________________________
________________________________________________
Set ObjWE2=Description.Create
ObjWE2("class").value="hf_Insurance_Outerwraper"
ObjWE2("index").value=2
ObjWE2("html tag").value="DIV"
For Software Testing Documents visit: ww.gcreddy.net 8
For QTP Scripts & Documents Visit: www.gcreddy.com
ObjWE2("micclass").value="WebElement"
If Browser("creationtime:=" & intBCount).WebElement(objWE2).Exist
Then
intLinksCount1 = Browser("creationtime:=" &
intBCount).WebElement(objWE2).ChildObjects(ObjLink).Count
For intCount1 = 0 to intLinksCount1 -1
'______________________________________________________________
'clearing variables
strBreadCrumb="":strChildLink="":strActualUrl="":strTargetUrl="":strStatus=""
'______________________________________________________________
Set objBrowser1 = Browser("creationtime:=" &
intBCount).WebElement(objWE2).ChildObjects(ObjLink)
strChildLink = trim(objBrowser1(intCount1).GetROProperty("text"))
strActualUrl = objBrowser1(intCount1).GetROProperty("url")
strTarget = objBrowser1(intCount1).GetROProperty("target")
If mid(strActualUrl,1,10) <> "javascript"
Then
objBrowser1(intCount1).Click
'if it opens in new browser
If strTarget = "_blank" Then
Set objBCount1 = Desktop.ChildObjects(objBrwsr)
intBCount1 = objBCount1.Count - 1
Set objBCount1 = Nothing
strTargetUrl = Browser("creationtime:=" &
intBCount1).GetROProperty("url")
For Software Testing Documents visit: ww.gcreddy.net 9
For QTP Scripts & Documents Visit: www.gcreddy.com
If Browser("creationtime:=" &
intBCount1).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
&
intBCount1).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" & intBCount1).Close
Else
strTargetUrl = Browser("creationtime:=" &
intBCount).GetROProperty("url")
If Browser("creationtime:=" &
intBCount).WebElement("class:=slideBreadcumbLine").Exist Then
strBreadCrumb = Browser("creationtime:="
& intBCount).WebElement("class:=slideBreadcumbLine").GetROProperty("outertext")
Else
strBreadCrumb = "Not Available"
End If
Browser("creationtime:=" &
intBCount).Back
End If
'verify the link
If Instr(strActualUrl,strTargetUrl) <= 0 Then
intfailCount = intfailCount +1
strStatus = "Fail"
LogFile()
End If
End If
For Software Testing Documents visit: ww.gcreddy.net 10
For QTP Scripts & Documents Visit: www.gcreddy.com
Next
End If
'___________________________________________________________________
________________________________________________
'Close the Browsers and advertisement windows
Set objBCount2 = Desktop.ChildObjects(objBrwsr)
intBCount2 = objBCount2.Count - 1
For I = intBCount to intBCount2
Browser("creationtime:=" & intBCount).Close
Next
'___________________________________________________________________
________________________________________________
Set objMainLink = Nothing
Set objChildLink = Nothing
Set objBrwsr = Nothing
Set ObjWE = Nothing
Set ObjWE1 = Nothing
Set objBCount = nothing
Set objBCount1 = Nothing
'___________________________________________________________________
________________________________________________
'To send Email when test failed
If intfailCount > 0 Then
SendMail
End If
'___________________________________________________________________
________________________________________________
For Software Testing Documents visit: ww.gcreddy.net 11
For QTP Scripts & Documents Visit: www.gcreddy.com
''SubRoutine to CreateLogFile
Sub LogFile
Set objExcel=CreateObject("Excel.Application")
objExcel.visible=False
objExcel.workbooks.open(strOutputFilePath)
r=1
Do Until len(objExcel.cells(r,1))=0
r=r+1
Loop
objExcel.cells(r,1).value=strBreadCrumb
objExcel.cells(r,2).value=strMainLink
objExcel.cells(r,3).value=strChildLink
objExcel.cells(r,4).value=strActualUrl
objExcel.cells(r,5).value=strTargetUrl
objExcel.cells(r,6).value=Date
objExcel.cells(r,7).value= Time
objExcel.cells(r,8).value=strEnvrnName
objExcel.cells(r,9).value=strServerName
objExcel.cells(r,10).value=strComputerName
objExcel.cells(r,11).value=strUserName
objExcel.cells(r,12).value=strUserDomain
objExcel.cells(r,13).value=strStatus
objExcel.DisplayAlerts = False
objExcel.Save
objExcel.Quit
For Software Testing Documents visit: ww.gcreddy.net 12
For QTP Scripts & Documents Visit: www.gcreddy.com
Set objExcel = Nothing
End Sub
'___________________________________________________________________
________________________________________________
'SubRoutine to Send Mail
Sub SendMail
msgSub = "2 Step_Geo_Mileage Pattern Search:" &vbcrLf &"One or more of
the Test Cases Failed." &vbcrLf &"See attachment for details."
doc=strOutputFilePath
Set objConf = CreateObject("CDO.Configuration")
cdoSendUsingPort=2
sMailServerName="smtp.phx.move.com"
cdoAnonymous=cdoNONE
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusing") =
cdoSendUsingPort
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
sMailServerName
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpauthentica
te") = cdoAnonymous 'cdoBasic
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") =
False
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpor
t") = 25
'objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusername
") = "corp\GEdamadaka"
For Software Testing Documents visit: ww.gcreddy.net 13
For QTP Scripts & Documents Visit: www.gcreddy.com
'objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendpassword"
) = "$lgs0112"
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectio
ntimeout") = 60
objConf.fields.Update
Set objMsg = CreateObject("CDO.Message")
objMsg.Configuration = objConf
objMsg.Subject = "~2 Step_Geo_Mileage Pattern Search"
objMsg.TextBody =msgSub
objMsg.AddAttachment(doc)
objMsg.Send
Set objMsg = Nothing
NewMail = True
End Sub
####################################################
QTP Information:
www.gcreddy.com
For Manual Testing:
For Software Testing Documents visit: ww.gcreddy.net 14
For QTP Scripts & Documents Visit: www.gcreddy.com
www.gcreddy.net
For Software Testing Documents visit: ww.gcreddy.net 15