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

Merged
merged 12 commits into from
Feb 24, 2025
Merged

Add page.on('response') #4296

merged 12 commits into from
Feb 24, 2025

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)

@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.

@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
@ankur22 ankur22 force-pushed the add/page-on-response branch from fea5839 to 0833a1d Compare January 31, 2025 10:19
@oleiade
Copy link
Member

oleiade commented Feb 13, 2025

What's your take on this @inancgumus ? Anything specific I should pay attention to or know to effectively review it? 🙇🏻

@inancgumus
Copy link
Member

What's your take on this @inancgumus ? Anything specific I should pay attention to or know to effectively review it? 🙇🏻

@oleiade For my take, please check out the resolved discussions in #4290, as this PR is pretty much the same as the other PR. There is also this shortcoming. Let me know if you need anything else 🙇

@inancgumus inancgumus force-pushed the add/page-on-request branch 2 times, most recently from 6b8b7f9 to 6f26ce7 Compare February 14, 2025 14:59
Base automatically changed from add/page-on-request to master February 19, 2025 15:47
@inancgumus inancgumus requested review from oleiade and removed request for oleiade February 19, 2025 18:31
@inancgumus inancgumus added the documentation-needed A PR which will need a separate PR for documentation label Feb 19, 2025
@inancgumus inancgumus added this to the v1.0.0-rc1 milestone Feb 20, 2025
ankur22 and others added 12 commits February 24, 2025 17:43
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.
@inancgumus
Copy link
Member

@oleiade Could you check this PR out? 🙇

Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

Redid a short pass. LGTM (as far as I can tell 😅 ) 🚀

@inancgumus inancgumus merged commit 4f2acb9 into master Feb 24, 2025
28 checks passed
@inancgumus inancgumus deleted the add/page-on-response branch February 24, 2025 17:20
@inancgumus inancgumus removed the documentation-needed A PR which will need a separate PR for documentation label Feb 25, 2025
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.

3 participants