UFT Tutorial 29

UFT Class 29 (VBScript File System Operations)
> What is Computer File System?

It is a feature of Operating System, used to work with Drives, Folders and files.

Computer understands things in terms of files only, Computer point of view everything is file.

> Examples for File system Operations

Create a folder

Copy a folder

Delete a folder

Create 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.

If it is Command line operating system with the help of operating system commands.
--------------------------------
> How to perform automatic File system operations?

Using VBScript FileSystemObject we can perform automatic File system operations.

Syntax for creating Automation object in VBScript?

Set Variable = CreateObject("Class Value")

Set - It is VBScript statement

CreateObject- VBScript Built in Function
---------------------
Create File System Object:

Set Variable = CreateObject("Scripting.FileSystemObject")

Note: File System Object is only for handling Flat files, but we can create and
delete other types of files also.

> We can't perform internal operations (Ex: Read, write etc...) on other types of files.

> If you want perform operations Excel files then use Excel Application object.

> If you want perform operations Word files then use Word Application object.
----------------------------------------------
Examples:
----------------
1) Create a Folder

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder "C:\Users\G C Reddy\Desktop\UFT"

Set objFso = Nothing 'To release the Memory
------------------------
'2 Check existance of the UFT folder, if not exists then create the Folder
Dim objFso, FolderPath
FolderPath = "C:\Users\G C Reddy\Desktop\UFT"
Set objFso = CreateObject("Scripting.FileSystemObject")

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

Set objFso = Nothing 'To release the Memory
-------------------------------------------
3) Copy a Folder

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder "C:\Users\G C Reddy\Desktop\UFT", "C:\"
Set objFso = Nothing
----------------------------------
4) Delete a Folder
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.DeleteFolder "C:\Users\G C Reddy\Desktop\UFT"
Set objFso = Nothing
------------------------------
5) Check existance of the UFT folder if exists then Delete the folder

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

If objFso.FolderExists("C:\Users\G C Reddy\Desktop\UFT") Then
    objFso.DeleteFolder "C:\Users\G C Reddy\Desktop\UFT"
End If

Set objFso = Nothing
------------------------------
6) Create a Text File

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

objFso.CreateTextFile ("C:\Users\G C Reddy\Desktop\UFT.txt")

objFso.CreateTextFile ("C:\Users\G C Reddy\Desktop\UFT.xls")

objFso.CreateTextFile ("C:\Users\G C Reddy\Desktop\UFT.doc")

objFso.CreateTextFile ("C:\Users\G C Reddy\Desktop\UFT.pdf")

Set objFso = Nothing
-------------------------------------
7) Delete a Text File
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

objFso.DeleteFile ("C:\Users\G C Reddy\Desktop\UFT.txt")

objFso.DeleteFile ("C:\Users\G C Reddy\Desktop\UFT.xls")

objFso.DeleteFile ("C:\Users\G C Reddy\Desktop\UFT.doc")

objFso.DeleteFile ("C:\Users\G C Reddy\Desktop\UFT.pdf")

Set objFso = Nothing
-------------------------------------------------
8) Copy a Text File

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CopyFile "C:\Users\G C Reddy\Desktop\UFT Class.txt", "D:\"

Set objFso = Nothing
-----------------------------------------------
File System Operations
    > High Level Operations
    > Internal / Text related operations

i) High Level Operations:

Create a folder

Copy a folder

Delete a folder

Create a text file

Delete a text file
Etc...
-----------------------
ii) Text Related or internal

a) Reading
    Read Character by Character
    Read Line by Line
    Read All
b) Writing
    Write continuously
    Write Line by Line
    Append
c) Comparison
    by Size (* High Level Operation)
    by Text
    by Binary values
d) Search operations
etc...
------------------------
> Using FileSystemObject we can perform High level File system operations.

> Using TextStream Object we can perform Text related operations.

Create FileSystem Object

Set variable = CreateObject("Scripting.FileSystemObject")
------------------------
Create Textstream object

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

We have three file modes for Text files.

1 for Read (Default mode)

2 for Write

8 for Append
--------------------------------------------
9) Read a Text file Character by Character

Dim objFso, objTextstream, myChar
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C Reddy\Desktop\UFT.txt", 1)

Do While objTextstream.AtEndOfStream = False
    myChar = objTextstream.Read(1)
    Msgbox myChar
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
------------------------------
10) Read a Text file Line by Line

Dim objFso, objTextstream, myLine
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C Reddy\Desktop\UFT.txt", 1)

Do While objTextstream.AtEndOfStream = False
    myLine = objTextstream.ReadLine
    Msgbox myLine
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
---------------------------------
11) Read entire file data
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C Reddy\Desktop\UFT.txt", 1)

myContent = objTextstream.ReadAll
Msgbox myContent
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-----------------------------------
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C Reddy\Desktop\UFT Class.txt", 1)

myContent = objTextstream.ReadAll
Print myContent
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
--------------------------------------------------
12) Read data from a text file and perform data driven Testing for Login Functionality.

Dim objFso, objTextstream, myLine, myField
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C Reddy\Desktop\UFT.txt")
objTextstream.SkipLine

Do 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   
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------

0 comments:

Post a Comment