How to handle Cookies in Selenium WebDriver



Cookies in Selenium

When a website loads, it is common to see a pop-up asking the users to provide permission to the site to run cookies. A cookie is a piece of information that consists of a name, value, expiry, path, etc. It helps users retain the search history, login, and other relevant details.

Introduction to Selenium WebDriver Cookies

A cookie is a small piece of data sent from a website and stored on the user’s computer. Cookies also recognize users returning to a website and loading the previously stored information. Mainly, cookies store the user’s identity and track the user’s journey through the website’s pages. WebDriver API provides a way to interact with cookies with built-in methods.

Selenium Commands for Cookies

The commands below are used to get, add, and delete all cookies present in a browser:

    • Get Cookie: Gets the cookies for the current domain.

      driver.manage().getCookies(); // Returns the List of all Cookies

    • Add Cookie: Adds a specific cookie into cookies. If the cookie’s domain name is blank, it is assumed that the cookie is meant for the domain of the current document.driver.manage().getCookieNamed(arg0); //Returns the specific cookie according to name

      driver.manage().addCookie(arg0); //Creates and adds the cookie

  •  

    • Delete Cookie: Deletes the cookies for the current domain.
    • driver.manage().deleteCookie(arg0); // Deletes the specific cookie
driver.manage().deleteCookieNamed(arg0); // Deletes the specific cookie according to the Name
driver.manage().deleteAllCookies(); // Deletes all the cookies
Handling Cookies in Selenium WebDriver (Example)
The following code snippet demonstrates how to store cookies in a file system and retrieve the necessary information with the help of Selenium WebDriver.
import java.io.BufferedWriter; 
import java.io.File;
import java.io.FileWriter;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Cookie;

public class cookieRead{

public static void main(String[] args){
WebDriver driver;
System.setProperty("webdriver.chrome.driver","Chrome_driver_path");
driver=new ChromeDriver();
driver.get("https://www.facebook.com");
//Enter Email id and Password if you are already Registered user
driver.findElement(By.name("username")).sendKeys("your_username");
driver.findElement(By.name("password")).sendKeys("your_password");
driver.findElement(By.name("submit")).click();
// Create a file to store Login Information
File file = new File("Cookiefile.data");
try{
// Delete old file if already exists
file.delete();
file.createNewFile();
FileWriter file = new FileWriter(file);
BufferedWriter Bwritecookie = new BufferedWriter(file); //Getting the cookie information
for(Cookie ck : driver.manage().getCookies()) {
Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));
Bwritecookie.newLine();
}
Bwritecookie.close();
file.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

When the code is executed, WebDriver will store the cookie information using FileWriter Class to write streams of characters and BufferedWriter to write the text into a file named “Cookiefile.data“.

The file stores cookie information – “Name, Value, Domain, Path”.

The tester can retrieve this information and log in without entering login credentials.


How to clear the Browser Cache using Selenium WebDriver?

Method 1

1. Clearing browser cookies before starting your test are essential.

2. If the tester uses Selenium WebDriver for test automation, they can use the method below to clear all cookies.

3. Create a void method below and then call the method before navigating to the application URL.

public void ClearBrowserCache()
{
webDriver.Manage().Cookies.DeleteAllCookies(); //delete all cookies
Thread.Sleep(7000); //wait 7 seconds to clear cookies.
}
Tags: , , , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18