@@ -2809,6 +2809,115 @@ func TestPageOnResponse(t *testing.T) {
28092809 }
28102810}
28112811
2812+ // TestPageOnRequestFinished tests that the requestfinished event fires when requests complete successfully.
2813+ func TestPageOnRequestFinished (t * testing.T ) {
2814+ t .Parallel ()
2815+
2816+ tb := newTestBrowser (t , withHTTPServer ())
2817+ tb .withHandler ("/home" , func (w http.ResponseWriter , _ * http.Request ) {
2818+ _ , err := fmt .Fprintf (w , `<!DOCTYPE html>
2819+ <html>
2820+ <head>
2821+ <link rel="stylesheet" href="/style.css">
2822+ </head>
2823+ <body>
2824+ <script>fetch('/api', {
2825+ method: 'POST',
2826+ headers: {
2827+ 'Content-Type': 'application/json'
2828+ },
2829+ body: JSON.stringify({name: 'tester'})
2830+ })</script>
2831+ </body>
2832+ </html>` )
2833+ require .NoError (t , err )
2834+ })
2835+ tb .withHandler ("/api" , func (w http.ResponseWriter , r * http.Request ) {
2836+ body , err := io .ReadAll (r .Body )
2837+ require .NoError (t , err )
2838+ defer require .NoError (t , r .Body .Close ())
2839+
2840+ var data struct {
2841+ Name string `json:"name"`
2842+ }
2843+ err = json .Unmarshal (body , & data )
2844+ require .NoError (t , err )
2845+
2846+ w .Header ().Set ("Content-Type" , "application/json" )
2847+ _ , err = fmt .Fprintf (w , `{"message": "Hello %s!"}` , data .Name )
2848+ require .NoError (t , err )
2849+ })
2850+ tb .withHandler ("/style.css" , func (w http.ResponseWriter , _ * http.Request ) {
2851+ w .Header ().Set ("Content-Type" , "text/css" )
2852+ _ , err := fmt .Fprintf (w , `body { background-color: #f0f0f0; }` )
2853+ require .NoError (t , err )
2854+ })
2855+
2856+ tb .vu .ActivateVU ()
2857+ tb .vu .StartIteration (t )
2858+ defer tb .vu .EndIteration (t )
2859+
2860+ gv , err := tb .vu .RunAsync (t , `
2861+ const context = await browser.newContext();
2862+ const page = await context.newPage();
2863+
2864+ var finishedRequests = [];
2865+ page.on('requestfinished', (request) => {
2866+ finishedRequests.push({
2867+ url: request.url(),
2868+ method: request.method(),
2869+ resourceType: request.resourceType(),
2870+ isNavigationRequest: request.isNavigationRequest(),
2871+ });
2872+ });
2873+
2874+ await page.goto('%s', {waitUntil: 'networkidle'});
2875+ await page.close();
2876+ return JSON.stringify(finishedRequests, null, 2);
2877+ ` , tb .url ("/home" ))
2878+ require .NoError (t , err )
2879+
2880+ got := k6test .ToPromise (t , gv )
2881+ require .Equal (t , sobek .PromiseStateFulfilled , got .State ())
2882+
2883+ var finishedRequests []struct {
2884+ URL string `json:"url"`
2885+ Method string `json:"method"`
2886+ ResourceType string `json:"resourceType"`
2887+ IsNavigationRequest bool `json:"isNavigationRequest"`
2888+ }
2889+ err = json .Unmarshal ([]byte (got .Result ().String ()), & finishedRequests )
2890+ require .NoError (t , err )
2891+
2892+ // Verify we captured some finished requests
2893+ require .NotEmpty (t , finishedRequests , "expected to capture at least one finished request" )
2894+
2895+ var foundHome , foundAPI , foundCSS bool
2896+ for _ , req := range finishedRequests {
2897+ switch {
2898+ case strings .HasSuffix (req .URL , "/home" ):
2899+ foundHome = true
2900+ assert .Equal (t , "GET" , req .Method )
2901+ assert .Equal (t , "Document" , req .ResourceType )
2902+ assert .True (t , req .IsNavigationRequest )
2903+ case strings .HasSuffix (req .URL , "/api" ):
2904+ foundAPI = true
2905+ assert .Equal (t , "POST" , req .Method )
2906+ assert .Equal (t , "Fetch" , req .ResourceType )
2907+ assert .False (t , req .IsNavigationRequest )
2908+ case strings .HasSuffix (req .URL , "/style.css" ):
2909+ foundCSS = true
2910+ assert .Equal (t , "GET" , req .Method )
2911+ assert .Equal (t , "Stylesheet" , req .ResourceType )
2912+ assert .False (t , req .IsNavigationRequest )
2913+ }
2914+ }
2915+
2916+ assert .True (t , foundHome , "expected to find /home request in finished requests" )
2917+ assert .True (t , foundAPI , "expected to find /api request in finished requests" )
2918+ assert .True (t , foundCSS , "expected to find /style.css request in finished requests" )
2919+ }
2920+
28122921func TestPageMustUseNativeJavaScriptObjects (t * testing.T ) {
28132922 t .Parallel ()
28142923
0 commit comments