Site icon

Add a Cookie using Selenium WebDriver


package TestCases;

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

public class MakeinJavaTest {
	public WebDriver driver;

	@Test
	public void VerifyAddedCookie() {

		System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
		driver = new ChromeDriver();

		driver.get("https://makeinjava.com");
		// We are passing parameters, name = CookieName and value=12345678
		Cookie cookie = new Cookie("CookieName", "12345678");
		driver.manage().addCookie(cookie);

		// Display all the cookies on Console
		Set cookieList = driver.manage().getCookies();
		for (Cookie newCookie : cookieList) {
			System.out.println(newCookie);
		}

	}

}

 

Exit mobile version