bas-remote-dev/main.ts
Jānis Jansons 93613c9753
Some checks failed
Playwright Tests / test (push) Failing after 51s
Initial commit
2024-12-10 21:34:36 +02:00

138 lines
3.8 KiB
TypeScript

import playwright from "npm:playwright";
const devSpace = Deno.env.get("BAS_DEVSPACE")!;
const headless = Deno.env.get("DENO_HEADLESS")! === "true" ? true : false;
if (import.meta.main) {
while (true) {
console.log(`Starting new loop`);
await main(devSpace);
await new Promise((resolve) => setTimeout(resolve, 3 * 1000));
}
}
async function main(devSpacename: string) {
const browser = await playwright.chromium.launch({
headless: headless,
});
const context = await browser.newContext();
const page = await context.newPage();
console.log(`Logging in to BAS`);
await page.goto(Deno.env.get("BAS_URL")!);
await page.locator(`text=${Deno.env.get("BAS_PROFILE")!}`).click();
await page
.getByLabel("Email or User Name")
.fill(Deno.env.get("BAS_USERNAME")!);
await page.getByLabel("Password").fill(Deno.env.get("BAS_PASSWORD")!);
await page.waitForTimeout(500);
await page.getByText("Keep me signed in").check();
await page.waitForTimeout(500);
await page.getByText("Continue").click();
await page.waitForLoadState();
await page.waitForURL("**/index.html");
// Request manager page directly (without iframe)
console.log(`Loading management page`);
await page.goto(Deno.env.get("BAS_URL")! + "/workspace-manager-ui/");
await page.waitForTimeout(1000);
console.log(`Loaded management page`);
// const pageSource = await page.content();
// console.log(JSON.stringify(pageSource));
const devSpaceEl = await findCorrectDevspace(page, devSpacename);
if (!devSpaceEl) {
throw Error(`Could not find defined devspace: ${devSpacename}`);
}
await startDevSpace(devSpaceEl, page);
await loadVSCode(devSpaceEl, devSpacename);
console.log(`Waiting for 5 minutes and then starting again...`);
await page.waitForTimeout(5 * 60 * 1000);
// Close on done
await new Promise((resolve) => {
page.on("close", resolve); // <-- add this
});
await context.close();
await browser.close();
}
async function findCorrectDevspace(page: playwright.Page, devSpacename: string) {
const rows = await page
.locator('div.dev-spaces-row:has-text("' + devSpacename + '")')
.all();
let findFullStack = false;
if (rows.length > 1) {
console.log(`Found multiple "${devSpacename}" dev spaces`);
findFullStack = true;
}
for (const el of rows) {
console.log(`Checking dev space row`);
const fullstack = await el
.locator(
'div.left-row:has-text("Full-Stack Application Using Productivity Tools")'
)
.isVisible();
if (fullstack) {
console.log(`Found the fullstack dev space row`);
}
// Skip non-fullstack lines if they exist for that name
if (findFullStack && !fullstack) {
continue;
}
return el;
}
}
async function startDevSpace(
devSpaceEl: playwright.Locator,
page: playwright.Page
) {
const running = await devSpaceEl
.locator('div.text-center a:has-text("RUNNING")')
.isVisible();
if (running) {
console.log(`Dev Space running`);
return true;
}
const stopped = await devSpaceEl
.locator('div.text-center a:has-text("STOPPED")')
.isVisible();
if (stopped) {
await devSpaceEl.locator('button[id^="startButton"]').click();
await page.waitForTimeout(5 * 1000);
}
const stopping = await devSpaceEl
.locator('div.text-center a:has-text("STOPPING")')
.isVisible();
const starting = await devSpaceEl
.locator('div.text-center a:has-text("STARTING")')
.isVisible();
if (stopping || starting) {
await page.waitForTimeout(2 * 1000);
console.log(`Waiting for dev space to ${stopping ? "stop" : "start"}...`);
await startDevSpace(devSpaceEl, page);
}
}
async function loadVSCode(devSpaceEl: playwright.Locator, devSpacename: string) {
console.log(`Opening VSCode dev space`);
await devSpaceEl.locator('a:has-text("' + devSpacename + '")').click();
}