|
| 1 | +import { Given, When, Then } from "@cucumber/cucumber"; |
| 2 | +import puppeteer from 'puppeteer'; |
| 3 | +import path from 'path'; |
| 4 | +import * as chai from 'chai' |
| 5 | +import * as fs from 'fs' |
| 6 | +const expect = chai.expect; |
| 7 | + |
| 8 | +import { fileURLToPath } from 'url'; |
| 9 | +import { dirname } from 'path'; |
| 10 | +const __filename = fileURLToPath(import.meta.url); |
| 11 | +const __dirname = dirname(__filename); |
| 12 | + |
| 13 | +function delay(milliseconds) { |
| 14 | + return new Promise((resolve) => { |
| 15 | + setTimeout(resolve, milliseconds); |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +Given("I load an empty browser", async function () { |
| 20 | + this.browser = await puppeteer.launch(); |
| 21 | + this.page = await this.browser.newPage(); |
| 22 | +}); |
| 23 | + |
| 24 | +Given("I set the browser to intercept outbound requests", async function () { |
| 25 | + await this.page.setRequestInterception(true); |
| 26 | + this.requests = []; |
| 27 | + this.responses = []; |
| 28 | + |
| 29 | + this.page.on('request', (request) => { |
| 30 | + this.requests.push({ method: request.method(), headers: request.headers(), url: request.url() }); |
| 31 | + request.continue(); |
| 32 | + }); |
| 33 | + |
| 34 | + this.page.on('response', (response) => { |
| 35 | + this.responses.push(response); |
| 36 | + }) |
| 37 | +}); |
| 38 | + |
| 39 | +When("I set the page content with {string} HTML file", async function (fileName) { |
| 40 | + await this.page.setContent(fs.readFileSync(path.join(__dirname, `..`, fileName), 'utf8')); |
| 41 | +}); |
| 42 | + |
| 43 | +// This is still in progress. GA requests don't currently occur after clicking a button element. |
| 44 | +When("I click on element with selector {string}", async function (elementSelector) { |
| 45 | + await this.page.locator(elementSelector).click(); |
| 46 | + await delay(2000) |
| 47 | +}) |
| 48 | + |
| 49 | +Then("there are no unexpected requests", function () { |
| 50 | + this.requests.forEach((request) => { |
| 51 | + let isAllowed = false; |
| 52 | + if (request.url.includes('https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js') || request.url.includes("https://www.googletagmanager.com/gtag/js?id=G-CSLL4ZEK4L")) { |
| 53 | + isAllowed = true; |
| 54 | + } |
| 55 | + expect(isAllowed).to.equal(true) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
0 commit comments