Cookies issue fixed, Browser launches with Persistance
This commit is contained in:
parent
e4d11cb068
commit
9b0ba1743a
|
@ -11,10 +11,17 @@ public class CookieManager {
|
||||||
private static final String COOKIE_FILE = "cookies.data";
|
private static final String COOKIE_FILE = "cookies.data";
|
||||||
|
|
||||||
public static void saveCookies(WebDriver driver) {
|
public static void saveCookies(WebDriver driver) {
|
||||||
|
Set<Cookie> cookies = driver.manage().getCookies();
|
||||||
|
System.out.println("🍪 Saving cookies (" + cookies.size() + " total):");
|
||||||
|
for (Cookie c : cookies) {
|
||||||
|
System.out.println(" - " + c.getName() + " = " + c.getValue() + ", domain=" + c.getDomain());
|
||||||
|
}
|
||||||
|
|
||||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(COOKIE_FILE))) {
|
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(COOKIE_FILE))) {
|
||||||
oos.writeObject(driver.manage().getCookies());
|
oos.writeObject(cookies);
|
||||||
System.out.println("🍪 Cookies saved successfully!");
|
System.out.println("🍪 Cookies saved successfully!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
System.err.println("❌ Failed to save cookies:");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,13 +36,16 @@ public class CookieManager {
|
||||||
|
|
||||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
|
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
|
||||||
Set<Cookie> cookies = (Set<Cookie>) ois.readObject();
|
Set<Cookie> cookies = (Set<Cookie>) ois.readObject();
|
||||||
|
System.out.println("🍪 Loading cookies (" + cookies.size() + " total):");
|
||||||
driver.get(url); // must open the domain before adding cookies
|
driver.get(url); // must open the domain before adding cookies
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie : cookies) {
|
||||||
|
System.out.println(" - Adding cookie: " + cookie.getName() + " = " + cookie.getValue() + ", domain=" + cookie.getDomain());
|
||||||
driver.manage().addCookie(cookie);
|
driver.manage().addCookie(cookie);
|
||||||
}
|
}
|
||||||
driver.navigate().refresh();
|
driver.navigate().refresh();
|
||||||
System.out.println("✅ Cookies loaded successfully!");
|
System.out.println("✅ Cookies loaded successfully!");
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
System.err.println("❌ Failed to load cookies:");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,18 @@ public class StorageManager {
|
||||||
String localStorage = (String) js.executeScript("return JSON.stringify(window.localStorage);");
|
String localStorage = (String) js.executeScript("return JSON.stringify(window.localStorage);");
|
||||||
String sessionStorage = (String) js.executeScript("return JSON.stringify(window.sessionStorage);");
|
String sessionStorage = (String) js.executeScript("return JSON.stringify(window.sessionStorage);");
|
||||||
|
|
||||||
|
System.out.println("💾 Saving localStorage: " + localStorage);
|
||||||
|
System.out.println("💾 Saving sessionStorage: " + sessionStorage);
|
||||||
|
|
||||||
String json = "{\"localStorage\":" + localStorage + ",\"sessionStorage\":" + sessionStorage + "}";
|
String json = "{\"localStorage\":" + localStorage + ",\"sessionStorage\":" + sessionStorage + "}";
|
||||||
|
|
||||||
try (FileWriter writer = new FileWriter(STORAGE_FILE)) {
|
try (FileWriter writer = new FileWriter(STORAGE_FILE)) {
|
||||||
writer.write(json);
|
writer.write(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("✅ localStorage and sessionStorage saved");
|
System.out.println("✅ localStorage and sessionStorage saved successfully");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
System.err.println("❌ Error saving storage:");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +40,8 @@ public class StorageManager {
|
||||||
public static void loadStorage(WebDriver driver, String url) {
|
public static void loadStorage(WebDriver driver, String url) {
|
||||||
try {
|
try {
|
||||||
String json = new String(Files.readAllBytes(Paths.get(STORAGE_FILE)), StandardCharsets.UTF_8);
|
String json = new String(Files.readAllBytes(Paths.get(STORAGE_FILE)), StandardCharsets.UTF_8);
|
||||||
|
System.out.println("📂 Loaded storage JSON: " + json);
|
||||||
|
|
||||||
driver.get(url); // Open page first to set storage
|
driver.get(url); // Open page first to set storage
|
||||||
|
|
||||||
JavascriptExecutor js = (JavascriptExecutor) driver;
|
JavascriptExecutor js = (JavascriptExecutor) driver;
|
||||||
|
@ -47,7 +53,7 @@ public class StorageManager {
|
||||||
);
|
);
|
||||||
|
|
||||||
driver.navigate().refresh(); // Refresh so storage is applied
|
driver.navigate().refresh(); // Refresh so storage is applied
|
||||||
System.out.println("✅ localStorage and sessionStorage loaded");
|
System.out.println("✅ localStorage and sessionStorage loaded successfully");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("⚠️ Storage file not found, skipping load");
|
System.out.println("⚠️ Storage file not found, skipping load");
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class Hooks {
|
||||||
}
|
}
|
||||||
StorageManager.saveStorage(DriverManager.getDriver()); // Save local/session storage
|
StorageManager.saveStorage(DriverManager.getDriver()); // Save local/session storage
|
||||||
CookieManager.saveCookies(DriverManager.getDriver()); // Save cookies
|
CookieManager.saveCookies(DriverManager.getDriver()); // Save cookies
|
||||||
//DriverManager.quitDriver(); // Then quit driver
|
DriverManager.quitDriver(); // Then quit driver
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user