New to Telerik Test Studio? Download free 30-day trial

Cookie Support

Using the CookieManager class you can:

  • Retrieve cookies for a particular website from the browser

  • Set the contents of a cookie in the browser

  • Create a new cookie in the browser

  • Delete a cookie in the browser

Retrieving Cookies

There are two functions that can be used to retrieve cookies from the browser:

public CookieCollection GetCookies (string url)
public CookieCollection GetCookies (Uri uri)
Public Function GetCookies(ByVal url As String) As System.Net.CookieCollection
Public Function GetCookies(ByVal uri As System.Uri) As System.Net.CookieCollection

These functions retrieve all the cookies from the browser for the specified website. For example:

// Query the cookie
System.Net.CookieCollection siteCookies = ActiveBrowser.Cookies.GetCookies("");
' Query the cookie
Dim siteCookies As System.Net.CookieCollection = ActiveBrowser.Cookies.GetCookies("")

Creating and Setting Cookies

Below is an example of how to create or set a cookie. If the cookie already exists, it will be overwritten. If it does not exist, a new cookie will be created in the browser:

// Let's create a new cookie for a url.
ActiveBrowser.Cookies.SetCookie(new System.Net.Cookie("WebAii", "Rocks", "/", ""));
' Let's create a new cookie for a url.
ActiveBrowser.Cookies.SetCookie(New System.Net.Cookie("WebAii", "Rocks", "/", ""))

Deleting Cookies

Here is an example of how to delete a cookie:

// Now delete the cookie.
' Now delete the cookie.

A simple foreach loop can be used to delete all the cookies for a particular website:

// Purge any cookies associated with this server
System.Net.CookieCollection cookies = ActiveBrowser.Cookies.GetCookies(Settings.Current.BaseUrl);
foreach (System.Net.Cookie cookie in cookies)
' Purge any cookies associated with this server
Dim cookies As System.Net.CookieCollection = ActiveBrowser.Cookies.GetCookies(Settings.Current.BaseUrl)
For Each cookie As System.Net.Cookie In cookies
Next cookie
In this article