File system Object in VBScript

File System Operations in UFT

  i) What is Computer File System?
 
It is a feature of operating system used to work with Drives, Folders and files.
 

ii) Give examples for File System Operations

Create a folder

Copy folder

Delete folder

Create text file

Delete text file

Write data

Read data

Compare data

Search operations etc...
---------------------------------
    iii) How end user performs File System Operations

Manually, with the help of Input devices.

    iv) How to do automatic File System Operations using VBScript

Using File system Object
---------------------------------
Script Examples:
-------------------
'Create a folder
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objfso.CreateFolder "C:\Users\gcreddy\Desktop\UFT"
Set objFso = Nothing 'To release the Memory
----------------------------------------------------
'Check the existence of UFT folder, if not exists then Create the folder
Dim objFso, FolderPath
FolderPath = "C:\Users\gcreddy\Desktop\UFT"
Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FolderExists(FolderPath) Then
    objFso.CreateFolder Folderpath
End If

Set objFso = Nothing
--------------------------------------
'Delete a folder
Dim objFso, FolderPath
FolderPath = "C:\Users\gcreddy\Desktop\UFT"
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.DeleteFolder FolderPath

Set objFso = Nothing

'Copy a Folder
Dim objFso
Set objFso= CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder "C:\Users\gcreddy\Desktop\UFT", "C:\"
Set objFso = Nothing
------------------------------------
'Create a Text File
Dim objFso
Set objFso= CreateObject("Scripting.FileSystemObject")

objFso.CreateTextFile "C:\Users\gcreddy\Desktop\QTP.txt"

objFso.CreateTextFile "C:\Users\gcreddy\Desktop\QTP.doc"

objFso.CreateTextFile "C:\Users\gcreddy\Desktop\QTP.xls"

objFso.CreateTextFile "C:\Users\gcreddy\Desktop\QTP.pdf"

Set objFso = Nothing
----------------------------------------------------
File System Operations

External operations

Create a Folder

Copy a Folder

Delete a Folder

Create a Text file

Delete a Text file

Etc...
----------------------
Text related operations

Writing
    Write Continuously
    Write Line by Line
    Append

Reading
    Read char by Char
    Read Line by Line
    Read All

Comparison
    by Size (*External operation)
    by Text
    by Binary values

Search operations
-------------------------
Create File System Object

Set Variable = CreateObject("Scripting.FileSystemObject")
---------------------

Create Text Stream Object

Set Variable = FileSystemObject.CreateTextFile / OpenTextFile("File Path", File Mode)

File Modes

1 for Read (Default)

2 for Write

8 for Append
---------------------
'Read a Text file char by char
Dim objFso, objTextstream, myChar
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

Do While objTextstream.AtEndOfStream = False
    myChar = objTextstream.Read(1)
    Msgbox myChar
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
--------------------------------
'Read a Text file Line by Line
Dim objFso, objTextstream, myLine
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

Do While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    Msgbox myLine
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
------------------------------
'Read a Text file Line by Line (Range of Lines)
Dim objFso, objTextstream, myLine, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

LineCount = 0
Do While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    LineCount = LineCount + 1
   
    If LineCount > 2 And LineCount < 6 Then
        Msgbox myLine
    End If
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
--------------------------
'Read a Text file Line by Line (Range of Lines)
Dim objFso, objTextstream, myLine, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

LineCount = 0
Do While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    LineCount = LineCount + 1
   
    If LineCount = 3 Then
    a = Split(myLine)
        Msgbox a(1)
    End If
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
----------------------------------
'Read a Text file (Read All)
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

myContent = objTextstream.ReadAll
Msgbox myContent

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
-------------------------------
'Read a Text file (Read All)
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\UFT.txt")

myContent = objTextstream.ReadAll
Print myContent

objTextstream.Close
Set objTextstream = Nothing
Set objfso = Nothing
----------------------------------------
'Read Test Data from a Text file and perform Data Driven Testing for Login Operation
Dim objFso, objTextstream, myLine, myField
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

objTextstream.SkipLine
While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    myField = Split(myLine, ", ")
   
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
Wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Wend

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
---------------------------------
'Read Test Data from a Text file (Range of Records) and perform Data Driven Testing for Login Operation
Dim objFso, objTextstream, myLine, myField, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")

LineCount = 0
While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    LineCount = LineCount + 1
   
    If LineCount > 4 And LineCount < 9 Then
    myField = Split(myLine, ", ")
   
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe”
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
Wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End If
Wend

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
'Read Test Data from a Text file and perform Data Driven Testing for Login Operation
'Write Result to another File
Dim objFso, objTextstream1, objTextstream2, myLine, myField, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream1 = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt")
Set objTextstream2 = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\Result.txt", 2)
objTextstream1.SkipLine

