EnergyVenom's picture
Astra: PO/GL actions, usage coverage, assisted sign-in, self-healing, recorder, new top navigation and theme (172a4e22a2bf)
fb62a97 verified
Raw History Blame Contribute Delete
3.91 kB
// One sign-in for the whole run (playwright.fusion.config.ts globalSetup / globalTeardown): the test user signs in once,
// and every test starts from that session instead of signing in again (20-60 s each). The session file holds Fusion's
// session cookies (never the password), sits in the run's own folder and is deleted when the run ends. When the
// sign-in here fails, an empty session is written and each test signs in by itself, as before.
//
// Assisted sign-in (FPT_SIGN_IN_MODE=assisted: the pod signs in through the company's identity provider, with MFA): a
// person signed in on Astra's run page, and FPT_FUSION_STORAGE_STATE points to that session (written by Astra or the
// runner for this pass only). It is checked once here; when the pod no longer accepts it, every test of the pass is
// skipped as "sign-in expired" (fixtures.ts) and Astra asks for a new sign-in instead of failing them all.
import * as fs from 'fs';
import * as path from 'path';
import { chromium } from '@playwright/test';
import { assistedSignIn, clearSignInExpired, FusionUi, markSignInExpired } from './ui';
const EMPTY = { cookies: [], origins: [] };
export function sessionFile(): string {
const f = process.env.FPT_FUSION_SESSION_FILE;
if (!f) throw new Error('FPT_FUSION_SESSION_FILE is not set (playwright.fusion.config.ts sets it)');
return f;
}
/** Assisted sign-in: the session a person signed in becomes this run's session, if the pod still accepts it. */
async function provided(file: string): Promise<void> {
const given = process.env.FPT_FUSION_STORAGE_STATE;
if (!given || !fs.existsSync(given)) {
markSignInExpired('Sign-in expired: no signed-in session was given for this pod (assisted sign-in); Astra asks a person to sign in.');
return;
}
fs.writeFileSync(file, fs.readFileSync(given), { mode: 0o600 });
if (!process.env.FUSION_BASE_URL) return;
const browser = await chromium.launch({ headless: true });
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, storageState: file });
const page = await context.newPage();
page.setDefaultNavigationTimeout(120_000);
if (await new FusionUi(page).isSignedIn()) console.log('[session] the session a person signed in is accepted; every test reuses it');
else markSignInExpired('Sign-in expired: the pod no longer accepts the session a person signed in (assisted sign-in); Astra asks for a new sign-in.');
} catch (e) {
console.log(`[session] the signed-in session could not be checked, the tests try it: ${String((e as Error).message || e).split('\n')[0]}`);
} finally {
await browser.close().catch(() => {});
}
}
export async function setup(): Promise<void> {
const file = sessionFile();
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(EMPTY), { mode: 0o600 });
clearSignInExpired();
if (assistedSignIn()) return provided(file);
if (!process.env.FUSION_BASE_URL || !process.env.FUSION_USERNAME || !process.env.FUSION_PASSWORD) return;
const browser = await chromium.launch({ headless: true });
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
const page = await context.newPage();
page.setDefaultTimeout(60_000);
page.setDefaultNavigationTimeout(120_000);
await new FusionUi(page).signIn();
await context.storageState({ path: file });
fs.chmodSync(file, 0o600);
console.log('[session] signed in once for this run; every test reuses the session');
} catch (e) {
console.log(`[session] one sign-in for the run failed, so each test signs in by itself: ${String((e as Error).message || e).split('\n')[0]}`);
} finally {
await browser.close().catch(() => {});
}
}
export async function teardown(): Promise<void> {
try { fs.unlinkSync(sessionFile()); } catch { /* already gone */ }
clearSignInExpired();
}