VBScript in UFT

VBScript is used as a scripting language in UFT. Here are some important concepts in VBScript.

Variables

Variable declarations are optional in VBScript but it is recommended to declare variables using dim keyword. In one of the UFT scenarios, I was getting error saying "Runtime error - Object doesn't support this property or method:". After declaration of variable, I was able to fix the issue.

Language constructs

Functions

Here is the syntax to create function.


' as you can see we have passed 2 parameters a and b. 
' parameters can be passed by value or by ref
' By default, Primitives are passed by value and objects are passed by ref.


function sum(byref a, byref b)
  'to return a primitive use below syntax. Note that return value is
   assigned to function name
  sum = cint(a) + cint(b)
end function

x = sum(1,2)


  ' If you want to return an object, you need to use below syntax.
  ' If you do not use "set" keyword, you will get error saying "Object Required"
  ' set FunctionName1 = obj1
function getDictionary()
    Set dictionaryObject = CreateObject("Scripting.Dictionary")
    dictionaryObject.CompareMode = 1
    dictionaryObject.Add "Name", "Salunke"
    'If you use set keyword and do not assign object, you will get below error
    'runtime error: Wrong number of arguments or invalid property assignment
    set getDictionary = dictionaryObject
end function 

'If function does not retun an object, you will get error "Object Required"
set d1 = getDictionary()    
Dim StdOut : Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
a = d1.keys              'Get the keys
b = d1.Items
For i = 0 To d1.Count -1 'Iterate the array
    Stdout.WriteLine a(i) & " - " & b(i)          'Print key
Next

Primitives and Objects

Web development and Automation testing

solutions delivered!!