- May 20, 2024
- admin
- 0
- Home
- AddCookies
- 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.
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.
The commands below are used to get, add, and delete all cookies present in a browser:
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.
}
Handle multiple windows in selenium [Test] public void HandleMultipleWindows() { dr.Navigate().GoToUrl("https://demoqa.com/browser-windows"); string windowhandleParent = dr.CurrentWindowHandle;…
How to Handle Multiple Tabs in Selenium [Test] public void HandleMultipleTabs() { dr.Navigate().GoToUrl("https://demoqa.com/browser-windows"); string windowhandleParent…
What is Selenium? Selenium is a portable software testing framework for web applications. Selenium provides a playback…
Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!