How to read and write variables in ini file with VBscript

There are many ways to store variables during test run. SToring values in ini file is such a popular way. During Test Run, We need to store some values that can be reused even after we execute the test from middle of the test. Use of excel to store 2 or 3 values seems lengthy or useless.
Lets see how to read and write variables in ini file using vbscript


1. Store variables in .ini file:


Public Function setGlobal (par_name, par_value)

' Pupose: Store parameters  variable 
 
 gDATAFOLDER= Environment.Value("TestLocation or some path") &"Data"
 gPARFILENAME = gDATAFOLDER & "qtpparam.ini"


 Dim rc
Extern.Declare micInteger,"WritePrivateProfileStringA", "kernel32.dll","WritePrivateProfileStringA",micString, micString, micString, micString 
rc = Extern.WritePrivateProfileStringA("PARAMS",par_name,par_value, gPARFILENAME ) 
FnReadGlobalFile

End function
 

2. Read variables stored in .ini file:


Public Function  FnReadGlobalFile

' Pupose: To Reterive the data's from data sheet dyanmically

 Dim gDATAFOLDER 
 Dim gPARFILENAME
 
 gDATAFOLDER= Environment.Value("TestLocation or some path") &"Data"
 gPARFILENAME = gDATAFOLDER & "qtpparam.ini"
 
 Dim fArrFileLines()
 i = 0
 Set fObjFSO = CreateObject("Scripting.FileSystemObject")
 If (fObjFSO.FolderExists(gDATAFOLDER) = False) Then
  fObjFSO.CreateFolder(gDATAFOLDER)
 End If
 If fObjFSO.FileExists(gPARFILENAME)=False Then
  Exit Function
 End If
 Set fObjFile = fObjFSO.OpenTextFile(gPARFILENAME, 1)

 Do Until fObjFile.AtEndOfStream
   Redim Preserve fArrFileLines(i)
   fArrFileLines(i) = fObjFile.ReadLine
   i = i + 1
 Loop
 fObjFile.Close
 For l = Ubound(fArrFileLines) to 1  Step -1
  fTempstr=split (fArrFileLines(l),"=")
  fvar= fTempstr(0)
  fval=fTempstr(1)  
  Execute fVar & "=fVal"
 Next

 Set fObjFSO=Nothing
End Function
 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.