26
Jun

add custom cookie with selenium webdriver

package SeleniumPractice;

import java.text.ParseException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class AddCookiesInSelenium {
WebDriver driver;

@Test
public void enterText() throws InterruptedException, ParseException {

// First step set the driver location
System.setProperty(“webdriver.chrome.driver”, “C:\\softwares\\Selenium jars\\chromedriver.exe”);

// create chrome object
driver = new ChromeDriver();
// navigate to site
driver.get(“https://www.google.com/”);
// delete all non-system cookies
driver.manage().deleteAllCookies();
// An implicit wait tells WebDriver to poll the DOM for a certain amount of time
// when trying to find an element if they are not immediately available.
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Cookie cookie = new Cookie(“customeCookie”, “12345”);

driver.manage().addCookie(cookie);

Set cookies = driver.manage().getCookies();
for (Cookie cook : cookies) {
System.out.println(cook);
}

driver.quit();
}
}