Skip to content

Commit

Permalink
Adding backend verification to smoke test.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsommardahl committed Mar 7, 2024
1 parent 10ffdbf commit 02d9508
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
35 changes: 27 additions & 8 deletions frontend/e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"puppeteer": "^18.0.3"
},
"devDependencies": {
"@types/node": "^20.11.25",
"ts-node": "^10.9.1"
}
}
61 changes: 44 additions & 17 deletions frontend/e2e/smoke.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
import puppeteer from "puppeteer";

async function main() {
async function getElement(page: puppeteer.Page, selector: string){
await page.waitForSelector(selector);
const element = await page.$(selector);
if (!element) throw new Error(`Element "${selector}" not found on page.`);
return element;
}

async function getElementValue(page: puppeteer.Page, selector: string){
const element = await getElement(page, selector);
const value = await element.evaluate((el: any) => el.textContent);
if (!value) throw new Error(`Element "${selector}" had no value.`);
return value;
}

async function verifyElementContainsText(page: puppeteer.Page, selector: string, expectedText: string){
const value = await getElementValue(page, selector);
console.log(`Found: ${value}`);
const found = value.toLowerCase().includes(expectedText.toLowerCase());
if (!found) {
throw new Error(`Smoke test failed. "${expectedText}" was not found in element "${selector}".`);
}
}

async function enterText(page: puppeteer.Page, selector: string, text: string){
const element = await getElement(page, selector);
await element.type(text);
}

async function clickElement(page: puppeteer.Page, selector: string){
const element = await getElement(page, selector);
await element.click();
}

async function main(url: string) {
const browser = await puppeteer.launch();
try {
const url = process.env.TEST_URL;
if (!url) throw Error("No TEST_URL defined.");
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const selector = "#page-title";
await page.waitForSelector(selector);
let element = await page.$(selector);
if (!element) throw new Error("Element not found on page.");
let value = await element.evaluate((el: any) => el.textContent);
if (!value) throw new Error("Element had no value.");
await browser.close();
const found = value.toLowerCase().indexOf("anyhasher") > -1;
if (!found) {
console.log(value);
throw new Error("Smoke test failed.");
}
await verifyElementContainsText(page, "#page-title", "anyhasher");
await enterText(page, "input[type=text]", "This had BETTER work!");
await clickElement(page, "button");
await verifyElementContainsText(page, "p", "d6115deb306b9655598232c871ef6a04");
console.log("Smoke test passed.");
(<any>process).exitCode = 0;
} catch (err) {
(<any>process).exitCode = 1;
throw err;
} finally {
await browser.close();
}
}

main();
const url = <any>process.env.TEST_URL || "http://localhost:3000/";
if (!url) throw Error("No TEST_URL defined.");
main(url);
3 changes: 1 addition & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02d9508

Please sign in to comment.