Search code examples
rubycucumberwatirwatir-webdriver

Unable to clear cookies using browser.cookies.clear


I am unable to clear cookies using watir-webdriver and browser.cookies.clear Are there any other alternatives ?


Solution

  • This is as far as I know a browser based limitation, due to concerns of security and privacy.

    Webdriver is interacting with the browser by javascript, and javascript is not allowed to clear all cookies (think how nasty that could be on a malicious site). In the non testing env, most JS that is executing came from the server of the site you are accessing. So the most it is allowed to do is clear the cookies for the 'current domain' e.g. the cookies belonging to the current URL. So if a web page wants to clear all its own cookies, that is OK, but it is not allowed to clear cookies belonging to other domains.

    So if you want all your tests to start with fresh cookies, you will need something in the 'Before' section in env.rb that goes to the root of the site in question, and then clears the cookies

    BTW the same limitation applies to setting cookies, if you want to create cookies for a specific site, you need to navigate to that site before trying to create them, or nothing gets created.