Automating Using QTP Day4 Some VbScripting Techniques

We will also discuss some other object types used in VBscript programming, rather scripting so as to be familiar with Automation Objects in QTP. Such types are as such Filesystem objects, Database ADODB objects ,Scripting dictionary objects which are discussed at length as under.
Variables in VBscript :
Array can contain both types of data in it. It is not confined  to any particular datatype just because  it has only datatype as variant. Hence it can have both types of generic data in it. A single array may contain a collection of integers as well as string, and double value in it.

FSO - Naming Convention for Filesystem Objects. Usedin general for the file handling operation in windows environment and generally very useful for QTP Automation.

DataObJ - Dataobj is generically used for creating objects using VBscriot such as Word applications or Internet Explorer as well. We use CreateObject utility for creating an instance of the same.

ADo Objects - ADODB objects are used for creating database objects, It is used for creating Connection objects as well as Recordset objects.
For vbscript Do until loop shows that the code block will be executed until condition becomes true. This very feature can be brought to enormous use in our coding mechanism. Do..Until.. loop is actually very under utilised by the Automation coders.
Built in Functions : VBScript has lots of utility functions in addition to objects we discussed above. Below are the further classifictaion of those utility functions.
  • Date Time function
  • Conversion Function
  • Format Function
  • Math
  • Array
  • STring
Procedures are the code blocks which can be further classified into Sub Routines as well as Functions
Functions  - Used for getting some return values from the called function
Subroutine - Used for just segregating some code blocks from others.

Isemty is a function to check if the expression is returniing some empty ? Return boolean

Option Explicit
: Once this is written all variables needs to be explicitly Dim a.
  • Used to bring in a disciplined coding standards.
  • It is used for enforcing the forced declaration.

Some other very commonly used built in functions are as under :
Eval is used even for function calls.
Format Functions :

txt="Hello World"
msgbox (a(0))
msgbox (a(1))
Now is Current date time.

the Built in Function ? Not possible at all.

Cint(32.568)  - Gives output as 33 after rounding off the decimal values.
Format it y rounding the data
Cdate("Oct 19,2010")
String Functions  :
LCase   : Converts the string into its lower case
uCase : Converts the string into its Upper case
Ltrim : Trims the string with the preceeding space characters
Rtrim : Trims the string with the suffixing space characters
Trim : Trims the string with the both prexceeding as well as suffixing space characters
Len(String)  : Returns the length of the string

Split and Join are used for vice versa activities.
instr(var,"y") :Returns the position of "y" in the string var
Sub Routines  :
  •  N number of arguments
  • Can operform actions but cannot return values.
  • Without argument must contain empty braces.
Sub NewSunRoutine()
 code blocks
End Sub
Function NewFunction()
 code blocks
 NewFunction=some value
End Function
  •             Returns a value by assiging it a value

  1. An object is a collection of variables and functions or methods
  2. Object.method or object.parameter()
Keyword SEt is required to instantiate an object.
Set Variable = Object

Two types of objects in VBScript:

  • Run Tinme objects.
  • Built in Objects.

The process of making connections to the objects is called Binding.
The keyword is CreateObject, GetObject to create a reference to the Objects.No OOPs Concept.These objects are called Automation Objects.
We generally use this for asssociating set of variables to some values .
Dim ObjDict
Set ObjDict=CreateObject("Scripting.Dictionary")

You just have to know the library and class name. of  the object so that they can be passed to create object.
Object that stores data key,item pairs
Item can be any data vales.
Item is stored in an array
Key is uniquely asssociated to the each item.
Key is used to access data
key is usually integere or char.

Dim myDi 
Set myDi=CreateObject("Scripting.Dictionary")
myDi.Add 203936,"Viplav"   'Add some keys and items
myDi.Add 165451,"Aswini"  
myDi.Add 177826,"Janani"  
For each Item in myKeys
 Msgbox(Item )  'Returns the array to see individual key values

The loop above automatically parses each ketys wityhin the myDi object

Utiliy of Referennce Object :
Open A Win Word Application
Dim myWord
SEt myWord=createObject("word.application")
Open A Internet EXplorer Application
Dim myIe
SEt myIe=createObject("InternetExplorer.application")
myIe.navigate ""

Handling fileSystem Objects:
It is nmainly used for manipulating files. Filesystem object is a kind of master object that serves as the access point for a family of objects.
Files folders and drives can be handled .
Dim fso,f1,ts
const forWriting = 2
Set fso= CreateObject("scripting.FilesystemObject")
Set f1=fso.getfile("c:\Test1.txt")
Set ts=f1.openAsTextStream(ForWriting,True)
ts.write("Hi Viplav")

1 - read
2- writng
8 - appending
For reading the contents :
Set f=fso.opentextfile("",Forreading)
Do while Not f.endoftream
 msgbox (ReadFromfile)

Database Connection Objects API are used for daabase technology for lanaguage indepeendent modeling of dtaa aceessin
ADODB has two objects connection and commands, Recordset
DSN is nothing but Data Source name.
ACtivex being ageebneric mmodel hence all database objects can be accessed.It is based on the COm object modeling.
ADo connection object creates a connection to a  datasource
Database can  be accessed and manipulated through this.
Recordsets are records based on  such query.
strSQLQuery="Select * from Tablename"
Set conn= createobject("ADODB.Connection")
Set rs= createobject("ADODB.REcordset") strSQLQuery,conn
Errors :
Syntax  Cmd trying do wrong action
On error Resume Next is used for the exception handling in case it arises.


Post a Comment