Cookies are those small files on your computer that store information between sessions or between two login attempts. There are valid reasons where you might need to delete co
okies stored on your computer through scripting.
Now, how can we delete cookies through VB Scripting in QTP? This one is a very often repeated question on the testing tools forums. I will first provide a hint here.
- You need to write a custom function to do this. IMHO there is no direct way available.
- Write a shell script to reach to the cookies file residing in your corresponding folder.
- Delete all the files inside that folder.
Please post your script in the comments below. If I don’t get an answer by tomorrow, I will update the post with my script.
Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!
If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.




28 comments ↓
Browser("Browser").WinToolbar("ToolbarWindow32").Press "&Tools"
Browser("Browser").WinToolbar("ToolbarWindow32").Press "&Tools"
Browser("Browser").WinMenu("ContextMenu").Select "Delete Browsing History…"
Browser("Browser").Dialog("Delete Browsing History").WinButton("Delete cookies").Click
Browser("Browser").Dialog("Delete Browsing History").Dialog("Delete Cookies").WinButton("Yes").Click
Browser("Browser").Dialog("Delete Browsing History").WinButton("Close").Click
'##################################
'#Function ID/Name: ClearCookies
'#Function Description:The Function clears all cookies that are present on your HardDrive in an Internet Transaction. '#Function Logic: The function opens the Properies Window of Internet Explorer & fires the Click Event for the Delete Cookies ' Button & Delete Temporary Internet Files.
'
' Example
'
' Call ClearCookies()
'
'#Author: Manjula Palnati
'##################################
Function ClearCookies()
SystemUtil.Run "Control.exe","inetcpl.cpl"
Set objShell = CreateObject("Wscript.Shell")
Do Until Success = True
Success = objShell.AppActivate("Internet Properties")
Wait(1)
Loop
objShell.Sendkeys "%i"
Wait(1)
objShell.Sendkeys "{ENTER}"
Wait(1)
objShell.Sendkeys "%f"
Wait(1)
objShell.Sendkeys "%d"
Wait(1)
objShell.Sendkeys "{ENTER}"
Wait(4)
objShell.Sendkeys "{ENTER}"
End Function
'key codes
Const VK_RETURN = 28
Const VK_CONTROL = 29
Const VK_DOWN = 80
Dim oBrowser
Set oBrowser = Description.Create()
oBrowser( "micClass" ).Value = "Browser"
'oBrowser( "title" ).Value = "about:blank"
oBrowser( "application version" ).Value = "internet explorer 7"
oBrowser( "version" ).Value = "internet explorer 7"
Set BrowserObject = Browser(oBrowser).object
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
Window("hwnd:=" & BrowserObject.hwnd).activate
Window("hwnd:=" & BrowserObject.hwnd).Type micNumLockOff
Window("hwnd:=" & BrowserObject.hwnd).Type micAltDwn + "t"+ micAltUp
wait(1)
' deviceReplay.PressNKeys VK_DOWN , sIndex
wait(1)
deviceReplay.PressKey VK_RETURN
Browser("Browser").Dialog("Delete Browsing History").WinButton("Delete cookies").Click
wait(1)
Browser("Browser").Dialog("Delete Browsing History").Dialog("Delete Cookies").WinButton("Yes").Click
wait(1)
Browser("Browser").Dialog("Delete Browsing History").WinButton("Close").Click
We can use WebUtil.(Method)
webutil.DeleteCookie
webutil.DeleteCookies
It’s interesting to see the solutions here. It wouldn’t have even occurred to me to write a script that actually clicks through the IE menu items to do it.
@itpayz, Basim: Nice solutions.
@Maniz: Good one. Webutil is an undocumented method (for some unknown reasons!)
@Manjula: How about reaching the cookies directory directly through the shell object you created and deleting all the files inside? That will reduce the length of code to a single statement.
@Will:
Same here. By the way for quite some time I have not seen any updates on your blog… trust everything is fine?
@ankur : i need to clarify one doubt when u said to manjula that “How about reaching the cookies directory directly through the shell object you created and deleting all the files inside? That will reduce the length of code to a single statement” ?
how to achieve this ?
Hi Ankur,
there is 1 more code to delete TEMp files,COOKIES using SHELL codes.
COOKIES = 0×21
using the above code u prepare a vbscript to delete files.. i used that script around 1 year back in my project.. now i am unable to remember the exact code.. the script was just of 5-6 lines.. can you help me in this code..
Ankur – please ignore my previous comments..
below is the code of cookies
COOKIES = &H21&
below are the constants for special folders in windows:
&H1& Internet Explorer
&H2& Programs
&H3& Control Panel
&H4& Printers and Faxes
&H5& My Documents
&H6& Favorites
&H7& Startup
&H8& My Recent Documents
&H9& SendTo
&Ha& Recycle Bin
&Hb& Start Menu
&Hd& My Music
&He& My Videos
&H10& Desktop
&H11& My Computer
&H12& My Network Places
&H13& NetHood
&H14& Fonts
&H15& Templates
&H16& All Users Start Menu
&H17& All Users Programs
&H18& All Users Startup
&H19& All Users Desktop
&H1a& Application Data
&H1b& PrintHood
&H1c& Local Settings\Application Data
&H19& All Users Favorites
&H20& Local Settings\ Temporary Internet Files
&H21& Cookies
&H22& Local Settings\History
&H23& All Users Application Data
&H24& Windows
&H25& System32
&H26& Program Files
&H27& My Pictures
&H28& User Profile
&H2b& Common Files
&H2e& All Users Templates
&H2f& Administrative Tools
&H31& Network Connections
Ankur – without creating shell object.. u can delete cookies by VBscript..
using the above code u prepare a vbscript to delete files.. i used that script around 1 year back in my project.. now i am unable to remember the exact code.. the script was just of 5-6 lines.. can you help me in this code..
below is the general code to delete cookies by creating shell obj.:
Const COOKIES = &H21&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path & "\*.*"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(strPath)
but u can delete without creating shell obj… forget that code
Hi
For IE7 browser I use next function:
Sub ClearCookies()
‘work only for IE7
Set wshshell = CreateObject(“WScript.Shell”)
WshShell.Run “Rundll32.exe InetCpl.cpl, ClearMyTracksByProcess 255″, 1, true
End Sub
To read more about clearing IE7 history from command line go to next URL:
http://www.howtogeek.com/howto/windows/clear-ie7-browsing-history-from-the-command-line/
try
webutil.deletecookies
Function Deletecookies
userName=Environment.Value(“UserName”)
Dim oShell
Set oShell=CreateObject(“WScript.shell”)
oShell.run(“cmd /C cd C:\Documents and Settings\” & userName & “\cookies & del *.txt”)
Set oshell=nothing
End Function
Browser(“Browser”).WinToolbar(“ToolbarWindow32″).Press “&Tools”
Browser(“Browser”).WinToolbar(“ToolbarWindow32″).Press “&Tools”
Browser(“Browser”).WinMenu(“ContextMenu”).Select “Delete Browsing History…”
Browser(“Browser”).Dialog(“Delete Browsing History”).WinButton(“Delete cookies”).Click
Browser(“Browser”).Dialog(“Delete Browsing History”).Dialog(“Delete Cookies”).WinButton(“Yes”).Click
Browser(“Browser”).Dialog(“Delete Browsing History”).WinButton(“Close”).Click
how to check log files?
rem ********************************
rem delete cookies
call fn_DeletesubFolderAndFiles(“C:\DOCUME~1\%USERNAME%\Cookies”)
rem *********************************
rem delete Temporary internet files
call fn_DeletesubFolderAndFiles(“C:\DOCUME~1\%USERNAME%\Locals~1\Tempor~1″)
rem ***********************************
REM *************************************************************************
REM Function fn_DeletesubFolderAndFiles(path)
Rem this Function delete the files & subfolder under the specified path
REM Input spath := Files path or parent Folder path
REM Output(ReturnType) := None
REM Note := IF file Protected then it will not delete without giving Error
REM Created: 17/April/2009 Rajiv Kumar Nandvani ## Changed:MM/DD/YYYY
REM *************************************************************************
Public Function fn_DeletesubFolderAndFiles(spath)
on Error Resume Next
rem Create File System object
set objFileSystem = CreateObject(“Scripting.FileSystemObject”)
rem Create Window Shel object
Set objWshShell = CreateObject(“WScript.Shell”)
Rem get Folderpath under which file present
Set objoFolder = objFileSystem.GetFolder(objWshShell.ExpandEnvironmentStrings(spath))
rem get count the files under the folder and loop run for delete the files
For Each oFile In objoFolder.files
On Error Resume Next
objFileSystem.DeleteFile oFile
Err.clear
Next
rem get count the subfolders under the folder and loop run for delete the subfolders
For Each oSubFolder In objoFolder.SubFolders
On Error Resume Next
objFileSystem.DeleteFolder oSubFolder
Err.clear
Next
rem clear the object
set objFileSystem = nothing
set objWshShell = nothing
set objoFolder = nothing
Err.clear
End function
Good JOb guys.
how can i block or allow all cookies in ie6/7 (privacy settings) through vb script without using windows tool options,
Hi,
Whether is it possible to copy a specific file from a folder whose foldername gets often changed while the Test runs. The file name is constant, while the folder name containing the file gets changed. { Ex:- Filename – ABC.xml, location of the file – “C:\programfile\Test\temp*2009070310109*\Runtime\ABC.xml”. The folder temp2009070310109 value often gets changed while the Test runs, and only the first few characters are constant. My Target is to copy the ABC.xml. May i know is it possible?, if so can you please send me the Code and syntax.}
Thanks in Advance.
Function clearCookies()
on error Resume Next
Const COOKIES = 33 ‘&H21&
Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path & “\*.txt”
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
objFSO.DeleteFile(strPath)
clearCookies = True
If Err.Number 0 Then
err.Clear
msgbox(“Couldn’t clear IE cookies…”)
clearCookies = False
End If
End Function
What is the correct script from these number of answers?
To delete cookies you can write a one line code.
webutil.deletecookies
webutil.deletecookie
First one deletes all the cookies. Second one deletes cookies specific to the session
Virender Singh
sudhir0101@gmail.com
‘This VBScript Made by Hackoo
I and i hope that could help ‘you !
‘it Works so fine for me
set WshShell = CreateObject(“WScript.Shell”)
user= WshShell.ExpandEnvironmentStrings(“%userprofile%”)
Set fso = CreateObject(“Scripting.FileSystemObject”)
file_path = user & “\Cookies”
WScript.echo(file_path)
Set folder = fso.getFolder(file_path)
For Each Fichier in folder.Files
If UCase(FSO.GetExtensionName(Fichier.Path)) = “TXT” Then
fso.DeleteFile(Fichier)
end if
Next
WScript.echo(“All Cookies are deleted succefuly !”)
WebUtil.deletecookies is the direct method available in QTP to delete the cookies
I dont want to write script in the qtp.
How to access data from excel sheet and do operations on the application.
For that one i need functions.
we can use the property values in the excel sheet.
can u plz help me
Hi Ankur,
Could you please let me know the script for clicking the top row in Google Search results.
I was able to write the script as follows:
SystemUtil.Run “iexplore.exe”, “www.google.com”
Browser(“title:=Google – Windows Internet Explorer”).Page(“title:=Google”).WebTable(“name:=q”).WebEdit(“name:=q”).Set “Testing Concepts”
wait(5)
Browser(“title:=Google – Windows Internet Explorer”).Page(“title:=Google”).WebTable(“name:=q”).WebButton(“name:=Google Search”).click
What would be the code for getting QTP to click on the first(top) link in the results that are returned by the search?
I would appreciate it if you could help me with this script.
Thanks,
Neeraj
Leave a Comment