VBScript Tutorial 7

VBScript Tutorial 7
(VBScript File System Operations Part-2)

6) Create a Text file

Note: File System Object is only used for Drives, Folders and Flat files.
We can create and delete other types of also, but we can't perform internal operations like Reading, Writing etc...

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
-------------------------------------
7) Delete a Text file

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

objFso.DeleteFile "C:\Users\gcreddy\Desktop\QTP.txt"
objFso.DeleteFile "C:\Users\gcreddy\Desktop\QTP.doc"
objFso.DeleteFile "C:\Users\gcreddy\Desktop\QTP.xls"
objFso.DeleteFile "C:\Users\gcreddy\Desktop\QTP.pdf"

Set objFso = Nothing
-----------------------------------------------
External Operations

Create a Text file

Delete a Text file

Check existence of a File

Count size

etc...

Internal operations

Read
    Read character by Character
    Read Line by Line
    Read All

Write
    Write Continuously
    Write Line by Line
    Append

Compare
    Compare two text files by Size (*External operation)
    Compare two text files by Text
    Compare two text files by Binary values.

Search operations
--------------------------------------------------
> using File System object we can perform External File operations

> Using Text stream object we can perform Internal operations.

> Using File System Object we can create Text stream object.

Create File System Object
Set Variable = CreateObject("Scripting.FileSystemObject")

Create Text Stream object

Set Variable = FileSystemObject.CreateTextFile / OpenTextFile("File path", File mode)

File Modes for Text files
-------------------------
i) 1 for Read

ii) 2 for Write

iii) 8 for Append
----------------------------
Note: Read mode is default mode.

Examples:
---------------------
8) Read a Text File Character by Character
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
--------------------------------
9) 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
--------------------------------------
10) 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")

Do While objTextstream.AtEndOfStream = False
myContent = objTextstream.ReadAll
Msgbox myContent   
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
------------------------------------------
Dim objFso, objTextstream, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\UFT.txt")

Do While objTextstream.AtEndOfStream = False
myContent = objTextstream.ReadAll
Print myContent   
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-------------------------------------
Note: Print command is not a Global VBScript command, it is only for UFT.

11) Read Test 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\gcreddy\Desktop\testdata.txt")

objTextStream.SkipLine 'To skip the 1st line

Do Until objTextStream.AtEndOfStream
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
----------------------------------
12) Read Test data (Range of Records, ex: 4 to 7 records) from a Text file and perform Data driven Testing for Login functionality.

Dim objFso, objTextstream, myLine, myField, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt")

LineCount = 0
Do Until objTextStream.AtEndOfStream
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
Loop
objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
---------------------------------------------
13) Write data to a Text file
Dim objFso, objTextstream, num1, num2, result
num1 = 10 : num2 = 30 : result = num1 + num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 2)

objTextStream.Write "Addition of num1, num2 is: " & result

objTextstream.Close
Set objTextStream = Nothing
Set objFso = Nothing

14) Append data to a Text file


Dim objFso, objTextstream, num1, num2, result
num1 = 10 : num2 = 60 : result = num1 + num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 8)

objTextStream.Write "Addition of num1, num2 is: " & result

objTextstream.Close
Set objTextStream = Nothing
Set objFso = Nothing
------------------------------
Dim objFso, objTextstream, num1, num2, result
num1 = 10 : num2 = 80 : result = num1 + num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 8)

objTextStream.WriteLine "Addition of num1, num2 is: " & result

objTextstream.Close
Set objTextStream = Nothing
Set objFso = Nothing
------------------------------------
15) Read Button Names from Login Dialog box and export to a Text file
Dim objFso, objTextstream, oButton, Buttons, i
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 2)

Set oButton = Description.Create
oButton("Class Name").Value = "WinButton"

Set Buttons = Dialog("Login").ChildObjects(oButton)
'Msgbox Buttons.count

objTextstream.WriteLine "button Names"
objTextstream.WriteLine "---------"
For i = 0 To Buttons.count - 1 Step 1
    objTextstream.WriteLine Buttons(i).GetRoProperty("text")
Next
objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
---------------------------------------------
16) Read Link Names from Rediff home page and export to a text file
Dim objFso, objTextstream, oLink, Links, i, sno
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 2)

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

Set Links = Browser("Rediff.com: Online Shopping,").Page("Rediff.com: Online Shopping,").ChildObjects(oLink)

objTextstream.WriteLine "Link Names"
sno = 0
For i = 0 To Links.count - 1 Step 1
sno = sno + 1
    objTextstream.WriteLine sno& ") " &Links(i).GetRoProperty("text")
Next
objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
--------------------------------------------------
17) Read Customer names from 1 to 10 Records and export to a Text file
Dim objFso, objTextstream, CustomerName
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\testdata.txt", 2)

sno = 0
objtextstream.WriteLine "Customer Names"
For i = 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 i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
CustomerName = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
sno = sno + 1
objTextstream.WriteLine sno &") " & CustomerName
Next

objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
-----------------------------------------
18) Compare two text file by Size, by Text and by Binary values.
Dim objFso, File1, File2, objTextstream1, objTextstream2, Read1, Read2
File1 = "C:\Users\gcreddy\Desktop\abc.txt"
File2 = "C:\Users\gcreddy\Desktop\xyz.txt"

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

'Compare two text files by Size
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

'Compare two text files by Text
Set objTextstream1 = objFso.OpenTextFile(File1)
Set objTextstream2 = objFso.OpenTextFile(File2)

Read1 = objTextstream1.ReadAll
Read2 = objTextstream2.ReadAll

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

'Compare Two text file by Binary (ANSI character codes) Values
If StrComp(Read1, Read2, 0) = 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
--------------------------------------------------
How to work with multiple Text files?

By using multiple Text Stream objects.
-----------------------------------------------------
19) Count how many times the word "UFT" appeared in a Text file 

Dim objfso, objTextstream, objRegEx, matches, myContent
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.OpenTextFile("C:\Users\gcreddy\Desktop\abc.txt")
myContent = objTextStream.ReadAll

Set objRegEx = New RegExp
objRegEx.pattern = "UFT"
objRegEx.Global = True ' To find all matches
objRegEx.IgnoreCase = True 'To ignore cases (Considers Upper and Lower case letters)

Set matches = objRegex.Execute(myContent)
Msgbox matches.count

objTextStream.Close
Set objRegEx = Nothing
Set objTextStream = Nothing
Set objFso = Nothing
-------------------------------------------
Create Regular Expression Object
No specific Class for creating Regular Expression Object, using New and RegExp keywords we can create Regular Expression Object.

0 comments:

Post a Comment