selenium tutorial testing tools

How to addcookie with Selenium Webdriver

Using webdriver we can easily pass the cookie to the domain. In order to pass cookie, we should use a method named "addCookie(cookie)"

Method Name: addCookie(Cookie cookie)
Syntax:driver.manage().addCookie(arg0);
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name and value of the cookie to be add.

Example:

@Test
	public void addCookie()
	{
		driver= new FirefoxDriver();
		String URL="http://flipkart.com/";
		driver.navigate().to(URL);
                //we should pass name and value for cookie as parameters
                // In this example we are passing, name=mycookie and value=123456789123
		Cookie name = new Cookie("mycookie", "123456789123");
		driver.manage().addCookie(name);
		
                // After adding the cookie we will check that by displaying all the cookies.
		Set<Cookie> cookiesList =  driver.manage().getCookies();
		for(Cookie getcookies :cookiesList) {
		    System.out.println(getcookies );
		}
	}

The output looks like below. We can see the added cookie along with the other cookies of the domain.

addcookiesexample

Selenium Tutorials: 

Comments

Its really a nice article....and hope few more complementary articles you may read on

Thank you !!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Wanted Contributers

Hello Viewer! We're a relatively a small group of writers who have combined efforts to write the articles. Well, we need more help from talented people to contribute and grow the size of articles. It's great work if you're looking for a little money on the side – make this as your Part time job.

We are mainly looking into Protractor, Cucumber, JBehave, RSpec, Behat Mink, Selenium Python or any other tool which uses selenium.

If you can contribute on any the above, Please contact us.