31
FILE SYSTEM OBJECTS EXAMPLES Examples: i) Create a Folder Dim objFso Set objFso=CreateObject(“scripting.FileSystemObject”) objFso.CreateFolder “C:Documents and Settings1 Desktophyderabad” ii) Check if the Folder Exist or not? If not create the Folder Dim objFso, myFolder myFolder=”C:Documents and Settings1 Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”) If Not objFso.FolderExists(myFolder) Then objFso.CreateFolder (myFolder) End If iii) Copy a Folder Dim objFso, myFolder myFolder=”C:Documents and Settings1 Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”) objFso.CopyFolder myFolder,”E:abcd” iv) Delete a folder Dim objFso, myFolder myFolder=”C:Documents and Settings1Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”)

Qtp Imp Script Examples

Embed Size (px)

Citation preview

Page 1: Qtp Imp Script Examples

FILE SYSTEM OBJECTS EXAMPLES

Examples:

i) Create a Folder

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

objFso.CreateFolder “C:Documents and Settings1 Desktophyderabad”

ii) Check if the Folder Exist or not? If not create the Folder

Dim objFso, myFolder

myFolder=”C:Documents and Settings1 Desktophyderabad”

Set objFso=CreateObject(“scripting.FileSystemObject”)

If Not objFso.FolderExists(myFolder)  Then

objFso.CreateFolder (myFolder)

End If

iii) Copy a Folder

Dim objFso, myFolder

myFolder=”C:Documents and Settings1 Desktophyderabad”

Set objFso=CreateObject(“scripting.FileSystemObject”)

objFso.CopyFolder myFolder,”E:abcd”

iv) Delete a folder

Dim objFso, myFolder

myFolder=”C:Documents and Settings1Desktophyderabad”

Set objFso=CreateObject(“scripting.FileSystemObject”)

objFso.DeleteFolder( myFolder)

2nd

Dim objFso, myFolder

Page 2: Qtp Imp Script Examples

myFolder=”C:Documents and Settings1Desktophyderabad”

Set objFso=CreateObject(“scripting.FileSystemObject”)

If objFso.FolderExists(myFolder) Then

objFso.DeleteFolder( myFolder)

End If

v)  Return a Collection of Disk Drives

Dim objFso, colDrives

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set colDrives=objFso.Drives

For Each oDrive in colDrives

Msgbox oDrive

Next

vi) Get available space on a Drive

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myDrive=objFso.GetDrive(“D:”)

Msgbox myDrive.AvailableSpace/(1024^3) & ” GB”

vii) Creating a Text File

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.txt”)

objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.doc”)

objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.xls”)

objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.pdf”)

Note: We can Create other files also, but they act as Text/Flat Files

viii)  Check if the File Exist or not? If not create the File

Dim objFso, myFile1,myFile2, myFile3, myFile4

Page 3: Qtp Imp Script Examples

myFile1=”C:Documents and Settings1 Desktopabcd.txt”

myFile2=”C:Documents and Settings1 Desktopabcd.doc”

myFile3=”C:Documents and Settings1 Desktopabcd.xls”

myFile4=”C:Documents and Settings1 Desktopabcd.pdf”

Set objFso=CreateObject(“scripting.FileSystemObject”)

If Not objFso.FileExists(myFile1) Then

objFso.CreateTextFile (myFile1)

End If

If Not objFso.FileExists(myFile2) Then

objFso.CreateTextFile (myFile2)

End If

If Not objFso.FileExists(myFile3) Then

objFso.CreateTextFile (myFile3)

End If

If Not objFso.FileExists(myFile4)  Then

objFso.CreateTextFile (myFile4)

End If

ix) Read Data Character by Character from a text file

Dim objFso, myFile, myChar

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append

Do Until myFile.AtEndOfStream=True

myChar=myFile.Read(1)

Msgbox myChar

Loop

myFile.Close

Set objFso=Nothing

Page 4: Qtp Imp Script Examples

x)Read Line by Line from a Text File

Dim objFso, myFile, myChar

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append

Do Until myFile.AtEndOfStream=True

myChar=myFile.ReadLine

Msgbox myChar

Loop

myFile.Close

Set objFso=Nothing

