niemeczek
niemeczek

Reputation: 35

Waiting for deleting cookies with webdriver?

I have a question about deleting cookies using webdriver. Should I create additional code that will wait when deleting cookies is finished or this function will do it for me?

driver.Manage().Cookies.DeleteAllCookies();

I meam test will not go to next steps until all cookies will be deleted?

Upvotes: 1

Views: 3093

Answers (3)

Rushby
Rushby

Reputation: 889

As it's non-blocking, if you're reusing the same browser it can continue onto the next test before all cookies have been cleared, you can poll for the current count and if necessary attempt to clear again:

C#

while (_driver.Manage().Cookies.AllCookies.Count > 0)
{
  driver.Manage().Cookies.DeleteAllCookies();
  Thread.Sleep(100);
  
}

Upvotes: 0

Dude
Dude

Reputation: 692

In my tests, the driver.manage().deleteAllCookies() is non blocking. So i got issues that some cookies were deleted in the middle of the test.

So maybe you want to do the deleteAllCookies() in a tearDown method, or you need to wait after the call:

driver.manage().deleteAllCookies();
try {
  Thread.sleep(5000);
} catch (InterruptedException e) {
  e.printStackTrace();
}

Upvotes: 4

Madusudanan
Madusudanan

Reputation: 1017

That depends on how the Application Under Test(AUT) handles the session.Usually when we do driver.quit (see here) it should close the browser window which automatically closes the session.

If that doesn't happen then the AUT must be handling it in a different way and as you mentioned driver.Manage().Cookies.DeleteAllCookies() should clear all the cookies.You should also use clear cookies if you are running multiple tests on the same webdriver session, in that case the browser is not closed and hence session is not cleared.

In general, it is good practice to use the logout functionality of the AUT and then use clear cookies or driver.quit() as part of test cleanup.

Upvotes: 1

Related Questions