Back to Resources

Blog

Posted November 16, 2018

Selenium Tips: Efficiently Removing Cookies

quote

Let’s say you have a simple SaaS application with a setup that starts a browser and logs in. Towards the end of the test, the cleanup logs the user out, so the next test will run on time. But something goes wrong, and logout does not happen. The next test will go to the wrong screen, a screen with no username, password, or submit button field, causing long delays as the test waits for elements, false errors, and likely both.

The simple answer for this in FireFox and Chrome is to driver.quit at the end of each test. The new session will lose all the cookies. You might, however, not want to pay the cost of shutting down and restarting the browser. In that case, you could use the driver.clear_all_cookies() method.

That works fine for the webkit browsers, but what about internet explorer and edge?

For those devices, things aren’t nearly as straightforward.

As of this publication, with sample code in ruby, those commands simply don’t work for Edge or Internet Explorer. Neither do the commands to get the cookies - see this in the call to all_cookies in the same code gmail_login_windows.rb. We may explore cookies through webdriver later. For now, know they don’t work for the microsoft browsers.

Alternatives Under Windows

Today we’ll provide two examples. For internet explorer, consider shelling out to the command line to remove cookies:

system(“RunDll32.exe InetCpl.cp,ClearMyTracksByProcess 2”);

For Edge, the simplest way is to go into browser settings, delete cookies, then click the button to clear the elements every time the browser quits.

It may also be possible to use the registry editor to delete cookies. Likewise, the example above uses some explicit waits, because the implicit waits fail. To earn some reputation, check some code into github that delete the cookies/cache, perhaps as a batch file. Or suggest a workaround for the explicit waits. Your solution could be featured in a future Selenium tip, and we’ll send you a $100 amazon gift card.

Another alternative, of course, let someone else figure it out renting the web browser used for testing. For now, though, here’s how to do it yourself.

Published:
Nov 16, 2018
Topics
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.