xi) Data Driven Testing by fetching Test data directly from a Text file.

‘*************************************************************************************

‘Test Requirement: Data Driven Testing by fetching Test data directly from a Text file.

‘Author: xyz

‘Date of Creation: 24-08-2010

‘Pre-requasites:

‘abcd.txt (Test Data File)

‘Test Flow:

‘Create File System object

‘Open the file with Read mode and store reference into a variable

‘Skipe the first line

‘Read line by line and split the Data

‘Login Operation

‘Form Looping and pass Parameters

‘*************************************************************************************

Page 5: Qtp Imp Script Examples

Dim objFso, myFile, myLine, myField

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append

myFile.SkipLine

Do Until myFile.AtEndOfStream =True

myLine=myFile.ReadLine

myField=Split(myLine,”,”)

SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”

Dialog(“text:=Login”).Activate

Dialog(“text:=Login”).WinEdit(“attached text:=Agent Name:”).Set myField(0)

Dialog(“text:=Login”).WinEdit(“attached text:=Password:”).Set myField(1)

Wait 2

Dialog(“text:=Login”).WinButton(“text:=OK”).Click

Window(“text:=Flight Reservation”).Close

Loop

myFile.Close

Set objFso=Nothing

xii) Write Data to a Text File

Dim objFso, myFile, Result, a, b

a=10: b=20

Result=a+b

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,2) ’1 for Read, 2 for Write and 8 for Append

myFile.WriteLine “Addition of a, b is: “&Result

myFile.Close

Set objFso=Nothing

Page 6: Qtp Imp Script Examples

xiii) Delete a Text File

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

objFso.DeleteFile(“C:Documents and Settings1 Desktopabcd.doc”)

Set objFso=Nothing

xiv) Check if the File Exists or not? If Exists delete the File

———–

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

If objFso.FileExists(“C:Documents and Settings1 Desktopabcd.pdf”) Then

objFso.DeleteFile(“C:Documents and Settings1 Desktopabcd.pdf”)

End If

Set objFso=Nothing

xv) Calculate size of a Text File

Dim objFso

Set objFso=CreateObject(“scripting.FileSystemObject”)

File_Size= objFso.GetFile(“C:Documents and Settings1 Desktopabcd.txt”).Size

Msgbox File_Size& ” Bytes”

Set objFso=Nothing

xvi)Compare Two Text File by Size, by Text and by Binary values

Option Explicit

Dim objFso, File1, File2, myFile1, myFile2, File_First, File_Second, Files_Compare

File1=”C:Documents and Settings1 abcd.txt”

File2=”C:Documents and Settings1 desktopxyz.txt”

Set objFso=CreateObject(“scripting.FileSystemObject”)

‘Comaring two text files by Size

If objFso.GetFile(File1).Size= objFso.GetFile(File2).Size Then

Page 7: Qtp Imp Script Examples

Msgbox “Files are Same in Size”

Else

Msgbox “Files are Not Same”

End If

‘Comaring two text files by Text

Set File_First=objFso.OpenTextFile(File1)

Set File_Second=objFso.OpenTextFile(File2)

myFile1=File_First.ReadAll

myFile2=File_Second.ReadAll

‘Msgbox myFile1

Files_Compare=strComp(myFile1,myFile2,1) ’1 for Texual Comparision

If  Files_Compare=0 Then

Msgbox “Files are having Same Text”

Else

Msgbox “Files are having Different Text”

End If

‘Binary Comparision of Two Text Files

Files_Compare=strComp(myFile1,myFile2,0) ’0 for Binary Comparision (It is Default mode)

If  Files_Compare=0 Then

Msgbox “Files are Equal”

Else

Msgbox “Files are Not Equal”

End If

Set objFso=Nothing

xvii) Count the number of times a word appears in a Text File

Option Explicit

Dim objFso, File1, myWord, myData, myFile, objRegEx,  MatchesFound, TotMatches

Page 8: Qtp Imp Script Examples

File1=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(File1)

myData=myFile.ReadAll

myWord=”QTP”

Set objRegEx= New RegExp ‘Creating Regular Expression Object

objRegEx.Pattern=myWord ‘Search string

objRegEx.Global=True ‘ Finding all Matches

