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

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group