Earlier we already learn different selenium webdriver methods to add cookie, get cookies detail by name and all cookies. Sometimes you also need to delete all cookies in selenium webdriver test. You may experience where website is asking for permission to run and use cookies when you first time visit the site. Cookies is piece of experience which helps user to retain login, search history and other details for better user experience. Some time you need to clear all cookies in selenium test and you need selenium webdriver command to delete all cookies in the browser. Selenium Webdriver have built in deleteAllCookies() to Delete all cookies from current running browser window in test.
Let's see how to use deleteAllCookies() to Delete all cookies in selenium.
Deleting all cookies in selenium using deleteAllCookies() in Selenium WebDriver
package TestPack;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class deleteAllCookies {
public static void main(String[] args) {
String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
//Add cookie with name, value, domain, path, expiry, isSecure and isHTTPOnly.
driver.manage().addCookie(new Cookie("Cookiename1", "cookieval1", ".only-testing-blog.blogspot.com", "/", new Date((2035-1900),05,07), false, true));
driver.manage().addCookie(new Cookie("Cookiename2", "cookieval2", ".only-testing-blog.blogspot.com", "/", new Date((2035-1900),02,06), false, true));
driver.manage().addCookie(new Cookie("Cookiename3", "cookieval3", ".only-testing-blog.blogspot.com", "/", new Date((2035-1900),03,06), false, true));
//Get all cookie details from browser using getCookies().
Set<Cookie> cookies = driver.manage().getCookies();
//Iterate through cookies to isolate each cookie.
Iterator value = cookies.iterator();
while (value.hasNext()) {
System.out.println(value.next());
}
//Delete all cookies in selenium using deleteAllCookies.
driver.manage().deleteAllCookies();
System.out.println("\n");
System.out.println("********** Check if any cookie present after deleting all cookies **************");
System.out.println("\n");
//Get all cookie details from browser.
Set<Cookie> cookies1 = driver.manage().getCookies();
//Check if set has any value.
if(!cookies1.isEmpty()) {
//Iterate through cookies to isolate each cookie.
Iterator value1 = cookies1.iterator();
while (value1.hasNext()) {
System.out.println(value1.next());
}
}
else
{
System.out.println("Set is empty. No cookie found.");
}
}
}
Output :
Cookiename1=cookieval1; expires=Thu, 07 Jun 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;
Cookiename2=cookieval2; expires=Tue, 06 Mar 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;
Cookiename3=cookieval3; expires=Fri, 06 Apr 2035 12:00:00 IST; path=/; domain=.only-testing-blog.blogspot.com;secure;
********** Check if any cookie present after deleting all cookies **************
Set is empty. No cookie found.
In above example you can see that deleteAllCookies() has cleared all cookies in selenium we set earlier using addCookie() for current browsing context.
No comments:
Post a Comment