-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding backend verification to smoke test.
- Loading branch information
1 parent
10ffdbf
commit 02d9508
Showing
4 changed files
with
73 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"puppeteer": "^18.0.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.11.25", | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.