Skip to content

Instantly share code, notes, and snippets.

@elbeicktalat
Created August 23, 2023 16:33
Show Gist options
  • Save elbeicktalat/ce83431036adbf70bccb2bbc976b6f1b to your computer and use it in GitHub Desktop.
Save elbeicktalat/ce83431036adbf70bccb2bbc976b6f1b to your computer and use it in GitHub Desktop.
Selenium java cookies manager. Contains save delete and load methods.
package com.elbeicktalat;
import lombok.RequiredArgsConstructor;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.json.Json;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import static com.elbeicktalat.utils.Constants.COOKIES_PATH;
import static com.elbeicktalat.utils.ExceptionMessages.UNABLE_TO_SAVE_THE_COOKIES;
import static com.elbeicktalat.utils.Messages.FILE_ALREADY_DELETED;
import static com.elbeicktalat.utils.Messages.FILE_ALREADY_EXISTS;
import static com.elbeicktalat.utils.Messages.FILE_CREATED_SUCCESSFULLY;
import static com.elbeicktalat.utils.Messages.FILE_DELETED_SUCCESSFULLY;
import static com.elbeicktalat.utils.Messages.YOU_ARE_ALREADY_LOGGED_OUT;
@RequiredArgsConstructor
public class CookiesManager {
private final WebDriver driver;
private final Json json = new Json();
private void save() {
final File cookiesFile = new File(COOKIES_PATH);
try {
if (!cookiesFile.createNewFile()) {
System.out.printf(FILE_ALREADY_EXISTS, COOKIES_PATH);
return;
}
System.out.printf(FILE_CREATED_SUCCESSFULLY, COOKIES_PATH);
final FileWriter fileWriter = new FileWriter(COOKIES_PATH);
final Set<Cookie> cookies = driver.manage().getCookies();
fileWriter.write(json.toJson(cookies));
fileWriter.close();
} catch (IOException e) {
System.out.println(UNABLE_TO_SAVE_THE_COOKIES);
throw new RuntimeException(e);
}
}
public void load() {
final JSONArray jsonArray;
try {
final String contents = new String(Files.readAllBytes(Path.of(COOKIES_PATH)));
jsonArray = new JSONArray(contents);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (Object object : jsonArray) {
final JSONObject jsonObject = new JSONObject(json.toJson(object));
final Cookie cookie = new Cookie
.Builder(jsonObject.getString("name"), jsonObject.getString("value"))
.domain(jsonObject.getString("domain"))
.isHttpOnly(jsonObject.getBoolean("httpOnly"))
.isSecure(jsonObject.getBoolean("secure"))
.path(jsonObject.getString("path"))
.sameSite(jsonObject.getString("sameSite"))
.build();
driver.manage().addCookie(cookie);
}
driver.navigate().refresh();
}
public void delete() {
final File cookiesJsonFile = new File(COOKIES_PATH);
if (!cookiesJsonFile.delete()) {
System.out.println(YOU_ARE_ALREADY_LOGGED_OUT);
System.out.printf(FILE_ALREADY_DELETED, COOKIES_PATH);
return;
}
System.out.printf(FILE_DELETED_SUCCESSFULLY, COOKIES_PATH);
driver.manage().deleteAllCookies();
driver.navigate().refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment