This post helps you,How to extract Cookies in a page.We can Add/Delete Cookies by using few methods.
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Cookies {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(“http://www.xyz.com”);
}
@Test
public void extrctCookie(){
//Get and extract all cookies of google domain and print cookie parameters.
Set totalCookies = driver.manage().getCookies();
System.out.println(“Total Number Of cookies : ” +totalCookies.size());
for (Cookie currentCookie : totalCookies) {
System.out.println(String.format(“%s -> %s -> %s -> %s”, “Domain Name : “+currentCookie.getDomain(), “Cookie Name : “+currentCookie.getName(), “Cookie Value : “+currentCookie.getValue(), “Cookie Expiry : “+currentCookie.getExpiry()));
}
}
}
Please change “Set totalCookies = driver.manage().getCookies();” to “Set totalCookies = driver.manage().getCookies();” since getCookies() method will return a set of cookies… Thanks
LikeLike
sorry to this… Set” totalCookies = driver.manage().getCookies();
LikeLike
sorry i am not able to comment with in tags cookie
LikeLike
Set cookies = driver.manage().getCookies();
Iterator itr = cookies.iterator();
while (itr.hasNext()){
Cookie c = itr.next();
System.out.println(“—–Cookies Detail—–“);
System.out.println(“Cookie Name: ” + c.getName() + “\n\tCookie Domain: ” + c.getDomain() + “\n\tCookie Value: ” + c.getValue() + “\n\tPath: ” + c.getPath()+ “\n\tExpiry Date: ” + c.getExpiry()+ “\n\tSecure: ” + c.isSecure());
}
Update the Code with above code snippet.
LikeLike
Thanks..! Anyway this is quite old.
You can find latest code here with all generics
https://seleniumbycharan.wordpress.com/2015/09/27/most-commonly-used-methods-in-your-selenium-framework-project-independent/
LikeLike