Вы находитесь на странице: 1из 18

FileSystemObject Scripts

Examples:


i) Create a Folder

Dim objFso

Set objFso=CreateObject(scripting.FileSystemObject)

objFso.CreateFolder C:\NageshQtp

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

Dim objFso, myFolder

myFolder=C:\NageshQtp

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:\Nagesh\Qtp

Set objFso=CreateObject(scripting.FileSystemObject)

objFso.CopyFolder myFolder,E:abcd

iv) Delete a folder

Dim objFso, myFolder

myFolder=C:\NageshQtp

Set objFso=CreateObject(scripting.FileSystemObject)

objFso.DeleteFolder( myFolder)

2nd

Dim objFso, myFolder

myFolder=C:\NageshQtp

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:\NageshQtp1.txt)

objFso.CreateTextFile (C:\NageshQtp2.doc)

objFso.CreateTextFile (C:\NageshQtp3.xls)

objFso.CreateTextFile (C:\NageshQtp4.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

myFile1=C:\NageshQtp1.txt

myFile2=C:\NageshQtp2.doc

myFile3=C:\NageshQtp3.xls

myFile4=C:\NageshQtp4.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:\NageshQtp.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

x)Read Line by Line from a Text File

Dim objFso, myFile, myChar

Set objFso=CreateObject(scripting.FileSystemObject)

Set myFile=objFso.OpenTextFile(C:\NageshQtp2.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

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

Dim objFso, myFile, myLine, myField

Set objFso=CreateObject(scripting.FileSystemObject)

Set myFile=objFso.OpenTextFile(C:\NageshQtp.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 Professional\samples\flight\app\flight4a.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:\NageshQtp3.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

xiii) Delete a Text File

Dim objFso

Set objFso=CreateObject(scripting.FileSystemObject)

objFso.DeleteFile(C:\NageshQtp.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:\NageshQtp1.pdf) Then

objFso.DeleteFile(C:\NageshQtp.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:\NageshQtp.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:\NageshQtp.txt

File2=C:\Nagesh.txt

Set objFso=CreateObject(scripting.FileSystemObject)

Comaring two text files by Size

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

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

File1=C:\NageshQtp.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) How to count no of items in the listbox

Option Explicit

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

FilePath=C:\NageshQtp.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 Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe

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

TotButtons=Buttons.Count

For i= 0 to TotButtons-1 Step 1

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 system 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:\NageshQtp.txt,2)

myFile.WriteLine Customer Names

myFile.WriteLine

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

SystemUtil.Run C:Program Files\HP\QuickTest
Professional\samples\flight\app\flight4a.exe,",C:Program Files\HP\QuickTest
Professional\samples\flight\app,open

Dialog(Login).Activate

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

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

xx) how to verify update Order behaviour in the Flight reservation application and how to pass
multiple values from the textfile

Option explicit

Dim fso,f,s,arr,strStatus

Set fso=CreateObject("Scripting.filesystemobject")

set f=fso.OpenTextFile("E:\Records.txt",1)

While not f.AtEndOfStream

s=f.ReadLine

arr=split(s," ")

Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."

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

Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set arr(1)

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

strStatus = Window("Flight Reservation").WinButton("Update
Order").GetROProperty("Enabled")

If strStatus="False" Then

Reporter.ReportEvent micPass,"Update is Disabled","Test is pass"

Else

Reporter.ReportEvent micFail,"Update is Enabled","Test id failed"

End If

Wend

f.Close

Set fso=nothing

xxi)How to count number of items in the Fly From Combo Box and how to display their name
one by one.



s = Window("Flight Reservation").WinComboBox("Fly From:"). GetItemsCount()

msgbox s

For i=0 to n-1 step 1

y = Window("Flight Reservation").WinComboBox("Fly From:").GetItem(i) msgbox y

Next

xxii):How to display FlyFrom Combo Box items one by one into Notepad

Option explicit

Dim fso,s,x,n

Set fso=createobject("Scripting.filesystemobject")

Set s=fso.opentextfile ("C:\Documents and Settings\Sai Sarvani\Desktop\ sample.txt",2)

x = Window("Flight Reservation").WinComboBox("Fly From:"). GetItemsCount

For i=0 to n-1 step 1

n = Window("Flight Reservation").WinComboBox("Fly From:").GetItem(i) s.writeline n

next

s.close

