/**
* Browser Manager - Handles browser lifecycle for LocatorLabs MCP
*
* @author Naveen AutomationLabs
* @license MIT
* @date 2025
* @see https://github.com/naveenanimation20/locatorlabs-mcp
*/
import { chromium, Browser, Page, BrowserContext } from "playwright";
export class BrowserManager {
private browser: Browser | null = null;
private context: BrowserContext | null = null;
private page: Page | null = null;
async launch(): Promise<void> {
if (this.browser) return;
this.browser = await chromium.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
this.context = await this.browser.newContext({
viewport: { width: 1920, height: 1080 },
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
});
this.page = await this.context.newPage();
}
async getPage(): Promise<Page> {
if (!this.page) {
await this.launch();
}
return this.page!;
}
async navigateTo(url: string): Promise<Page> {
const page = await this.getPage();
await page.goto(url, { waitUntil: "networkidle", timeout: 30000 });
return page;
}
async close(): Promise<void> {
if (this.context) {
await this.context.close();
this.context = null;
}
if (this.browser) {
await this.browser.close();
this.browser = null;
}
this.page = null;
}
}