objRegEx.IgnoreCase=True ‘ Ignoring Case

Set MatchesFound=objRegex.Execute(myData) ‘Executing the Total file data to find natches

TotMatches=MatchesFound.Count

Msgbox “Matches: “&TotMatches

Set objFso=Nothing

xviii)  Capture all Button Names from Login dialog Box  and Export to a Text File

Option Explicit

Dim objFso, FilePath, myFile, oButton, myButton, Buttons, i, TotButtons

FilePath=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”

Set objFso=CreateObject(“scripting.FileSystemObject”)

Set myFile=objFso.OpenTextFile(FilePath,2)

myFile.WriteLine “Button Names”

myFile.WriteLine “————”

Set oButton=Description.Create

oButton(“micclass”).value=”WinButton”

SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”

Set Buttons=Dialog(“text:=Login”).ChildObjects(oButton)

TotButtons=Buttons.Count

For i= 0 to TotButtons-1 Step 1

Page 9: Qtp Imp Script Examples

myButton=Buttons(i).GetRoProperty(“text”)

myFile.WriteLine myButton

Next

myFile.Close

Set objFso=Nothing

xix) Capture Customer Names from 1 to 10 Orders in FR and export to a Text File

*****************************************************************

‘Test Requirement: Capture Customer names from 1 to 10 orders

‘and export to text file

‘Test Flow:

‘Create an object in File system class

‘Open the text file in write mode using File sytem object

‘Login Operation

‘Form Loop to open 1 to 10 orders

‘capture the Customer names and write to external text file

‘*****************************************************************

Dim objFso, myFile

Set objFso=CreateObject(“scripting.FilesystemObject”)

Set myFile=objFso.OpenTextFile(“C:Documents and Settingsgcr.GCRC-9A12FBD3D9Desktopabc.txt”,2)

myFile.WriteLine “Customer Names”

myFile.WriteLine “———”

If Not Window(“Flight Reservation”).Exist(3) Then

SystemUtil.Run “C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe”,”",”C:Program FilesHPQuickTest Professionalsamplesflightapp”,”open”

Dialog(“Login”).Activate

Dialog(“Login”).WinEdit(“Agent Name:”).Set “nagesh”

Page 10: Qtp Imp Script Examples

Dialog(“Login”).WinEdit(“Password:”).SetSecure “4c9e05a626f9b6471971fb15474e791b28cc1ed0″

Dialog(“Login”).WinButton(“OK”).Click

End If

For Order_Number= 1 to 10 step 1

Window(“Flight Reservation”).Activate

Window(“Flight Reservation”).WinButton(“Button”).Click

Window(“Flight Reservation”).Dialog(“Open Order”).WinCheckBox(“Order No.”).Set “ON”

Window(“Flight Reservation”).Dialog(“Open Order”).WinEdit(“Edit”).Set Order_Number

Window(“Flight Reservation”).Dialog(“Open Order”).WinButton(“OK”).Click

wait 2

Customer_Name = Window(“Flight Reservation”).WinEdit(“Name:”).GetVisibleText()

myFile.WriteLine Customer_Name

Next

myFile.Close

Set objFso=Nothing

Page 11: Qtp Imp Script Examples

HOW TO WORK WITH EXCEL

