grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
344 stars 41 forks source link

Add request.fetchPostData API #1470

Open ankur22 opened 1 month ago

ankur22 commented 1 month ago

Feature Description

request.fetchPostData is useful when the postdata wasn't available when the request was intercepted, but it does have request data linked to it (this is when the incoming event field HasPostData is true). This should replace the existing request.postData API.

Suggested Solution (optional)

It should be something like this:

// FetchPostData returns the request post data, if any.
//
// If will attempt to fetch the data if it should have some but nothing is
// cached locally.
//
// This relies on PostDataEntries. It will only ever return the 0th entry. To
// retrieve the full list use PostDataEntries.
func (r *Request) FetchPostData() (string, error) {
    if len(r.postDataEntries) > 0 {
        return r.postDataEntries[0], nil
    }
    if r.hasPostData && len(r.postDataEntries) == 0 {
        action := network.GetRequestPostData(r.getID())
        pd, err := action.Do(cdp.WithExecutor(r.ctx, r.frame.page.session))
        if err != nil {
            return "", fmt.Errorf("getting postData: %w", err)
        }

        r.postDataEntries = []string{pd}

        return pd, nil
    }

    return "", nil
}

I hesitate to add this in since we've got no use case for it.

Already existing or connected issues / PRs (optional)

No response