08 November 2017

How to declare a variable in one Function and use the same in different Function in UFT

Function Msg1()

    Execute "Public X : X=10"
    ExecuteGlobal "Public Y : Y=10"


End Function

Function Msg2()

    MsgBox X 'Returns empty
    Msgbox Y 'Returns 10


End Function


Call Msg1
Call Msg2



...

Function to copy files from one Folder to another based on their Extension like .txt, .bmp, .docx using VB Script

SF = "C:\Copy\SF\" 'Source Folder
DF = "C:\Copy\DF\" 'Destination Folder

Call CopyFiles(SF, DF, "txt")


'Function to copy Files based on their extension
Function CopyFiles(SF, DF, Ext)

    'SF = Source Folder
    'DF = Destination Folder
    'Ext = Extension like txt, bmp, docx
    'Example - Call CopyFiles("C:\Copy\SF\", "C:\Copy\DF\", "txt")
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fol1 = fso.GetFolder(SF)
    Set fis = fol1.Files

    For Each fs In fis

    fname = fs.Name
            
        If Ext = fso.GetExtensionName(fname) Then
            fso.CopyFile SF&fname, DF
        End If
    
    Next

End Function



...

Releasing memory space used by an Array in VB Script


Dim NumArray(9)
NumArray(0) = 20

MsgBox UBound(NumArray) 'Returns 9
MsgBox NumArray(0) 'Returns 20

Erase NumArray 'Free memory
MsgBox NumArray(0) 'Returns Empty


'For Dynamic Array

Dim DynamicArray()
ReDim DynamicArray(9) reinitializing 
DynamicArray(1) = 21

MsgBox DynamicArray(1) 'Returns 21
MsgBox UBound(DynamicArray) 'Returns 9

Erase DynamicArray ' Free memory

MsgBox UBound(DynamicArray) 'Error
MsgBox DynamicArray(1) 'Error

ReDim Preserve DynamicArray(9)

MsgBox UBound(DynamicArray) 'Returns 9
MsgBox DynamicArray(1) 'Returns Empty



'For Multidimensional Array

Dim ma(2,3,4)
ma(0,0,0) = 23
MsgBox ma(0,0,0) 'Returns 23

Erase ma

MsgBox UBound(ma,2) 'Returns 3
MsgBox ma(0,0,0) 'Returns Empty



...

AOM - Automation Object Model

'Creating QuicTest Object
Set qtapp = createobject("QuickTest.Application")

'Launching the Application
If qtapp.Launched<> Ture Then
    qtapp.Launch
End If

qtapp.Visible = True

'Connecting to ALM
If Not qtapp.TDConnection.IsConnected Then
    qtapp.TDConnection.Connect "QC_URL""Domain","Project","User","pwd"
End If

'Open a Test script
qtapp.Open "C:\UFT\Test1"True

'Get all Associated Addins and Print them all
adins = qtapp.Test.GetAssociatedAddins

For i = 1 To ubound(adins)
    msgbox adins(i)
Next

'Get the count of Actions associated with the test and Print them all
actions = qtapp.Test.Actions.Count

For i = 1 To actions
    msgbox qtapp.Test.Actions(i).Name
Next

'Get the count of Associated Function Libraries
lcount = qtapp.Test.Settings.Resources.Libraries.Count

'Find and add Library file to the test if not found
set Lib = qtapp.Test.Settings.Resources.Libraries

If Lib.Find("C:\UFT\lib.vbs") = -1 Then
    Lib.Add "C:\UFT\lib.vbs"1     'Position like 1,2
End If

'Find and add Object Repository fie to the test if not found
Set rep = qtapp.Test.Actions("Action1").ObjectRepositories

If rep.Find("C:\UFT\rep.tsr") = -1 Then
    rep.Add "C:\UFT\rep.tsr"1     'Position like 1,2
End If

'Adding recovery scenario to the Test
qtApp.Test.Settings.Recovery.Add "C:\UFT\Recover.qrs""Recover"1    'Position like 1,2

'Enabling the Recovery scenario
qtApp.Test.Settings.Recovery.Item(1).Enabled = True

'Saving the Test
qtapp.Test.Save



