Silktest Question 16: How to delete cookies files using the 4test language?

I created a 4test script to test our web based application and would like to know how to delete a specific cookie file during script execution?


The following 4test function can be used for deleting specific cookie file:

[-] RemoveCookies (String sPath, String sFile)
[ ] //*******************
[ ] //* The function for removing a stored web cookie file.
[ ] //* sPath The complete path to the directory where the cookie is stored
[ ] //* Example "C:\Documents and Settings\Administrator\Cookies\"
[ ] //* sFile unique part of the cookie file name
[ ] //* for example "*winrunner*"
[ ] //*******************
[ ] LIST OF FILEINFO lfInfo
[ ] LIST OF STRING lsFileName
[ ] INTEGER iCount
[ ] STRING sLFile
[ ] Boolean bFound
[ ]
[ ] lfInfo = SYS_GetDirContents (sPath) //obtain directory content
[-] for iCount = 1 to ListCount(lfInfo) //for every FILEINFO in lsInfo
[ ] ListAppend(lsFileName, lfInfo[iCount].sName) //append the name of the files
[ ]
[-] for each sLFile in lsFileName //for each file name in lsFileName
[ ] bFound = MatchStr (sFile, sLFile) //check if the sFile string is in the File Names
[-] if bFound == TRUE //if the sFile string is in file name
[ ] SYS_RemoveFile (sPath + sLFile) //delete the file from the sPath directory
[ ]

You can easily modify this 4test function to remove all files in the particular directory or within a loop delete a list of files.

2 comments:

David said...

This is an excellent way of getting rid of the cookie files, but it does nothing for the state of the cookies in memory. Even if you remove the file, the browser will still remember the logged-in cookie state of the AUT.
Also, the location of the cookies will be different depending on the user logged-in and the OS. This is what I did to get around that.
[ ] SYS_SetDrive("c")
[ ]
[ ] //Point this to the Dir you wish to remove all the files from
[ ] SYS_SetDir(Right(SYS_GetEnv("USERPROFILE"), Len(SYS_GetEnv("USERPROFILE"))-2)+"\Cookies")

MP said...

interesting. I will try. Thanks.

SilkTest interview questions for QA Testers