Handle Cookies in Selenium WebDriver - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday 6 February 2018

Handle Cookies in Selenium WebDriver

To Get Cookie Value

package mypackage;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverprog{


public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.quikr.com/");
Cookie cookiename=driver.manage().getCookieNamed("abRand");
System.out.println(cookiename);
}
}


Output:
abRand=17; path=/; domain=.quikr.com

To delete a cookie value

package mypackage;

import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverprog{
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.quikr.com/");
driver.manage().deleteCookieNamed("abRand");// delete cookie value
Set<Cookie> cookies=driver.manage().getCookies();// to print all cookies
for (Cookie cookie:cookies){
System.out.println(cookie);
}
driver.quit();
}
}


To Create a new Cookie

Cookie newcookie=new Cookie("cookiename","1");

Add a cookie
Cookie newcookie=new Cookie("Neeraj", "1");
driver.manage().addCookie(newcookie);

No comments:

Post a Comment