Deleting cookies

Cookies are actually files containing data that is used by websites to remember user preferences and other relevant information, such as authentication credentials. In some cases, we may need to delete these cookies in order to do the following:

  • Test if a site detects that the cookies are missing
  • Test if a site responds according to the browser's configuration (for example, it prompts for approval, and then stores the selection)
  • Test if a site responds according to the application requirements (for example, prompts for login if an authentication cookie is missing)

There are several ways to achieve deletion of cookies, but here we will show the simplest way with the undocumented WebUtil object and methods.

Getting ready

Let's take an example. From the File menu, navigate to New | Test, use the Ctrl + N shortcut, or use the Web_Functions.vbs library you created in the previous recipe, to encapsulate the commands in your own custom functions.

How to do it...

To delete all cookies, write the following code:

WebUtil.DeleteCookies()

To delete a specific cookie from a domain, write the following code:

WebUtil.DeleteCookie(Domain, Cookie)

To encapsulate the commands in custom functions, write the following functions in the function library:

Function DeleteCookies()
    WebUtil.DeleteCookies()
End Function
Function DeleteCookie(ByVal Domain, ByVal Cookie)
    WebUtil.DeleteCookie(Domain, Cookie)
End Function

Note

The WebUtil object operates only with an Internet Explorer (IE) browser. To delete cookies for other browsers, a custom function needs to be programmed to find the correct folder in which these are stored and then perform their deletion.

How it works...

The previous code is self-evident. The first statement is equivalent to choosing to delete cookies through the IE Internet Options in the Control Panel window by navigating to Browsing history under the General tab, and then clicking on the Delete… button. This opens a Delete Browsing History dialog. The second statement deletes a specific cookie for a given domain. In that case, the name of the cookie must be accurate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset