B VBScript11
B VBScript11
Session 11
Dani Vainstein 1
What we learn last session?
Dani Vainstein 2
Subjects for session 11
Dani Vainstein 3
OOP Model
Abstractions
Encapsulation
Polymorphism
Inheritance
Reusabillity
Dani Vainstein 4
OOP Model
Abstractions
Dani Vainstein 5
OOP Model
Encapsulation
Dani Vainstein 6
OOP Model
Polymorphism
Dani Vainstein 7
OOP Model
Polymorphism
• You might have a class named Road which calls the Drive
method of an additional class.
• This Car class may be SportsCar, or SmallCar, but both
would provide the Drive method.
• Though the implementation of the Drive method would
be different between the classes, the Road class would
still be able to call it, and it would provide results that
would be usable and interpretable by the Road class.
Dani Vainstein 8
OOP Model
Inheritance
Dani Vainstein 9
Dimensions Area
Flanks Shape
Perimeter
Radious Height
*pi Circle Rectangle
angles Width
Triangle
Where is the
right place for
Volume
this action? depth
3D
Dani Vainstein 10
OOP Model
Reusabillity
Dani Vainstein 11
Object Model
Object.
Collection.
Container object.
Method.
Event.
Property.
Attribute.
Dani Vainstein 12
Object Model
Objects
Dani Vainstein 13
Object Model
Collection
Dani Vainstein 14
Object Model
Container Object
Dani Vainstein 15
Object Model
Methods
Dani Vainstein 16
Object Model
Methods
Dani Vainstein 17
Object Model
Event
Dani Vainstein 18
Object Model
Property
Dani Vainstein 19
Object Model
Attribute
Dani Vainstein 20
COM
Component Object Model
Dani Vainstein 21
COM
IUnknown
Dani Vainstein 22
COM
COM Class
Dani Vainstein 23
COM
COM Interface
Dani Vainstein 24
COM Objects
COM Extensions Technologies
COM+(Component Services )
ActiveX, OLE
SOM, DSOM – IBM’s System object model
DCOM (Distributed COM)
Distributed COM
CORBA – Common Object Request Broker
Architecture
RMI, JavaBeans – SUN Microsystems
technologies.
RPC – Remote Procedure Call.
Dani Vainstein 25
COM objects
Where they are?
Dani Vainstein 26
Set statement
Assigns an object reference to a variable or property, or associates a procedure
reference with an event.
To be valid, object must be an object type consistent with the object being
assigned to it.
The Dim, Private, Public, or ReDim statements only declare a variable that
refers to an object.
No actual object is referred to until you use the Set statement to assign a
specific object.
Generally, when you use Set to assign an object reference to a variable, no
copy of the object is created for that variable.
Instead, a reference to the object is created.
More than one object variable can refer to the same object.
Because these variables are references to (rather than copies of) the object, any
change in the object is reflected in all variables that refer to it.
Dani Vainstein 27
Nothing Keyword
The Nothing keyword in VBScript is used to disassociate an object
variable from any actual object.
Use the Set statement to assign Nothing to an object variable. For
example:
Set MyObject = Nothing
Several object variables can refer to the same actual object.
When Nothing is assigned to an object variable, that variable no
longer refers to any actual object.
When several object variables refer to the same object, memory and
system resources associated with the object to which the variables
refer are released only after all of them have been set to Nothing,
either explicitly using Set, or implicitly after the last object variable
set to Nothing goes out of scope.
Dani Vainstein 28
CreateObject Function
CreateObject(servername.typename [, location])
Dani Vainstein 29
CreateObject Function
Dani Vainstein 30
CreateObject Function
Once an object is created, refer to it in code using the object variable you
defined.
you can access properties and methods of the new object using the object
variable.
Creating an object on a remote server can only be accomplished when
Internet security is turned off.
You can create an object on a remote networked computer by passing
the name of the computer to the servername argument of
CreateObject.
That name is the same as the machine name portion of a share name.
For a network share named "\\myserver\public", the servername is
"myserver". In addition, you can specify servername using DNS format
or an IP address.
Dani Vainstein 31
CreateObject Function
Summary
Creating an object
Set myDoc = CreateObject (“Word.Document”)
Using The object
MyDoc.content = “abc”
MyDoc.SaveAs “Doc1.doc”
MyDoc.Close
Free the object
Set Mydoc = Nothing
Type COM
Library Class
Dani Vainstein 32
FileSystemObject Object
Basics
Dani Vainstein 33
New Keyword
Keyword used to create a new instance of a class.
If objectvar contained a reference to an object, that
reference is released when the new one is assigned.
The New keyword can only be used to create an instance
of a class.
Using the New keyword allows you to concurrently
create an instance of a class and assign it to an object
reference variable.
The variable to which the instance of the class is being
assigned must already have been declared with the Dim
(or equivalent) statement.
Dani Vainstein 34
FileSystemObejct Object
Model
The FileSystemObject (FSO) object model allows you to use the
familiar object.method syntax with a rich set of properties, methods,
and events to process folders and files.
The FSO object model gives to the QTP the ability to create, alter,
move, and delete folders, or to detect if particular folders exist, and if
so, where. You can also find out information about folders, such as
their names, the date they were created or last modified, and so forth.
The FSO object model also makes it easy to process files. When
processing files, the primary goal is to store data in a space- and
resource-efficient, easy-to-access format.
You need to be able to create files, insert and change the data, and
output (read) the data.
Dani Vainstein 35
FileSystemObejct Object
Model
Dani Vainstein 36
FileSystemObject Objects
Main
Main object.
Contains methods and properties that allow you to create,
delete, gain information about, and generally manipulate
drives, folders, and files.
Many of the methods associated with this object duplicate
those in other FSO objects; they are provided for
convenience.
Dani Vainstein 37
FileSystemObject Objects
Drive Object
Dani Vainstein 38
FileSystemObject Objects
Drive Collection
Dani Vainstein 39
FileSystemObject Objects
File Object
Dani Vainstein 40
FileSystemObject Objects
Files Collection
Dani Vainstein 41
FileSystemObject Objects
Folder Object
Dani Vainstein 42
FileSystemObject Objects
Folders Collection
Dani Vainstein 43
FileSystemObject Objects
TextStream Object
Dani Vainstein 44
Programming the
FileSystemObject
Dani Vainstein 45
Accessing Existing Drives, Files,
and Folders
Dani Vainstein 46
Accessing the Object's
Properties
Dani Vainstein 47
Working with Drives and
Folders
Dani Vainstein 48
Getting Information About
Drives
Dani Vainstein 49
Getting Information About
Drives
The total size of the drive in bytes (TotalSize property).
How much space is available on the drive in bytes (AvailableSpace or
FreeSpace properties).
What letter is assigned to the drive (DriveLetter property).
What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM
disk (DriveType property).
The drive's serial number (SerialNumber property).
The type of file system the drive uses, such as FAT, FAT32, NTFS, and so forth
(FileSystem property).
Whether a drive is available for use (IsReady property)
The name of the share and/or volume (ShareName and VolumeName
properties)
The path or root folder of the drive (Path and RootFolder properties)
Dani Vainstein 50
Working With Folders
Create a folder - FileSystemObject.CreateFolder
Delete a folder - Folder.Delete or FileSystemObject.DeleteFolder
Move a folder - Folder.Move or FileSystemObject.MoveFolder
Copy a folder - Folder.Copy or FileSystemObject.CopyFolder
Retrieve the name of a folder - Folder.Name
Find out if a folder exists on a drive - FileSystemObject.FolderExists
Get an instance of an existing Folder object - FileSystemObject.GetFolder
Find out the name of a folder's parent folder -
FileSystemObject.GetParentFolderName
Find out the path of system folders - FileSystemObject.GetSpecialFolder
Dani Vainstein 51
Working With Files
Dani Vainstein 52
Creating Files
Dani Vainstein 53
Adding Data to the File
Once the text file is created, add data to the file using the following
three steps:
Open the text file.
Write the data.
Close the file.
To open an existing file, use either the OpenTextFile method of the
FileSystemObject object or the OpenAsTextStream method of the
File object.
To write data to the open text file, use the Write, WriteLine, or
WriteBlankLines methods of the TextStream object according to
your task.
To close an open file, use the Close method of the TextStream
object.
Note The newline character contains a character or characters to
advance the cursor to the beginning of the next line. Be aware that
the end of some strings may already have such nonprinting
characters.
Dani Vainstein 54
Reading Files
Dani Vainstein 55
Moving, Copying, and Deleting
Files
Dani Vainstein 56
Lab 11.1
Tip
Const DRV_UNKNOWN = 0
Const DRV_REMOVABLE = 1
Const DRV_FIXED = 2
Const DRV_NETWORK = 3
Const DRV_CDROM = 4
Const DRV_RAMDISK = 5
Dani Vainstein 57
Lab 11.2
Declare the follow constants :
Constants returned by Drive.DriveType
Const conDriveTypeRemovable = 1
Const conDriveTypeFixed = 2
Const conDriveTypeNetwork = 3
Const conDriveTypeCDROM = 4
Const conDriveTypeRAMDisk = 5
Constants returned by File.Attributes
Const conFileAttrNormal = 0
Const conFileAttrReadOnly = 1
Const conFileAttrHidden = 2
Const conFileAttrSystem = 4
Const conFileAttrVolume = 8
Const conFileAttrDirectory = 16
Const conFileAttrArchive = 32
Const conFileAttrAlias = 64
Const conFileAttrCompressed = 128
Constants for opening files
Const conOpenFileForReading = 1
Const conOpenFileForWriting = 2
Const conOpenFileForAppending = 8
Dani Vainstein 58
Lab 11.2
Write the following functions.
ShowDriveType(objDrive) - Generates a string describing the
drive type of a given Drive object.
ShowFileAttr(objFile) - Generates a string describing the
attributes of a file or folder.
GenerateDriveInformation(objFSO) – reports about the Drive
Letter,Path, Type, IsReady, ShareName, VolumeName, TotalSize,
FreeSpace, AvailableSpace, and SerialNumber.
GenerateFileInformation(objFile) – File Name, Type, File
Attributes, Date Created, Last Accessed, Last Modified and Size
GenerateFolderInformation(objFolder) – Folder Name, Folder
Attributes, Date Created, Last Accessed, Last Modified and Size
Dani Vainstein 59
Lab 11.1
Create a Folder in the D:\ drive, if not exist in
C:\ Drive.
The name of the folder is VBSInfo.
Create a file in the folder, TestInfo.txt
The file will contain the report of the program.
The data will displayed like a table (rows and
columns with headers) use the vbTab for
separate the data.
For getting information about the directories
and files, please DON’T do it an all drives, select
only the drive were you created your file.
Dani Vainstein 60
Lab 11.1
Create a Folder in the D:\ drive, if not exist in
C:\ Drive.
The name of the folder is VBSInfo.
Create a file in the folder, TestInfo.txt
The file will contain the report of the program.
The data will displayed like a table (rows and
columns with headers) use the vbTab for
separate the data.
For getting information about the directories
and files, please DON’T do it an all drives, select
only the drive were you created your file.
Dani Vainstein 61
What’s Next
Dani Vainstein 62
Make sure to visit us
Tutorials
Articles
Proikects
And much more
www.AdvancedQTP.com
63