Cookies

  • Cookie is a piece of information from website and saved by your web browser
  • Cookies are a way of remembering users and their interaction with the site by storing information in the cookie file as key value pairs.
  • It stores the login information like user name / email and password
  • HTTP cookie is also known as a web cookie, a browser cookie or Internet cookie.
  • One website cannot access the cookies set by other websites

Handling Cookie in kotlin Selenium :

  • Each cookie is associated with a name, value, domain, path, termination, and status of whether it is safe or not. We validate the cookie using selenium kotlin, selenium can parse all the cookies
  • When testing a web application using the kotlin Selenium, you may need to create, update or delete a cookie to check the behavior of the website with and without cookies.
  • For example, when you automate e-commerce sites, you will set cookies for logins, products added to carts using cookies and the product you visited in or searched in past days
Get Cookies: getCookies() used to return the list of all cookies stored in the web browser.

import org.openqa.selenium.Cookie
import java.util.concurrent.TimeUnit
import org.openqa.selenium.chrome.ChromeDriver
fun main(args: Array) {
    System.setProperty("webdriver.chrome.driver", "C:/Users/user/Pictures/chromedriver.exe")
    val driver = ChromeDriver()
    driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS)
    // Open the web page
    driver.get("https://google.com")
// set the name and value for the cookie
    val coo = Cookie("karthiQ", "chercher.tech")
// set the name and value for the cookie
    val ren = Cookie("selenium-webdriver.com", "Renamed to chercher.tech")
// add the cookies
    driver.manage().addCookie(coo)
    driver.manage().addCookie(ren)
// get the cookies from the browser,
// not only cookies we set but also the cookies set by the website
    val abc = driver.manage().cookies
    println("Number of Cookies present : " + abc.size)
    for (cookie in abc) {
        println("---------------")
        System.out.println("Cookie Name : " + cookie.name)
        System.out.println("Cookie Value : " + cookie.value)
    }
}

Get Cookie by name:

getCookieNamed() used to return the specific cookie according to its name.


 // Open the web page
    driver.get("https://google.com")
// set the name and value for the cookie
    val coo = Cookie("karthiQ", "chercher.tech")
// set the name and value for the cookie
    val ren = Cookie("selenium-webdriver.com", "Renamed to chercher.tech")
// add the cookies
    driver.manage().addCookie(coo)
    driver.manage().addCookie(ren)
    val cookie = driver.manage().getCookieNamed("karthiQ")
    println("---------------")
    println("Cookie Name : " + cookie.name)
    println("Cookie Value : " + cookie.value)

Add Cookie::

addCookie() method is used to create and add the cookie.


driver.get("https://google.com");
		// set the name and value for the cookie
		Cookie coo = new Cookie("karthiQ", "chercher.tech");
		// add the cookie
		driver.manage().addCookie(coo);

Delete Cookie:

Using deleteCookie() we can delete a specific cookie, with Cookie object


// Open the web page
    driver.get("https://google.com")
    // set the name and value for the cookie
    val coo = Cookie("karthiQ", "chercher.tech")
    // set the name and value for the cookie
    val ren = Cookie("selenium-webdriver.com", "Renamed to chercher.tech")
    // add the cookies
    driver.manage().addCookie(coo)
    driver.manage().addCookie(ren)
    // delete the 'ren' cookie
    driver.manage().deleteCookie(ren)

Delete Cookie by name:

Using deleteCookieNamed() to delete a cookie according to its name.


// Open the web page
    driver.get("https://google.com")
    // set the name and value for the cookie
    val coo = Cookie("karthiQ", "chercher.tech")
    // set the name and value for the cookie
    val ren = Cookie("selenium-webdriver.com", "Renamed to chercher.tech")
    // add the cookies
    driver.manage().addCookie(coo)
    driver.manage().addCookie(ren)
    // delete the 'karthiQ' cookie
    driver.manage().deleteCookieNamed("karthiQ")

Delete All Cookie

deleteAllCookies() is used to delete all cookies.


    // set the name and value for the cookie
    val coo = Cookie("karthiQ", "chercher.tech")
// set the name and value for the cookie
    val ren = Cookie("selenium-webdriver.com", "Renamed to chercher.tech")
// add the cookies
    driver.manage().addCookie(coo)
    driver.manage().addCookie(ren)
// added the sleep to allow the domain to set all the cookies
    Thread.sleep(10000)
// delete all the cookies
    driver.manage().deleteAllCookies()
}

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions