Pickle is very useful process of saving objects states as byte representation. You can find more here:
- Python pickle example cookies
- Test with geb firefox 52 and selenium geckodriver 18
- ubuntu 16.04 install headless chrome
In this psot we will demonstrate Java alternative for pickle by: org.apache.commons.lang.SerializationUtils;
The example shows how to save and load cookies for your selenium session with java 8 code:
Java method pickling cookies:
This method saves the cookie session in a file on your file system:
public static void saveCookie(String filename, WebDriver driver) {
Set<org.openqa.selenium.Cookie> myCookies = driver.manage().getCookies();
for (org.openqa.selenium.Cookie getcookies : myCookies) {
System.out.println(getcookies);
driver.manage().addCookie(getcookies);
}
String path = filename;
File CookieFile = new File(path);
byte[] repr = SerializationUtils.serialize((Serializable) myCookies);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(CookieFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
stream.write(repr);
} catch (IOException e) {
e.printStackTrace();
}
driver.close();
}
Java method unpickling cookies:
This method will load previous session of your last browsing.
public static void loadCookie(String filename, WebDriver driver){
Path path = Paths.get(filename);
byte[] text = new byte[0];
try {
text = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
Set<org.openqa.selenium.Cookie> myCookies = (Set<org.openqa.selenium.Cookie>) SerializationUtils.deserialize(text);
for (org.openqa.selenium.Cookie getcookies : myCookies) {
System.out.println(getcookies);
driver.manage().addCookie(getcookies);
}
driver.navigate().refresh();
}
Dependencies and imports
Required libraries and dependencies
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.apache.commons.lang.SerializationUtils;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
pom.xml file
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-support -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Java code example pickling and unpickling cookies
This is example of the whole code working. You need to add your login code if you want to test more advanced cookie example:
package other;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.apache.commons.lang.SerializationUtils;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CookiePickle {
public static final void main(String... aArgs) {
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese");
element.submit();
//login code to google
String file = "/home/test/cookie.file";
saveCookie(file, driver);
driver.quit();
WebDriver driver2 = new ChromeDriver();
driver2.get("http://www.google.com");
loadCookie(file, driver2);
driver2.navigate().refresh();
}
public static void loadCookie(String filename, WebDriver driver){
Path path = Paths.get(filename);
byte[] text = new byte[0];
try {
text = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
Set<org.openqa.selenium.Cookie> myCookies = (Set<org.openqa.selenium.Cookie>) SerializationUtils.deserialize(text);
for (org.openqa.selenium.Cookie getcookies : myCookies) {
System.out.println(getcookies);
driver.manage().addCookie(getcookies);
}
driver.navigate().refresh();
}
public static void saveCookie(String filename, WebDriver driver) {
Set<org.openqa.selenium.Cookie> myCookies = driver.manage().getCookies();
for (org.openqa.selenium.Cookie getcookies : myCookies) {
System.out.println(getcookies);
driver.manage().addCookie(getcookies);
}
String path = filename;
File CookieFile = new File(path);
byte[] repr = SerializationUtils.serialize((Serializable) myCookies);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(CookieFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
stream.write(repr);
} catch (IOException e) {
e.printStackTrace();
}
driver.close();
}
}