11import { Given , When , Then } from "@cucumber/cucumber" ;
22import puppeteer from 'puppeteer' ;
3- import path from 'path' ;
43import * as chai from 'chai'
5- import * as fs from 'fs'
64const expect = chai . expect ;
75
8- import { fileURLToPath } from 'url' ;
9- import { dirname } from 'path' ;
10- const __filename = fileURLToPath ( import . meta. url ) ;
11- const __dirname = dirname ( __filename ) ;
12-
136function delay ( milliseconds ) {
147 return new Promise ( ( resolve ) => {
158 setTimeout ( resolve , milliseconds ) ;
@@ -36,23 +29,18 @@ Given("I set the browser to intercept outbound requests", async function () {
3629 } )
3730} ) ;
3831
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' ) ) ;
32+ When ( "I wait {int} seconds " , async function ( delaySeconds ) {
33+ await delay ( delaySeconds * 1000 ) ;
4134} ) ;
4235
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- } )
36+ Then ( "there is a GA4 request" , function ( ) {
37+ const ga4Request = this . requests . find ( request => request . url . includes ( "https://www.google-analytics.com/g/collect" ) ) ;
38+ expect ( ga4Request ) . to . exist ;
39+ } ) ;
4840
4941Then ( "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-
42+ const requestUrls = this . requests . map ( ( request ) => {
43+ return ( new URL ( request . url ) ) . host ;
44+ } ) ;
45+ expect ( [ "localhost:8080" , "www.googletagmanager.com" , "www.google-analytics.com" ] ) . to . include . members ( requestUrls ) ;
46+ } )
0 commit comments