Selenium delete cookie
I am using Selenium via Java.
My test case logs into an account, but when the next test case runs (which happens to also log into an account) it fails since the credentials are still saved from the previous test case.
So, I used the driver.manage().deleteAllCookies() method, however, this method does NOT delete all cookies thus does not delete the cookie I want to be deleted. So I used the delete driver.manage().deleteCookieNamed("facebook") method but it does NOT delete the facebook cookie because it determines it not to exist for some reason when in fact it does exist.
My questions are, how does the selenium cookie method determine what cookies exist? My second question is, how can I delete this cookie? My third question is, if I can't delete this cookie, what other options do I have to delete the browser data in order for my test case to pass?
When you call
driver.manage().deleteAllCookies()it deletes all cookies for the current domain. So if you're on bbc.co.uk page it will not be able to remove cookies for facebook.com domain.
driver.manage().deleteCookieNamed(cookieName)can delete only a cookie for a current domain. It's stems from the the way Web browsers work, WebDriver cannot do more.
There are the following solutions to that:
driver.get(url)get to every URL you want cookies to be deleted for and then delete them.
WebDriverinstance from scratch.
- Click Logout link or go to logout URL, specific for your application. This will delete session cookies.
In my opinion, each test should start with a fresh browser (solution 2), unless there's some explicit dependency between tests. Solution 3 does not warranty session cookies will be deleted from a browser, if the application you are testing does not handle logout properly.