0

A number of my tests use a DeleteCookies method which does this:

chromeDriver.Manage().Cookies.DeleteAllCookies();

This was great until today, when one of the tests made the app take exception:

The required anti-forgery cookie "__RequestVerificationToken" is not present.

So I decided to delete the specific cookie in question, rather than all of them:

chromeDriver.Manage().Cookies.DeleteCookieNamed("user%40user.com");

The specified cookie was not deleted.

To verify DeleteCookieNamed actually worked at all, I deleted two of the other cookies the app creates and it worked.

When a user logs in, the app sets the name of the cookie to their username (in this case, user@user.com) ...but presumably due to URL encoding, changes @ to %40.

The cookies that did delete didn't contain %40.

If it was the case that you can't delete cookies where the name contains %40, then DeleteAllCookies wouldn't have worked. Therefore I suspect my targeted cookie has dependencies, similar to how foreign key constraints work in relational databases, such that I need to delete one or more other cookies in a specific order before I can delete the specified one.

A cursory glance at Google provides me with instructions on how to delete cookies in Chrome, rather than how Chrome deletes cookies. Joy. I've run out of time today, please do you have any insights?

Zuno
  • 433
  • 1
  • 7
  • 17
  • I wasn't fully convinced that %40 had nothing to do with this issue so I set the cookie manually with the @ symbol and it got deleted when I called DeleteCookieNamed, and didn't when I changed the @ symbol back to %40. This still doesn't explain why DeleteAllCookies deletes a cookie with %40 in its name so there's that. Maybe there's some way to cater for URL encoded characters in cookie names when deleting them. I've run out of time again but will check. – Zuno Jun 01 '18 at 09:30

3 Answers3

0

The way I got round this issue when I had it was to create the cookie through selenium, and then I was able to delete it. I did find it easier though not to use the @ in the cookie names. Hope that helps.

lardymonkey
  • 728
  • 1
  • 16
  • 34
  • My tests require that I create the cookie both through the app and through Selenium. I have a 'CanLogIn' test which simulates the user logging in via the relevant input elements, which creates the cookie with its name containing %40, and this is the point where the other tests (that need to delete said cookie) fall over. – Zuno Jun 04 '18 at 16:02
0

This seems an appropriate work-around for my particular situation - run the CanLogIn test last, using a Playlist:

https://stackoverflow.com/a/29039261/1875540

Zuno
  • 433
  • 1
  • 7
  • 17
  • A day later, the playlist has forgotten the order in which the tests were added. This is allegedly solve-able by editing the .playlist file and moving the relevant test's tag to the end of the list, but didn't work for me. Sigh. – Zuno Jun 05 '18 at 08:06
0

Use Chrome DevTools: https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-deleteCookies

The code will be like this:

  ChromeDriver driver;


  var parameters = new Dictionary<string, object> 
  {
                ["name"] = "user%40user.com";
  };
  driver.ExecuteChromeCommand("Network.deleteCookies", parameters);

You can do this by Selenium DevTools as well. But since they have some problems with integrations, and you may need to upgrade the library after every google update, using the above code can be more efficient and easier.

Y sharifpour
  • 361
  • 4
  • 12