qtp script examples
DESCRIPTION
QTP Flat file operations and File system object.TRANSCRIPT
gcreddy.com
Visit:
www.gcreddy.com
for QTP Info
File System OperationsFile System Object Model:
The File System Object (FSO) model provides an object-based tool for working with folders and files. It allows us to use the familiar object. method syntax with a rich set of properties, methods, and events to process folders and files. We can also employ the traditional Visual Basic statements and commands.
The FSO model gives our application the ability to create, alter, move, and delete folders, or to determine if and where particular folders exist. It also enables us to get information about folders, such as their names and the date they were created or last modified.
The FSO model makes processing files much easier as well. When processing files, our primary goal is to store data in an efficient, easy-to-access format. We need to be able to create files, insert and change the data, and output (read) the data. Although we can store data in a database, doing so adds a significant amount of overhead to our application. We may not want to have such overhead, or our data access requirements may not call for the extra functionality associated with a full-featured database. In this case, storing our data in a text file or binary file is the most efficient solution.
The FSO model, contained in the Scripting type library (Scrrun.dll), supports the creation and manipulation of text files through the TextStream object; however, the FSO model does not support binary files. To manipulate binary files, use the FileOpen Function with the Binary keyword.
Examples
QTP Training 1
gcreddy.com
1) Create a Folder
Option ExplicitDim objFSO, objFolder, strDirectorystrDirectory = "D:\Gcreddy"Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.CreateFolder(strDirectory)
2) Delete a Folder
Set oFSO = CreateObject("Scripting.FileSystemObject")oFSO.DeleteFolder("E:\Gcreddy")
3) Copying Folders
Set oFSO=createobject("Scripting.Filesystemobject")oFSO.CopyFolder "E:\gcr", "C:\jvr", True
4) Checking weather the folder available or not, if not creating the folder
Option ExplicitDim objFSO, objFolder, strDirectorystrDirectory = "D:\Gcreddy"Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists(strDirectory) ThenSet objFolder = objFSO.GetFolder(strDirectory)msgbox strDirectory & " already created "elseSet objFolder = objFSO.CreateFolder(strDirectory)end if
5) Returning a collection of Disk Drives
Set oFSO = CreateObject("Scripting.FileSystemObject")Set colDrives = oFSO.DrivesFor Each oDrive in colDrivesMsgBox "Drive letter: " & oDrive.DriveLetterNext
6) Getting available space on a Disk Drive
Set oFSO = CreateObject("Scripting.FileSystemObject")Set oDrive = oFSO.GetDrive("C:")MsgBox "Available space: " & oDrive.AvailableSpace
7) Creating a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.CreateTextFile("E:\Gcreddy.txt")
8) Checking weather the File is available or not, if not creating the File
QTP Training 2
gcreddy.com
strDirectory="E:\"strFile="Gcreddy.txt"Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FileExists(strDirectory & strFile) ThenSet objFolder = objFSO.GetFolder(strDirectory)ElseSet objFile = objFSO.CreateTextFile("E:\Gcreddy.txt")End if
9) Reading Data character by character from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("E:\Gcreddy.txt", 1)Do Until objFile.AtEndOfStreamstrCharacters = objFile.Read(1)msgbox strCharactersLoop
10) Reading Data line by line from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("E:\Gcreddy.txt", 1)Do Until objFile.AtEndOfStreamstrCharacters = objFile.Readlinemsgbox strCharactersLoop
11) Reading data from a flat file and using in data driven testing
Dim fso,myfileSet fso=createobject("scripting.filesystemobject")Set myfile= fso.opentextfile ("F:\Gcreddy.txt",1)myfile.skiplineWhile myfile.atendofline <> True x=myfile.readlines=split (x, ",")
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"Dialog("Login").ActivateDialog("Login").WinEdit("Agent Name:").Set s(0)Dialog("Login").WinEdit("Password:").SetSecure s(1)Dialog("Login").WinButton("OK").ClickWindow("Flight Reservation").CloseWend
12) Writing data to a text file
Dim Stuff, myFSO, WriteStuff, dateStampdateStamp = Date()Stuff = "I am Preparing this script: " &dateStampSet myFSO = CreateObject("Scripting.FileSystemObject")Set WriteStuff = myFSO.OpenTextFile("e:\Gcreddy.txt", 8, True)
QTP Training 3
gcreddy.com
WriteStuff.WriteLine(Stuff)WriteStuff.CloseSET WriteStuff = NOTHINGSET myFSO = NOTHING
13) Delete a text file
Set objFSO=createobject("Scripting.filesystemobject")Set txtFilepath = objFSO.GetFile("E:\gcr.txt")txtFilepath.Delete()
14) Checking weather the File is available or not, if available delete the File
strDirectory="E:\"strFile="gcr.txt"Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FileExists(strDirectory & strFile) ThenSet objFile = objFSO.Getfile(strDirectory & strFile)objFile.delete ()End if
15) Comparing two text files
Dim f1, f2f1="e:\Gcreddy1.txt"f2="e:\Gcreddy2.txt"Public Function CompareFiles (FilePath1, FilePath2)Dim FS, File1, File2Set FS = CreateObject("Scripting.FileSystemObject") If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size ThenCompareFiles = TrueExit FunctionEnd IfSet File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)CompareFiles = FalseDo While File1.AtEndOfStream = FalseStr1 = File1.Read Str2 = File2.Read CompareFiles = StrComp(Str1, Str2, 0)If CompareFiles <> 0 ThenCompareFiles = TrueExit DoEnd IfLoopFile1.Close()File2.Close()End Function Call Comparefiles(f1,f2) If CompareFiles(f1, f2) = False ThenMsgBox "Files are identical."ElseMsgBox "Files are different."End If
QTP Training 4
gcreddy.com
16) Counting the number of times a word appears in a file
sFileName="E:\gcr.txt"sString="gcreddy"Const FOR_READING = 1Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatchesSet oFso = CreateObject("Scripting.FileSystemObject")Set oTxtFile = oFso.OpenTextFile(sFileName, FOR_READING)sReadTxt = oTxtFile.ReadAllSet oRegEx = New RegExpoRegEx.Pattern = sStringoRegEx.IgnoreCase = bIgnoreCaseoRegEx.Global = TrueSet oMatches = oRegEx.Execute(sReadTxt)MatchesFound = oMatches.CountSet oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothingmsgbox MatchesFound
17) Read a CSV File Using Database Techniques
On Error Resume Next
Const adOpenStatic = 3Const adLockOptimistic = 3Const adCmdText = &H0001
Set objConnection = CreateObject("ADODB.Connection")Set objRecordSet = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\Databases\"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & strPathtoTextFile & ";" & _ "Extended Properties=""text;HDR=YES;FMT=Delimited"""
objRecordset.Open "SELECT * FROM PhoneList.csv", _ objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF Wscript.Echo "Name: " & objRecordset.Fields.Item("Name") Wscript.Echo "Department: " & _ objRecordset.Fields.Item("Department") Wscript.Echo "Extension: " & objRecordset.Fields.Item("Extension") objRecordset.MoveNextLoop
18) Read a Text File into an Array
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile _ ("e:\gcreddy.txt", ForReading)
Do Until objTextFile.AtEndOfStream
QTP Training 5
gcreddy.com
strNextLine = objTextFile.Readline arrServiceList = Split(strNextLine , ",") Wscript.Echo "Server name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "Service: " & arrServiceList(i) NextLoop
QTP Training 6