objTextstream2.WriteLine "Agent Password Result"
While objTextstream1.AtEndOfStream = False
    myLine = objTextstream1.ReadLine
    myField = Split(myLine, ", ")
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
Wait 2
Dialog("Login").WinButton("OK").Click

If Window("Flight Reservation").Exist(12) Then
    Window("Flight Reservation").Close
    objTextstream2.WriteLine myField(0) &", " &myField(1) & " Login Successful - Passed"
    Else
    SystemUtil.CloseDescendentProcesses
    objTextstream2.WriteLine myField(0) &", " &myField(1) & " Login Unsuccessful - Failed"
End If
Wend

objTextstream1.Close
objTextstream2.Close
Set objTextstream1 = Nothing
Set objTextstream2 = Nothing
Set objFso = Nothing
----------------------------
'Write Data
Dim objFso, objTextstream, num1, num2, res
num1 = 500 : num2 = 600 : res = num1 + num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt", 8)
objTextstream.Write "Addition of num1, num2 is: "& res

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-----------------------------------------
'Write Data Line by Line
Dim objFso, objTextstream, num1, num2, res
num1 = 500 : num2 = 600 : res = num1 + num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt", 8)

objTextstream.WriteLine "Addition of num1, num2 is: "& res

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-----------------------------------------------
'Read Button Names from Login Dialog and write to Text file
Dim objFso, objTextstream, oButton, Buttons, i, sno
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt", 2)

Set oButton = Description.Create
oButton("Class Name").Value = "WinButton"
Set Buttons = Dialog("Login").ChildObjects(oButton)

objTextstream.WriteLine "Button Names"
objTextstream.WriteLine "--------"
sno = 1
For i = 0 To Buttons.Count - 1 Step 1
objTextstream.WriteLine sno &") " & Buttons(i).GetRoProperty("text")
sno = sno + 1
Next

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
--------------------------------------------
'Read Link Names from Rediff Home Page and write to Text file
Dim objFso, objTextstream, oLink, Links, i, sno
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt", 2)

Set oLink = Description.Create
oLink("micclass").Value = "Link"

Set Links = Browser("Rediff.com - India, Business,").Page("Rediff.com - India, Business,").ChildObjects(oLink)

objTextstream.WriteLine "Link Names"
objTextstream.WriteLine "--------"
sno = 1
For i = 0 To Links.Count - 1 Step 1
objTextstream.WriteLine sno &") " & Links(i).GetRoProperty("text")
sno = sno + 1
Next

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
--------------------------------------------
'Open 1 to 10 Records in Flight Reservation and Read Customer Names , Tickets
'Export to Text file
Dim objFso, objTextstream, ord, C_Name, Tickets
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\QTP.txt", 2)

objTextstream.WriteLine "Customer Name    Tickets"
For ord = 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 ord
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
C_Name = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
Tickets = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
objTextstream.WriteLine C_Name &" "& Tickets
Next
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
--------------------------------------------
'Compare two text files by Size, by Text, and by Binary Values
Dim objFso, objTextstream1, objTextstream2, File1, File2, content1, content2
File1 = "C:\Users\gcreddy\Desktop\QTP.txt"
File2 = "C:\Users\gcreddy\Desktop\abc.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
'Compare two text files by Size
Msgbox "File 1 Size is: " & objFso.GetFile(File1).Size & " Bytes"
Msgbox "File 2 Size is: " & objFso.GetFile(File2).Size & " Bytes"

If objFso.GetFile(File1).Size = objFso.GetFile(File2).Size Then
    Msgbox "Files are Same by Size"
    Else
    Msgbox "Files are Not Same by Size"
End If

Set objTextstream1 = objFso.OpenTextFile(File1)
Set objTextstream2 = objFso.OpenTextFile(File2)

content1 = objTextstream1.ReadAll
content2 = objTextstream2.ReadAll

'Compare two text files by Text

If StrComp(content1, content2, 1) = 0 Then
    Msgbox "Files are Same by Text"
    Else
    Msgbox "Files are Not Same by Text"
End If

'Compare two text files by Binary Values
If StrComp(content1, content2) = 0 Then
    Msgbox "Files are Same"
    Else
    Msgbox "Files are Not Same"
End If
objTextstream1.Close
objTextstream2.Close

Set objTextstream1 = Nothing
Set objTextstream2 = Nothing
Set objFso = Nothing

0 comments:

Post a Comment