'''Script to create a new excel file , write data

'''save the file with read and write protected'''''pwd1 is for read protected pwd2 is for write protected

Set xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Addxl.DisplayAlerts=False Set ws=wb.Worksheets("sheet1")ws.cells(1,1)=100ws.cells(1,2)=200wb.Saveas "e:\data2.xls",,"pwd1","pwd2"wb.CloseSet xl=nothing

'''Script to open excel file ,which is read and write protected write data'''''pwd1 is for read protected pwd2 is for write protected

Set xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\data2.xls",0,False,5,"pwd1","pwd2")xl.DisplayAlerts=False Set ws=wb.Worksheets("sheet1")ws.cells(1,2)="hello"ws.cells(2,2)="new data"wb.Savewb.CloseSet xl=nothing ''Script to get the list of links in Google and do spell check===============================================================dim dset mw=CreateObject("Word.Application")set d=Description.Created("micclass").value="Link"set a=Browser("Google").page("Google").childobjects(d)for i=0 to a.count-1 mw.WordBasic.filenew s=a(i).getROProperty("innertext")mw.WordBasic.insert s if mw.ActiveDocument.Spellingerrors.count>0 then Reporter.ReportEvent 1,"Spelling","spelling error :"&s end ifmw.ActiveDocument.Close(False)next

Page 12: Qtp Imp Script Examples

mw.quitset mw=nothing=========================================''''Script to check ON the checkboxes in yahoo mail inbox=========================================Dim dSet d=Description.Created("micclass").value="WebCheckBox"Set c=Browser("Inbox (17) - Yahoo! Mail").Page("Inbox (17) - Yahoo! Mail").ChildObjects(d)For i=1 to 10c(i).set "ON"Next========================================'''script to select a mail having subject 'hi' or 'HI'========================================n=Browser("yahoo").Page("yahoo").WebTable("Inbox").RowCountFor i=2 to ns=Browser("yahoo").Page("yahoo").WebTable("Inbox").GetCellData(i,7)If lcase(trim(s))="hi" ThenBrowser("yahoo").Page("yahoo").WebCheckBox("index:="&i-1).set "ON"End IfNext========================================'''''Function to send a mail========================================Function SendMail(SendTo, Subject, Body, Attachment) Set otl=CreateObject("Outlook.Application")Set m=otl.CreateItem(0) m.to=SendTo m.Subject=Subject m.Body=Body If (Attachment <> "") Then Mail.Attachments.Add(Attachment)End Ifm.Send otl.Quit Set m = Nothing Set otl = NothingEnd FunctionCall SendMail("[email protected]","hi","This is test mail for testing","")'''''''''''''''create a new text file=====================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.CreateTextFile("e:\file1.txt")

Page 13: Qtp Imp Script Examples

f.WriteLine "hello"f.WriteLine "this is sample data"f.CloseSet fs=nothing====================================================='''''''''''''''read data from a text file=====================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.OpenTextFile("e:\file1.txt",1)While f.AtEndOfLine<>Truemsgbox f.ReadLineWendf.CloseSet fs=nothing=====================================================''''''''''create a new excel file and write data=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.AddSet ws=wb.Worksheets("sheet1")ws.cells(1,1)=10ws.cells(2,1)=20ws.cells(3,1)=50wb.SaveAs "e:\file1.xls"wb.CloseSet xl=nothing====================================================='''''''open existing file and write data in second column in Sheet1=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\file1.xls")Set ws=wb.Worksheets("sheet1")ws.cells(1,2)="Testing"ws.cells(2,2)="hyd"ws.cells(3,2)="ap"wb.Savewb.CloseSet xl=nothing====================================================='''''''''''read data from excel from rows and columns=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")

Page 14: Qtp Imp Script Examples

