File System Object in UFT

File System Object in UFT
------------------------
    What is computer File System?

It is a feature Operating System used to work with Drives, Folders and Files
---------------------------------------------------
    Give some examples for File System Operations

Create a Folder

Copy a Folder

Delete a Folder

Create a Text File

Copy a Text File

Delete a Text File

Read data

Write data

Compare data

Search operations

etc....
----------------------------------
    How end user performs File System Operations
End user performs File System Operations manually with the help of Input devices.

----------------------------------------------------
    How to perform automatic File System Operations using VBScript
Using File System Object we can perform automatic File System Operations
--------------------------------------------------------

Create an Automation Object

Set Variable = CreateObject("Class Value")

Set - VBScript Statement

CreateObject - Built in Function
------------------------------------------------

("Scripting.FileSystemObject")

-------------------------------------------
Examples:
----------
'Create a Folder
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder "C:\Documents and Settings\gcreddy\Desktop\QTP"
Set objFso = Nothing 'To release the memory
----------------------------------------------
'Check the existence of QTP Folder, if not exists then Create the Folder

Dim objFso, FolderPath
FolderPath = "C:\Documents and Settings\gcreddy\Desktop\QTP"
Set objFso = CreateObject("Scripting.FileSystemObject")

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

Set objFso = Nothing
----------------------------------------------
'Copy a Folder

Dim objFso, FolderPath
FolderPath = "C:\Documents and Settings\gcreddy\Desktop\QTP"
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder FolderPath, "C:\UFT"

Set objFso = Nothing
----------------------------------------------
'Delete a Folder

Dim objFso, FolderPath
FolderPath = "C:\Documents and Settings\gcreddy\Desktop\QTP"
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.DeleteFolder FolderPath

Set objFso = Nothing
---------------------------------------------------
'Check the existence of QTP folder, If exists then Delete the Folder

Dim ObjFso, FolderPath
FolderPath = "C:\Documents and Settings\gcreddy\Desktop\QTP"
Set ObjFso = CreateObject ("Scripting.FileSystemObject")

If ObjFso.FolderExists (FolderPath)Then
    ObjFso.DeleteFolder (FolderPath)
End If
Set ObjFso = Nothing
---------------------------------------------------
'Create a Text File
Dim ObjFso
Set ObjFso = CreateObject ("Scripting.FileSystemObject")
ObjFso.CreateTextFile "C:\Documents and Settings\gcreddy\Desktop\QTP.txt"

ObjFso.CreateTextFile "C:\Documents and Settings\gcreddy\Desktop\QTP.doc"

ObjFso.CreateTextFile "C:\Documents and Settings\gcreddy\Desktop\QTP.xls"

ObjFso.CreateTextFile "C:\Documents and Settings\gcreddy\Desktop\QTP.pdf"

Set ObjFso = Nothing
----------------------------------------------------------------------

Text stream Object

It is used to perform Text related Operations

Create Text stream Object

Set variable = FileSystemObject.CreateTextFile/OpenTextFile ("FilePath", File Mode)

File modes

1 for Read

2 for Write

8 for Append
------------------------
Text related Operations

Reading
    Read char by Char
    Line by Line
    Read All

Writing
    Write continuously
    Writ Line by Line
    Append

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

Search Operations
etc...
---------------------------------------------
'Read a Text file char by char
Dim objFso, objTextstream, myChar
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt")

Do Until objTextstream.AtEndOfStream
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:\Documents and Settings\gcreddy\Desktop\QTP.txt")

Do Until objTextstream.AtEndOfStream
myLine=objTextstream.ReadLine
Msgbox myLine   
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------------------
'Read a Text file (All)
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Documents and Settings\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:\Documents and Settings\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","","C:\Program Files\HP\Unified Functional Testing\samples\flight\app\","open"
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 (7th Record)

Dim objFso, objTextstream, myLine, myField, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt")

LineCount = 0
While objTextstream.AtEndOfStream = False
myLine=objTextstream.ReadLine
LineCount= LineCount + 1

If LineCount = 8 Then
myField = Split(myLine, ", ")

SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe","","C:\Program Files\HP\Unified Functional Testing\samples\flight\app\","open"
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
-------------------------------------------
'Write data
Dim objFso, objTextstream, num1, num2, res
num1=10 : num2 = 20: res = num1 + num2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt", 2)

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

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-------------------------------------------
'Write data
Dim objFso, objTextstream, num1, num2, res
num1=10 : num2 = 20: res = num1 + num2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.CreateTextFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt", 2)

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

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-------------------------------------------
'Write data line by line
'Read link names from Rediff home page and export to a Text file
Dim objFso, objTextstream, oLink, Links, i

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.CreateTextFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt", 2)
Set oLink = Description.Create
oLink("micclass").Value = "Link"

Set Links = Browser("Rediff.com - India, Business,").Page("Business Email | Company").ChildObjects(oLink)
objTextstream.WriteLine "Link Names"
objTextstream.WriteLine "---------------"


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

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-----------------------------------------------
'Read Button Names fro Login Dialog and export to Text file

Dim objfso, objtextstream, obutton, buttons, i

Set objfso = Createobject ("Scripting.Filesystemobject")
Set objtextstream = objfso.OpenTextFile ("C:\Documents and Settings\gcreddy\Desktop\QTP.txt",2)

Set obutton = Description.Create
obutton("micclass").value = "Winbutton"

Set buttons = Dialog("Login").ChildObjects(obutton)

objtextstream.WriteLine "Button Names"
objtextstream.WriteLine "------------"

For i = 0 To buttons.count-1 Step 1
    objtextstream.WriteLine i+1 & ") " & buttons(i).getroproperty("text")
Next
objtextstream.Close
Set objtextstream = Nothing
Set objfso = Nothing
-------------------------------------
'Find a File Size

Dim objfso
Set objfso = Createobject ("Scripting.Filesystemobject")
Msgbox objfso.GetFile("C:\Documents and Settings\gcreddy\Desktop\QTP.txt").Size & " Bytes"
Set objfso = Nothing

0 comments:

Post a Comment