xxiii)How to display Flyfrom Combo box items one by one into Runtime Data table and the
same data table export to Excel Sheet

datatable.AddSheet ("output").Addparameter "From", " "

n = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount

For i=0 to n-1 step 1

x = Window("Flight Reservation").WinComboBox("Fly From:").GetItem(i)
datatable.SetCurrentRow (i+1)

datatable.Value ("From", "output") =x

Next

datatable.ExportSheet "C:\Documents and Settings\Sai Sarvani\Desktop\ output1.xls", "output"

xxiv)If you select one item from Fly From Combo Box, that item should be disappeared in Fly
To Combo Box

n = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount

For i=0 to n-1 step 1

x = Window("Flight Reservation").WinComboBox("Fly From:").GetItem(i) Window("Flight
Reservation").WinComboBox("Fly From:").Select x

m = Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount()

For j=0 to m-1 step 1

y = Window("Flight Reservation").WinComboBox("Fly To:").GetItem(j)

If strcomp(x,y,0)<>0 Then

reporter.ReportEvent micPass,"Items are not same", "test isPass"

else

reporter.ReportEvent micFail,"Items are same", "Test is fail"

End If

Next

Next

xxv)How to count number of subfolders and files in a specified folder and how to display their
names one by one? Prepare script for above expected?

Option explicit Dim fso,f,sf,n,g,ft,m,k

Set fso=createobject("Scripting.filesystemobject") Set f=fso.folder ("F:\XP")

Set sf=f.subfolders n=sf.count msgbox n

For each g in sf

msgbox g.name

Next

Set ft=f.files m=ft.count msgbox m

For each k in ft

msgbox k.name

Next

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

xxvi)How to get data from one file and how set result into another file

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

Set objFso=Createobject("Scripting.FileSystemObject")

set f=objFso.OpenTextFile("C:\Testdata.txt",1)

objFso.CreateFolder("C:\Result")

Set f1=objFso.CreateTextFile("C:\Result\Result.txt")

f1.WriteLine "No Tickets"&space(3)&"Price"&space(3)&"Total"&space(5)&"status"

While not f.AtEndOfStream

s=f.ReadLine

arr=split(s," ")

Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."

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

Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set arr(1)

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

t = Window("Flight Reservation").WinEdit("Tickets:").GetVisibleText()

p = Window("Flight Reservation").WinEdit("Price:").GetVisibleText()

p=mid(p,2,len(p)-1)

tot = Window("Flight Reservation").WinEdit("Total:").GetVisibleText()

tot=mid(tot,2,len(tot)-1)

If cdbl(tot)=cdbl(p)*cint(t) Then

Reporter.ReportEvent 0,"Step1","Calculation Pass"

f1.writeline t&space(12)&p&space(2)&tot&space(4)&"Calculation is Pass"

else

Reporter.ReportEvent 1,"Step1","Calculation Fail"

f1.writeline t&space(12)&p&space(2)&tot&space(4)&"Calculation is Fail"

End If

Wend

f.Close

f1.Close

Set f=nothing

Set f1=nothing

Set objFSo=nothing

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

Descriptive Programming

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

Set objFlyFrom = Description.Create

objFlyFrom("Class Name").value = "wincomboBox"

objFlyFrom("attached text").value = "Fly From:"

Set objFlyTo = Description.Create

objFlyTo("Class Name").value = "wincomboBox"

objFlyTo("attached text").value = "Fly To:"

'Set objwin=Description.Create

'objwin("text").value="Flight Reservation"

FlyFrom_ItemsCount = Window("text:=Flight
Reservation").WinComboBox(objFlyFrom).GetItemsCount

For i=0 to FlyFrom_ItemsCount-1 step 1

FlyFrom_Item = Window("text:=Flight Reservation").WinComboBox(objFlyFrom).GetItem(i)

Window("text:=Flight Reservation").WinComboBox(objFlyFrom).Select FlyFrom_Item

FlyTo_ItemsCount=Window("text:=Flight
Reservation").WinComboBox(objFlyTo).GetItemsCount

For j = 0 to FlyTo_ItemsCount-1

If strcomp(FlyFrom_Item,Window("text:=Flight
Reservation").WinComboBox(objFlyTo).GetItem(j))=0 Then

Reporter.ReportEvent micFail, "step1", "Item equals in Fly To"

else

Reporter.ReportEvent micPass, "step1", "Item equals in Fly To"

End If

Next

Next


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