Search code examples
c#asp.net-mvc-3internet-explorerwatinsession-cookies

Expiring an IE session using WatiN


I'm trying to write an acceptance test using WatiN which checks that a user is redirected to the login page if they navigate to a page after their session times out. I'm using WatiN's IE class for the browser, and trying the following:

// 1. Login

// 2. Do this:
Browser.ClearCookies();
Browser.ClearCache();

// 3. Navigate to a different page

But the user is always still logged in. Other info:

  1. I'm running the test through the NUnit GUI running as an administrator
  2. It's an ASP.NET MVC 3 site, using forms authentication and in-process session state
  3. I'm using IE9.
  4. If I manually clear all cookies in Chrome, the user is logged out
  5. If I manually clear all cookies in IE the user stays logged in
  6. If I call Browser.Eval("alert(document.cookie)"); in IE it alerts an empty string

Given the above, I'm assuming this is a quirk with IE; any ideas how I can work around it?


Solution

  • It turns out this is due to the way IE deals with sessions and the deletion of authentication cookies; I found buried in this blog that you can clear authentication information in IE by making WatiN execute the following JavaScript:

    Browser.Eval(@"document.execCommand(""ClearAuthenticationCache"", false);");
    

    This works! After this line executes the session is cleared and the next page navigation redirects to the login screen.