Cookies not being respected?

4 posts, 0 answers
  1. Tony
    Tony avatar
    5 posts
    Member since:
    Dec 2010

    Posted 11 Feb 2011 Link to this post

    Hey all,

    I have a problem with adding cookies to a browser.  I basically set my cookie:

    CookiesManager cookiesManager = Manager.ActiveBrowser.Cookies;
     
    Cookie myCookie = new Cookie();
    myCookie.Name = "MyCookieName";
    myCookie.Value = "SomeLongString";
    myCookie.Domain = "Domain";
    myCookie.Path = "/";
     
    cookiesManager.SetCookie(myCookie);

    I can see the cookie created in the browser (using FireFox I go to Options -> Privacy -> History -> Use custom settings for history -> Show Cookies).

    The problem is, the cookie is NOT added as part of the HTTP request for the next url I navigate to.  Is this a bug, am I doing something wrong? 
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 15 Feb 2011 Link to this post

    Hello Tony,
       I tried to reproduce this issue.

    I create a test in which I set a Cookie. I used your code for this. After that I added a few navigation steps. However, I was still able to find my Cookie after executing each navigation step (I checked in the same way as suggested by you - FireFox I go to Options -> Privacy -> History -> Use custom settings for history -> Show Cookies).

    Could you please send me an entire test that demonstrates the issue?

    Hope to hear from you soon!

    All the best,
    Stoich
    the Telerik team
    Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
  3. Tony
    Tony avatar
    5 posts
    Member since:
    Dec 2010

    Posted 15 Feb 2011 Link to this post

    Hey Stoich,

    Thanks for the reply.  The problem isn't the issue with the Cookie not being created in FireFox, in fact I too see the Cookie in the "Show Cookies" option.

    The problem is, I have this cookie in place, but the cookie is not being respected by the browser.  The browser sees the cookie, but isn't doing anything with it.  It just sees it, and knows it's there.  It's not adding that cookie as part of my HTTP Requests.  (I confirm this by checking my net tab of FireBug and the cookie is not part of the HTTP Request Header).
  4. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 18 Feb 2011 Link to this post

    Hello Tony,
       I have a piece of code which might work for you. First add these methods:
          public void Authorize_Admin_user(BaseWebAiiTest test, string baseUrl)
            {
                string url = baseUrl + "/Default.aspx";
                // Safari and Chrome have to have a page loaded before it will do anything
                if (test.Manager.ActiveBrowser.BrowserType == BrowserType.Safari || test.Manager.ActiveBrowser.BrowserType == BrowserType.Chrome)
                {
                    test.ActiveBrowser.NavigateTo(url);
                }
     
                var ticketCookie = admin_cookie(url);
                ticketCookie.Domain = new Uri(test.Manager.Settings.BaseUrl).Host;
                ticketCookie.Expires = System.DateTime.Now.AddDays(1);
     
     
                SetCookie(test, url, ticketCookie);
            }
     
            public Cookie admin_cookie(string url)
            {
                CookieCollection admin = v1ticket(url, "admin", "admin");
     
                Cookie admin_cookie = admin[0];
     
                return admin_cookie;
    }
            private CookieCollection v1ticket(string url, string username, string password)
            {
                Uri app_uri = new Uri(url);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(app_uri);
                req.Headers.Add("V1-Username", username);
                req.Headers.Add("V1-Password", password);
                req.CookieContainer = new CookieContainer();
                req.Method = "POST";
                req.AllowAutoRedirect = false;
                //req.ContentType = "application/x-www-form-urlencoded";
        
                Stream requestStream = req.GetRequestStream();
                string postData = "";
                byte[] byteArray = Encoding.ASCII.GetBytes(postData);
     
                requestStream.Write(byteArray, 0, byteArray.Length);
     
                requestStream.Close();
                requestStream.Dispose();
     
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                CookieCollection v1ticket = resp.Cookies;
                resp.Close();
     
                return v1ticket;
     
            }
     
            private static void SetCookie(BaseWebAiiTest test, string url, Cookie ticketCookie)
            {
                
                test.Log.WriteLine(LogType.Information, "Got Ticket Cookie=" + ticketCookie.ToString());
                bool succeeded = test.ActiveBrowser.Cookies.SetCookie(ticketCookie);
                if (!succeeded)
                    throw new Exception("Failed to set ticket cookie");
            }
    and then here's how you use these methods to set a cookie:
    Cookie theCookie = new System.Net.Cookie("CMTEST", "1", "", "http://www.telerik.com");
    SetCookie(this, "http://www.telerik.com", theCookie);

    This code might work better for you than what you're currently trying to use.
    Let me know how it goes!

    Best wishes,
    Stoich
    the Telerik team
Back to Top