Set wb=xl.Workbooks.Open("e:\file1.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to r v="" For j=1 to c v=v&" "& ws.cells(i,j) Next print v print "-----------------------"Nextwb.CloseSet xl=nothing======================================================''''''''''''''''get the bgcolor in a cell in excel======================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\file3.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to rFor j=1 to c x=ws.cells(i,j).interior.colorindexmsgbox xNextNextwb.CloseSet xl=nothing=======================================================''''''''''''''''''''''create word and write data=======================================================dim mwset mw=CreateObject("Word.Application")mw.Documents.Addmw.selection.typetext "hello"mw.ActiveDocument.SaveAs "e:\file1.doc"mw.quitset mw=nothing=======================================================''''''''''script will display all the doc files in all the drives in the system========================================================Dim mwSet mw=CreateObject("Word.Application")

Page 15: Qtp Imp Script Examples

Set fs=createobject("Scripting.FileSystemObject")Set d=fs.Drivesmw.FileSearch.FileName="*.doc"For each dr in dmsgbox drmw.FileSearch.LookIn=drmw.FileSearch.SearchSubFolders=Truemw.FileSearch.Execute For each i in mw.FileSearch.FoundFiles print i Set f=fs.GetFile(i)print f.Name&" "&f.Size&" "&f.DateCreated print "-------------------------------------------------------------------"NextNextmw.Quit=========================================================='''''''''Open Internet Explorer and navigate to yahoomail==========================================================Dim ieSet ie=CreateObject("InternetExplorer.Application")ie.Visible=Trueie.Navigate "www.yahoomail.com"x=Browser("CreationTime:=0").GetROProperty("title")msgbox x==========================================================''''''Create word, Create table and write all the services names==========================================================Set mw = CreateObject("Word.Application")mw.Visible = TrueSet dc = mw.Documents.Add()Set objRange = dc.Range()dc.Tables.AddobjRange,1,3Set objTable = dc.Tables(1)x=1strComputer = "."Set wms=GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colItems = wms.ExecQuery("Select * from Win32_Service")For Each s in colItemsIf x > 1 Then objTable.Rows.Add() End If objTable.Cell(x, 1).Range.Font.Bold = True objTable.Cell(x, 1).Range.Text = s.NameobjTable.Cell(x, 2).Range.text = s.DisplayName

Page 16: Qtp Imp Script Examples

objTable.Cell(x, 3).Range.text = s.Statex = x + 1Next

*******************************************************************************************'How do we validate links in web page and how to display linknames with status in excel

'*************************************************************************

Set objExcel=Createobject("excel.application")

objExcel.Visible=True

objExcel.Workbooks.Add

set objSheet=objExcel.ActiveSheet

objSheet.Cells(1,1)="LinkName"

set c1=objSheet.Cells(1,1)

C1.Font.color=vbblue

objSheet.Cells(1,2)="Expected URL"

set c2=objSheet.Cells(1,2)

C2.Font.color=vbblue

objSheet.Cells(1,3)="Actual URL"

set c3=objSheet.Cells(1,3)

C3.Font.color=vbblue

objSheet.Cells(1,4)="Status"

set c4=objSheet.Cells(1,4)

C4.Font.color=vbBlue

Set objDesc=Description.Create

objDesc("micclass").value="Link"

set objLinks=Browser("title:=.*").page("title:=.*").childobjects(objDesc)

msgbox objLinks.count

Page 17: Qtp Imp Script Examples

For i=0 to objLinks.count-35 step 1

strLinkName=Browser("title:=.*").page("title:=.*").Link("name:=.*","index:="&i).Getroproperty("name")

strExpUrl=Browser("title:=.*").page("title:=.*").Link("name:=.*","index:="&i).GetRoproperty("url")

Browser("title:=.*").page("title:=.*").Link("name:=.*","index:="&i).click

Browser("title:=.*").Sync

strActUrl=Browser("title:=.*").GetROProperty("url")

If instr(strActUrl,strExpUrl)>0Then

Reporter.ReportEvent micPass,"Link Vaidation","Succeed"

objSheet.Cells(i+2,1)=strLinkName

objSheet.Cells(i+2,2)=strExpUrl

objSheet.Cells(i+2,3)=strActUrl

objSheet.Cells(i+2,4)="Pass"

Set C4=objSheet.Cells(i+2,4)

C4.Font.color=vbGreen

Else

Reporter.ReportEvent micFail,"Link Vaidation","Failed"

objSheet.Cells(i+2,1)=strLinkName

objSheet.Cells(i+2,2)=strExpUrl

objSheet.Cells(i+2,3)=strActUrl

objSheet.Cells(i+2,4)="Fail"

Set C4=objSheet.Cells(i+2,4)

C4.Font.color=vbRed

End If

Browser("title:=.*").Back

Page 18: Qtp Imp Script Examples

Next

set objWbook=objExcel.ActiveWorkbook

objWbook.SaveAs "C:\OutPutdata.xls"

objExcel.Quit

Set objExcel=nothing

'*****************************************************************************

HOW DO WE VERIFY LINKS IN A WEB PAGE

******************************************************************************

On error resume next 'To handle run time error''SystemUtil.CloseProcessByName "iexplore.exe" Systemutil.Run "iexplore","https://login.sample.com/amserver/UI/Login"Browser( "sample").Page("sample ").WebEdit("html id:=IDToken1").Set "56793287"Browser("sample ").Page("sample ").WebEdit("html id:=IDToken2").Set "ranao2009"Browser("sample ").Page("sample ").WebButton("html id:=signIn").Clickwait(10)Browser("sample ").Dialog("text:=Security Information").Winbutton("text:=&Yes").clickIf Browser("sample ").Page("sample ").Image("close_text_button").Exist(10) ThenBrowser("sample ").Page("sample ").Image("close_text_button").ClickEnd ifSet objLinkDesc=Description.CreateobjLinkDesc("micclass").value="Link"'To count no of links in a web pageSet objLinks = Browser("sample ").Page("sample ").ChildObjects(objLinkDesc)'Set objLinks=Browser("sample ").page("sample ").childobjects(objLinkDesc)msgbox objLinks.countFor i=1 to objLinks.count-187 step 1            strTargetUrl=Browser("title:=.*").Page("title:=.*").Link("text:=.*","index:="&i).GetRoProperty("url")            print(strTargetUrl)'   Reporter.ReportEvent micDone,""&strTargetUrl,""'           objLinks(i).click            Browser("title:=.*").Page("title:=.*").Link("text:=.*","index:="&i).click            Browser("title:=.*").Sync            strActualUrl=Browser("title:=.*").GetROProperty("url")            Print(strActualUrl)            'Verify Link is navigating into the correct page            If instr(strTargetUrl,strActualUrl)>0 Then                        Reporter.ReportEvent micPass,"Navigate to correct page","The Actual URL is"& vbcrlf &strActualUrl& vbcrlf &"The Target URL is"& vbcrlf &strTargetUrl  'Report result to the QTP test log                        Else                        Reporter.ReportEvent micFail,"Navigate to wrong page","The Actual URL is"& vbcrlf &strActualUrl& vbcrlf &"The Target URL is"& vbcrlf &strTargetUrl     'Report result to the QTP test log

Page 19: Qtp Imp Script Examples

            End If            Browser("title:=.*").Page("title:=.*").Link("name:=SampleHomeLogo").click            Browser("title:=.*").SyncNext

‘*************************************************************************

'How to select specified checkbox in a web table

‘***************************************************************************

intRowcount = Browser("Inbox (477) - Yahoo! Mail").Page("Inbox (477) - Yahoo! Mail").WebTable("Inbox").RowCount()

For i=2 to intRowcount

strTxt=Browser("Inbox (477) - Yahoo! Mail").Page("Inbox (477) - Yahoo! Mail").WebTable("Inbox").GetCellData(i,5)

If strcomp(strTxt,"FreeHotPasses")=0 Then

set objChkBox=Browser("Inbox (477) - Yahoo! Mail").Page("Inbox (477) - Yahoo! Mail").WebTable("Inbox").ChildItem(i,2,"WebCheckBox",0)

objChkBox.set "ON"

End If

Next

‘**********************************************************************************

 

‘Sample script on registration in realtor application

‘*******************************************************************************

systemutil.Run "iexplore","www.realtor.com"

Page 20: Qtp Imp Script Examples

If Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebElement("Welcome | Sign In | Sign").Exist Then

reporter.ReportEvent micPass,"Step 1","Test is Pass"

Else

Reporter.ReportEvent micFail,"Step 1","Test is Fail"

End if

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").Link("Sign Up").Click

If Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebElement("SIGN UP").Exist Then

Reporter.ReportEvent micPass,"Step2","Test is Pass"

else

Reporter.ReportEvent micFail,"Step 2","Test is Fail"

End if

UserId="nagesh.rao49"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebEdit("WebEdit").Set UserId&"@gmail.com"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebEdit("WebEdit_2").SetSecure "4c697cca0717f1d0d617bead25615149f6b4"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebEdit("WebEdit_3").SetSecure "4c697cce3c61c2ec9481da3cd6ab89da7bd9"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebList("select").Select "Female"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebEdit("WebEdit_4").Set "1980"

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").Link("Sign Up_2").Click

Page 21: Qtp Imp Script Examples

If Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebElement("THANK YOU").Exist Then

Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").Link("CLOSE").Click

End if

‘***********************************************************************

' How to delete cookies

‘*************************************************************************

systemutil.Run "iexplore","www.realtor.com"

webutil.DeleteCookies

systemutil.CloseDescendentProcesses

‘************************************************************************

'How to validate Links in specified web page

‘***************************************************************************

Set objLinkDesc=Description.Create

objLinkDesc("micclass").value="Link"

set objLinks=Browser("title:=.*").Page("title:=.*").ChildObjects(objLinkDesc)

msgbox objLinks.count

For i=0 to objLinks.count-5

strExpUrl= Browser("title:=.*").page("title:=.*").Link("text:=.*","index:="&i).Getroproperty("url")

Browser("title:=.*").page("title:=.*").Link("text:=.*","index:="&i).click

' strExpUrl=objLinks(i).Getroproperty("url")

'Browser("title:=.*").page("title:=.*").Link("text:=.*","index:="&i).click

Page 22: Qtp Imp Script Examples

'objLinks(i).click

Browser("title:=.*").Sync

strActUrl=Browser("title:=.*").GetROProperty("url")

If instr( strExpUrl,strActUrl)>0 Then

Reporter.ReportEvent micPass,"Link validation","Test is pass"

Else

Reporter.ReportEvent micFail,"Link validation","Test is Fail"

End If

Browser("title:=.*").Back

'Browser("Commercial Real Estate").Page("Commercial Real Estate").Image("REALTOR.com® - Official").Click

Browser("title:=.*").Sync

'wait(10)

Next

‘****************************************************************

'How to set link names of specified page and keep them into a excel file

‘****************************************************************

Set objExcel=Createobject("Excel.application")

objExcel.Visible=True

objExcel.Workbooks.Add

set objsheet=objExcel.ActiveSheet

objsheet.cells(1,1)="LinkName"

set C=objsheet.cells(1,1)

c.font.color=vbblue

Set objLinkdesc=Description.Create

Page 23: Qtp Imp Script Examples

objLinkdesc("micclass").value="Link"

Set objLinks=Browser("title:=.*").Page("title:=.*").ChildObjects(objLinkdesc)

For i=0 to objLinks.count-1

strLinkname=objLinks(i).GetRoproperty("name")

objsheet.cells(i+2,1)=strLinkname

Next

set objWbook=objExcel.ActiveWorkbook

objWbook.SaveAs("C:\Demo.xls")

objExcel.Quit

Set objExcel=nothing

‘**************************************************************************************

p = Browser("Inbox (477) - Yahoo! Mail").Page("Real Estate Listings,").WebElement("Welcome, nagesh.rao48").GetROProperty("innertext")

arr=split(p,",")

If trim(arr(1))=UserId Then

Reporter.ReportEvent micPass,"Step 3","Test is Pass"

Else

Reporter.ReportEvent micFail,"Step 3","Test is Fail"

Page 24: Qtp Imp Script Examples

End If

‘***********************************************************************

‘************************************************************************

'How to focus the cursor on the object and how to select the itemsfrom dropdown

‘*************************************************************************

x = Browser("Google").Page("Google").WebEdit("q").GetROProperty("abs_x")

y=Browser("Google").Page("Google").WebEdit("q").GetROProperty("abs_y")

'

Set objDevice=Createobject("mercury.Devicereplay")

objDevice.Mouseclick x,y,0

Set objShell=Createobject("wscript.shell")

objShell.SendKeys "q"

wait(4)

Set objDesc=Description.Create

objDesc("micclass").value="WebElement"

objDesc("name").value="Google Search"

set objWele=Browser("Google").Page("Google").WebTable("qtp interview questions").ChildObjects(objDesc)

msgbox objWele.count

For i= 0 to objWele.count-1 step 1

objShell.SendKeys"{DOWN}"

Page 25: Qtp Imp Script Examples

strName=objWele(i).GetRoproperty("innertext")

Reporter.ReportEvent micDone,"Item Name--"&strName,"Item Captured"

Next

Set objDevice=Nothing

Set objShell=nothing

****************************************************************************************************

'How to focus the cursor on the object

'Browser("Google").Page("Welcome: Mercury Tours").Image("Desinations").Click

'x=Browser("Google").Page("Welcome: Mercury Tours").Image("Desinations").GetROProperty("abs_x")

'y=Browser("Google").Page("Welcome: Mercury Tours").Image("Desinations").GetROProperty("abs_y")

'

' Set objDevice=Createobject("Mercury.Devicereplay")

' objDevice.MouseMove x,y

' wait(10)

''******************************************************************************************************************