Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add page.on('response') #4296

Open
wants to merge 12 commits into
base: add/page-on-request
Choose a base branch
from
Open

Conversation

ankur22
Copy link
Contributor

@ankur22 ankur22 commented Jan 29, 2025

What?

This implements the page.on('response') API from Playwright. When the handler is created, all subsequent responses will be directed to the handler, where the user can interact with the read only response object. The response is intercepted after it is received from the website under test to the browser.

An example usage is:

import { browser } from 'k6/browser'

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
}

export default async function () {
  const page = await browser.newPage()

  // Logs the url that the response comes from (where the request was made to).
  page.on('response', async (response) => {
    console.log(response.url());
  })

  await page.goto('https://quickpizza.grafana.com/', { waitUntil: 'networkidle' })

  await page.close();
}

Why?

This can be useful to:

  • retrieve data that is to be used later, such as auth tokens;
  • assert certain conditions on an expected response;

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

Updates: #4234
Linked to: #4290

@ankur22 ankur22 requested a review from a team as a code owner January 29, 2025 12:04
@ankur22 ankur22 requested review from inancgumus and oleiade and removed request for a team January 29, 2025 12:04
@ankur22 ankur22 marked this pull request as draft January 29, 2025 12:04
@ankur22 ankur22 force-pushed the add/page-on-request branch from 05b8969 to 6ac9bd0 Compare January 29, 2025 16:34
@ankur22 ankur22 changed the title Add/page on response Add page.on('response') Jan 29, 2025
@ankur22 ankur22 force-pushed the add/page-on-response branch from 3dd1239 to 78ccfc3 Compare January 29, 2025 16:38
@ankur22 ankur22 force-pushed the add/page-on-request branch from 6ac9bd0 to 09fbe5d Compare January 29, 2025 19:46
@ankur22 ankur22 force-pushed the add/page-on-response branch from 50e414d to e51b45d Compare January 29, 2025 19:47
@ankur22 ankur22 marked this pull request as ready for review January 29, 2025 19:55
@ankur22 ankur22 force-pushed the add/page-on-response branch 4 times, most recently from 221509d to 294a817 Compare January 29, 2025 20:57
Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments about this one would be similar to #4290. They are very alike.

Comment on lines +528 to +547
p.eventHandlersMu.RLock()
defer p.eventHandlersMu.RUnlock()
for _, h := range p.eventHandlers[EventPageResponseCalled] {
err := func() error {
// Handlers can register other handlers, so we need to
// unlock the mutex before calling the next handler.
p.eventHandlersMu.RUnlock()
defer p.eventHandlersMu.RLock()

// Call and wait for the handler to complete.
return h(PageOnEvent{
Response: resp,
})
}()
if err != nil {
p.logger.Warnf("onResponse", "handler returned an error: %v", err)
return
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We start to duplicate this logic in Page.urlTagName, Page.onRequest, and Page.onConsoleAPICalled (although this one differs a little). What do you think about introducing a helper function in #4290?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i had a feeling you were going to point that out 😄 Sure, i think that should be done in a separate refactor PR 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the test based on comments made in #4290.

@ankur22 ankur22 force-pushed the add/page-on-request branch 2 times, most recently from 2709a6c to 522a411 Compare January 30, 2025 09:41
@ankur22 ankur22 force-pushed the add/page-on-response branch from 294a817 to fea5839 Compare January 30, 2025 10:09
This is where the response will be set for the page.on handler to read.
This method is to be called when a new response has been received from
the WuT to chrome. It takes the response and sends it to the page.on
handler where the user test script can read the response data.
There seems to be a race condition with chrome and CDP requests when
trying to retrieve the response body. If the call to retrieve the
response body is too "quick", then we get an error back.

However with retries and a sleep it seems to solve the issue with the
small tests i have performed. I suspect this will be a greater issue in
the wild.
The refactor tries to work with the dynamic server values. It also
works with the defined types in common instead of a map.
Work with slices.IndexFunc and sort HeadersArray before comparing the
response and expected.
@ankur22 ankur22 force-pushed the add/page-on-response branch from fea5839 to 0833a1d Compare January 31, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants