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)

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("http://www.telerik.com");

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", "/", "http://www.telerik.com"));

Deleting Cookies

Here is an example of how to delete a cookie:

// Now delete the cookie.
ActiveBrowser.Cookies.DeleteCookie(siteCookies[0]);

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(ActiveBrowser.Url);
foreach (System.Net.Cookie cookie in cookies)
{
    ActiveBrowser.Cookies.DeleteCookie(cookie);
}