UFT Tutorial 29

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

It is a characteristic of Operating System, used to live on amongst Drives, Folders as well as files.

Computer understands things inwards damage of files only, Computer betoken of persuasion everything is file.

> Examples for File organisation Operations

Create a folder

Copy a folder

Delete a folder

Create a text file

Read data

Write data

Compare data

Search operations etc...
-----------------------------------
> How terminate user performs File organisation operations?

End user performs file organisation operations manually amongst the deal of Input devices.

If it is Command delineate of piece of job operating organisation amongst the deal of operating organisation commands.
--------------------------------
> How to perform automatic File organisation operations?

Using VBScript FileSystemObject nosotros tin perform automatic File organisation operations.

Syntax for creating Automation object inwards VBScript?

Set Variable = CreateObject("Class Value")

Set - It is VBScript statement

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

Set Variable = CreateObject("Scripting.FileSystemObject")

Note: File System Object is solely for treatment Flat files, but nosotros tin practise and
delete other types of files also.

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

> If y'all desire perform operations Excel files as well as then role Excel Application object.

> If y'all desire perform operations Word files as well as then role 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 liberate the Memory
------------------------
'2 Check existance of the UFT folder, if non exists as well as then practise 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 liberate 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 as well as 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 yesteryear Character
    Read Line yesteryear Line
    Read All
b) Writing
    Write continuously
    Write Line yesteryear Line
    Append
c) Comparison
    yesteryear Size (* High Level Operation)
    yesteryear Text
    yesteryear Binary values
d) Search operations
etc...
------------------------
> Using FileSystemObject nosotros tin perform High marker File organisation operations.

> Using TextStream Object nosotros tin perform Text related operations.

Create FileSystem Object

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

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

We bring iii file modes for Text files.

1 for Read (Default mode)

2 for Write

8 for Append
--------------------------------------------
9) Read a Text file Character yesteryear 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 yesteryear 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 information from a text file as well as perform information 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
----------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post