'*********************************
'Some Run Settings
qtApp.Test.Settings.Run.IterationMode = "rai"
qtApp.Test.Settings.Run.StartIteration = 1
qtApp.Test.Settings.Run.EndIteration = 1
qtApp.Test.Settings.Run.DisableSmartIdentification = False

'Some Log Tracking Settings
With App.Test.Settings.LogTracking
    .IncludeInResults = False
    .Port = 18081
    .IP = "127.0.0.1"
    .MinTriggerLevel = "ERROR"
    .EnableAutoConfig = False
    .RecoverConfigAfterRun = False
    .ConfigFile = ""
    .MinConfigLevel = "WARN"
End With

'***********************************


'Executing the Test
set qtest = qtapp.Test

'Performing next step if any error found
qtest.Settings.Run.OnError = "NextStep"

'Create the Run Results Options object and Save the Run Result
Set qtestResults = CreateObject("QuickTest.RunResultsOptions")

qtestResults.ResultsLocation = "C:\UFT\Res1" ' Set the Location

'Run the test 
qtTest.Run qtResultsOpt

MsgBox qtTest.LastRunResults.Status ' Check the results of the test run 

qtApp.Quit         'Close QuickTest
Set qtResultsOpt = Nothing        'Release the Run Results Options object 
Set qtApp = Nothing        'Releasing the memory


....

07 November 2017

Encrypt URIs using VB Script

SEncrypt = Escape("Https://google.com")

MsgBoxSEncrypt  'Returns 'Https%3A//google.com'



...

Use of TimeSerial Function In VB Script


Dim MyTime1

MsgBoxTimeSerial(12 - 6, -15, 0) ' Returns 5:45:00 AM.

MsgboxTimeSerial(12 - 4, -15, -20) ' Returns 7:44:40 AM.


...

Find if any number is Positive, Negative or 0 using VB Script inbuilt Function 'Sgn'

Dim MyVar1, MyVar2, MyVar3, MySign

MyVar1 = 250 : MyVar2 = -351.44 : MyVar3 = 0

MsgboxSgn(MyVar1)   ' Returns 1.

MsgboxSgn(MyVar2)   ' Returns -1.

MsgboxSgn(MyVar3)   ' Returns 0.


...

FormatDateTime Function in VB Script

MsgBoxFormatDateTime("01/01/2017 10:11:12", 0)   'Returns 01/01/2017 10:11:12


MsgBoxFormatDateTime("01/01/2017 10:11:12", 1)   'Returns Sunday, January 01, 2017


MsgBoxFormatDateTime("01/01/2017 10:11:12", 2)   'Returns 1/1/2017


MsgBoxFormatDateTime("01/01/2017 10:11:12", 3)   'Returns 10:11:12 AM


MsgBoxFormatDateTime("01/01/2017 10:11:12", 4)   'Returns 10:11


Constant
Value
Description
vbGeneralDate
0
Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
vbLongDate
1
Display a date using the long date format specified in your computer's regional settings.
vbShortDate
2
Display a date using the short date format specified in your computer's regional settings.
vbLongTime
3
Display a time using the time format specified in your computer's regional settings.
vbShortTime
4
Display a time using the 24-hour format (hh:mm).



...

List of all Vb Script Error code

VBScript Syntax Errors:

Error Number
Description
1052
1044
1053
1058
1057
1005
1006
1011
1021
1047
1025
1014
1023
1015
1010
1012
1046
1026
1049
1045
1019
1020
1050
1022
1024
1016
1017
1013
1018
1027
1028
1029
1030
1014
1039
1040
1013
1037
1038
1048
1042
1041
1051
1001
1054
1002
1055
1015



VBScript Run-time Errors:

Error Number
Description
429
507
449
17
430
506
11
48
5020
5019
432
92
5008
51
505
481
5
5021
94
448
447
445
438
451
504
503
502
424
91
7
28
14
6
35
9
5017
462
10
13
5018
500
458
450


...

AOM - Automation Object Model

'Creating QuicTest Object Set  qtapp =  createobject ( "QuickTest.Application" ) 'Launching the Application If  qtapp...