solo-level-app-automation/src/main/java/com/ghost/temple/BrowserLauncher.java

114 lines
3.9 KiB
Java

package com.ghost.temple;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.File;
import java.util.Scanner;
public class BrowserLauncher {
private static WebDriver driver;
private static final String APP_URL = "http://192.168.31.73:3000/";
public static void main(String[] args) {
// IMPORTANT: Remove WebDriverManager setup here if you already manage drivers elsewhere
// WebDriverManager.firefoxdriver().setup(); // uncomment if you want dynamic driver management
String projectDir = System.getProperty("user.dir");
// Paths — customize if your automation project has different structure
String firefoxBinaryPath = projectDir + "/firefox/firefox";
String profilePath = projectDir + "/.mozilla/firefox/b4xl9fej.default-release";
File firefoxBinary = new File(firefoxBinaryPath);
if (!firefoxBinary.exists() || !firefoxBinary.canExecute()) {
System.err.println("❌ Firefox binary missing or not executable at: " + firefoxBinaryPath);
System.exit(1);
}
File profileDir = new File(profilePath);
if (!profileDir.exists() || !profileDir.isDirectory()) {
System.err.println("❌ Firefox profile directory missing or invalid at: " + profilePath);
System.exit(1);
}
FirefoxProfile profile = new FirefoxProfile(profileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(new FirefoxBinary(firefoxBinary));
options.setProfile(profile);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("\n⚠ JVM Shutdown detected. Saving data before exit...");
saveAll();
}));
try {
StorageManager.loadStorage(driver, APP_URL);
CookieManager.loadCookies(driver, APP_URL);
System.out.println("🔥 Browser launched with persisted cookies and storage!");
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equalsIgnoreCase("save")) {
speakPrompt("Please type save and press Enter to save your work before closing the browser.");
System.out.print("> ");
input = scanner.nextLine().trim();
if (!input.equalsIgnoreCase("save")) {
System.out.println("❌ You must save before quitting. Try again.");
}
}
Thread.sleep(500);
saveAll();
System.out.println("💾 Storage and cookies saved. Closing browser now...");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
driver = null;
}
}
}
private static void speakPrompt(String message) {
System.out.println(message);
try {
ProcessBuilder pb = new ProcessBuilder("./festival-tts", message);
pb.redirectErrorStream(true);
Process process = pb.start();
try (var reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))) {
while (reader.readLine() != null) {}
}
process.waitFor();
} catch (Exception e) {
System.err.println("❌ Failed to run festival-tts:");
e.printStackTrace();
}
}
private static void saveAll() {
if (driver != null) {
CookieManager.saveCookies(driver);
StorageManager.saveStorage(driver);
}
}
}