test(*): Add basic BDD tests

This commit is contained in:
Josh Creek
2025-04-24 08:15:24 +01:00
parent fa69411829
commit a7c1d60db8
17 changed files with 1722 additions and 41 deletions
+30
View File
@@ -0,0 +1,30 @@
import { After, Status } from '@cucumber/cucumber';
import fs from 'fs';
import path from 'path';
const dir = path.resolve('screenshots');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
After(async function (scenario) {
// 'this' is the World instance
// @ts-ignore
const worldPage = this.page || (global as any).page;
const scenarioName =
scenario.pickle?.name ||
scenario.pickle?.title ||
(scenario.pickle && scenario.pickle) ||
'unknown_scenario';
if (scenario.result?.status === Status.FAILED && worldPage) {
const fileName = scenarioName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
const screenshotPath = path.join(dir, `${fileName}.png`);
await worldPage.screenshot({ path: screenshotPath, fullPage: true });
// Optionally attach to report if supported:
// this.attach(fs.readFileSync(screenshotPath), 'image/png');
console.log(`Screenshot saved: ${screenshotPath}`);
} else if (scenario.result?.status === Status.FAILED) {
console.log(`[Screenshot skipped] No page found for scenario: ${scenarioName}`);
}
});
+51
View File
@@ -0,0 +1,51 @@
import { chromium } from 'playwright';
import type { Browser, Page } from 'playwright';
import { setDefaultTimeout, setWorldConstructor, World, Before, After, AfterAll } from '@cucumber/cucumber';
setDefaultTimeout(60000); // 60 seconds
let browser: Browser;
class CustomWorld extends World {
page!: Page;
async openPage() {
if (!browser) {
browser = await chromium.launch({ headless: true });
}
this.page = await browser.newPage();
}
async closePage() {
if (this.page) {
await this.page.close();
}
}
}
setWorldConstructor(CustomWorld);
Before(async function () {
await this.openPage();
});
// Always clear assessment history for destructive scenarios after page is available
Before({ tags: '@deleteAssessment or @clearAssessment' }, async function () {
if (!this.page) throw new Error('Playwright page not initialized!');
await this.page.goto('http://localhost:5173/results');
// Clear assessment history to guarantee a clean state
await this.page.evaluate(() => localStorage.removeItem('assessmentHistory'));
});
// Always clear assessment history after every scenario for isolation
After(async function () {
if (this.page) {
await this.page.evaluate(() => localStorage.removeItem('assessmentHistory'));
}
});
AfterAll(async function () {
if (browser) {
await browser.close();
}
});