@@ -2,6 +2,7 @@ package acceptance
22
33import (
44 "fmt"
5+ "net/url"
56 "testing"
67 "time"
78
@@ -332,26 +333,22 @@ func (dp *DashboardPage) WaitForEventDetails(timeout float64) {
332333 require .NoError (dp .t , err , "failed to wait for event details" )
333334}
334335
335- // FetchAPI executes a fetch request from the browser context and waits for the response .
336+ // FetchAPI executes a GET request using Playwright's API request context (shares browser cookies) .
336337func (dp * DashboardPage ) FetchAPI (path string ) {
337338 dp .t .Helper ()
338339
339- // Wait for the fetch to complete fully (read the response body)
340- _ , err := dp .Page .Evaluate (fmt .Sprintf (`fetch('%s').then(r => r.text())` , path ))
340+ _ , err := dp .Page .Request ().Get (dp .BaseURL () + path )
341341 require .NoError (dp .t , err )
342342}
343343
344- // FetchAPIWithBody executes a POST fetch request with JSON body from the browser context.
344+ // FetchAPIWithBody executes a POST request with JSON body using Playwright's API request context.
345345func (dp * DashboardPage ) FetchAPIWithBody (path string , body string ) {
346346 dp .t .Helper ()
347347
348- js := fmt .Sprintf (`fetch('%s', {
349- method: 'POST',
350- headers: {'Content-Type': 'application/json'},
351- body: JSON.stringify(%s)
352- })` , path , body )
353-
354- _ , err := dp .Page .Evaluate (js )
348+ _ , err := dp .Page .Request ().Post (dp .BaseURL ()+ path , playwright.APIRequestContextPostOptions {
349+ Headers : map [string ]string {"Content-Type" : "application/json" },
350+ Data : body ,
351+ })
355352 require .NoError (dp .t , err )
356353}
357354
@@ -379,3 +376,68 @@ func (dp *DashboardPage) WaitForUsagePanel(timeout float64) {
379376 })
380377 require .NoError (dp .t , err , "usage panel did not load content" )
381378}
379+
380+ // BaseURL returns the origin (scheme://host) from the session URL.
381+ func (dp * DashboardPage ) BaseURL () string {
382+ dp .t .Helper ()
383+
384+ parsed , err := url .Parse (dp .SessionURL )
385+ require .NoError (dp .t , err )
386+ return fmt .Sprintf ("%s://%s" , parsed .Scheme , parsed .Host )
387+ }
388+
389+ // DownloadRequestBody finds the "Download" link in the Request Body section and fetches the content.
390+ // Returns the download URL path (for verification) and the response body content.
391+ func (dp * DashboardPage ) DownloadRequestBody () (path string , body []byte , contentType string ) {
392+ dp .t .Helper ()
393+
394+ // Find the Request section's Body download link
395+ // Structure: div with h3 "Request" > div with h4 "Body" > a[download] "Download"
396+ link := dp .Page .Locator ("#event-details" ).Locator ("h3:has-text('Request')" ).Locator (".." ).Locator ("h4:has-text('Body')" ).Locator (".." ).Locator ("a[download]:has-text('Download')" )
397+
398+ href , err := link .GetAttribute ("href" )
399+ require .NoError (dp .t , err , "failed to get request body download link href" )
400+ require .NotEmpty (dp .t , href , "request body download link should have href" )
401+
402+ // Use Playwright's native API request context (shares browser cookies/context)
403+ fullURL := dp .BaseURL () + href
404+ response , err := dp .Page .Request ().Get (fullURL )
405+ require .NoError (dp .t , err , "failed to fetch request body download" )
406+ require .Equal (dp .t , 200 , response .Status (), "request body download should return 200" )
407+
408+ bodyBytes , err := response .Body ()
409+ require .NoError (dp .t , err , "failed to read request body" )
410+
411+ headers := response .Headers ()
412+ ct := headers ["content-type" ]
413+
414+ return href , bodyBytes , ct
415+ }
416+
417+ // DownloadResponseBody finds the "Download" link in the Response Body section and fetches the content.
418+ // Returns the download URL path (for verification) and the response body content.
419+ func (dp * DashboardPage ) DownloadResponseBody () (path string , body []byte , contentType string ) {
420+ dp .t .Helper ()
421+
422+ // Find the Response section's Body download link
423+ // Structure: div with h3 "Response" > div with h4 "Body" > a[download] "Download"
424+ link := dp .Page .Locator ("#event-details" ).Locator ("h3:has-text('Response')" ).Locator (".." ).Locator ("h4:has-text('Body')" ).Locator (".." ).Locator ("a[download]:has-text('Download')" )
425+
426+ href , err := link .GetAttribute ("href" )
427+ require .NoError (dp .t , err , "failed to get response body download link href" )
428+ require .NotEmpty (dp .t , href , "response body download link should have href" )
429+
430+ // Use Playwright's native API request context (shares browser cookies/context)
431+ fullURL := dp .BaseURL () + href
432+ response , err := dp .Page .Request ().Get (fullURL )
433+ require .NoError (dp .t , err , "failed to fetch response body download" )
434+ require .Equal (dp .t , 200 , response .Status (), "response body download should return 200" )
435+
436+ bodyBytes , err := response .Body ()
437+ require .NoError (dp .t , err , "failed to read response body" )
438+
439+ headers := response .Headers ()
440+ ct := headers ["content-type" ]
441+
442+ return href , bodyBytes , ct
443+ }
0 commit comments