Selenium WebDriver and starting Safari in a clean session state

I first used selenium on a java project a while back and selenium has grown by leaps and bounds. With continued support and a myriad of bindings, it’s a tool to go to for automated testing. However, it does sometimes stumps you with nuances you’ve not seen in the past. One such quirk I came across this week was starting safari in a clean session state. Unlike Chrome and Firefox, it’s impossible to start safari in a clean session state. A clean session state means devoid of any hangovers, cookies, for instance is a good example.

The project I was working on was set up with cucumber features using capybara that in turn uses selenium webdriver’s ruby bindings for communicating with the browser. Like any astute developer, I googled for solutions.


Google search for ‘safari in clean session state’ may throw you off into a completely different directions, for instance:

1. Pass clean_session state as true as an additional option when a new capybara driver is registered for safari.

However, this will not work. Why? Because, there’s a comment in selenium webdriver’s code pointing out the fact that it’s still outstanding.

2. Open safari with different flags like ‘-n -b -F’.

This doesn’t work either. Even if it did, you will have to delve deep in selenium webdriver land to pass that option in and monkey patch a solution for this. Thankfully, it doesn’t work because monkey-patching is never a reliable way to fix bugs.

So, what’s the solution?

Well, the only way to fix it is by deleting the cookies before a feature run. Typically, add some code to delete cookies in a before hook for capybara to run before each feature.

It took a lot of trying, experimentation and patience to find this solution and sincerely hope it helps someone.

About andhapp

Namastey (Hello) Stranger, This is not the geekiest blog on the planet yet it is not the dumbest one either. I am a small time developer who likes to explore new technologies. I usually program in Java, Groovy and Ruby and take keen interest in the surrounding geeky developments.
This entry was posted in Programming. Bookmark the permalink.

2 Responses to Selenium WebDriver and starting Safari in a clean session state

  1. Amol Mithari says:


    I am struck on starting Safari in a clean session state. Could you please help me with it?

    I have tried different ways like setting desired capabilities cleanSession=true, ensureCleanSession=true etc. But the Safari always starts in the same session

  2. hi,
    thanks for sharing the very informative article.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>