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 core to inject k6 object into window #1007

Merged
merged 6 commits into from
Aug 31, 2023
Merged

Add core to inject k6 object into window #1007

merged 6 commits into from
Aug 31, 2023

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Aug 21, 2023

What?

The change injects window.k6 = {}; into all browser contexts and subsequent pages that are created.

Why?

In some cases we want to pass information to other apps to allow them to infer who is performing requests against a website. For now we're exposing an empty object, but we hope to extend the object with meta data that will be useful for other apps that have been instrumented on the website.

Test

To test this change you can run the following k6 script, which will give you enough time to go into the chrome dev tools console and type in window.k6 which should return an empty object ({}).

import { browser } from 'k6/experimental/browser';
import { sleep } from 'k6';

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

export default async function () {
  const page = browser.newPage();
  await page.goto("https://test.k6.io");

  sleep(20);

  page.close();
  context.close();
}
Screenshot 2023-08-21 at 14 47 18 Screenshot 2023-08-21 at 14 47 22

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Closes: #1017

@ankur22 ankur22 marked this pull request as draft August 21, 2023 14:03
@ankur22 ankur22 force-pushed the feat/inject-k6-obj branch 2 times, most recently from 3cf3de1 to ef03900 Compare August 21, 2023 15:05
@ankur22 ankur22 marked this pull request as ready for review August 30, 2023 15:32
In some cases we want to pass on information to other apps to allow
them to infer who or what is performing requests against a website.
For now we're exposing an empty object, but we hope to extend the
object with meta data that will be useful for other apps that have been
instrumented on the website.
This test will evaluate and return the value. It should be an empty
object and not null.
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.

Looks good to me overall, and I'm excited about this feature! 😊

I do have a few points for us to chat about. Also, it might be helpful to include some comments in the code to clarify the role of k6ObjScript.

common/browser_context.go Outdated Show resolved Hide resolved
common/browser_context_test.go Outdated Show resolved Hide resolved
ankur22 and others added 4 commits August 31, 2023 08:57
Co-authored-by: İnanç Gümüş <inanc.gumus@grafana.com>
We're renaming this since this file will embed scripts other than
web vital scripts.
To be consistent with how we work with other injected scripts, the k6
object is being moved to its own js file. This also helps us avoid
working with globals.

It's worth noting that the k6 object will need to be mutable at some
stage to add test specific unique metadata in the future.

Resolves: #1007 (comment)
This commit now works with the new k6 object file instead of the global
k6 object file.

Resolves: #1007 (comment)
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.

Nice job, thanks!

Copy link
Collaborator

@ka3de ka3de left a comment

Choose a reason for hiding this comment

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

LGTM 👏

@ankur22 ankur22 merged commit 594a8df into main Aug 31, 2023
12 checks passed
@ankur22 ankur22 deleted the feat/inject-k6-obj branch August 31, 2023 09:14
ankur22 added a commit that referenced this pull request Aug 31, 2023
To be consistent with how we work with other injected scripts, the k6
object is being moved to its own js file. This also helps us avoid
working with globals.

It's worth noting that the k6 object will need to be mutable at some
stage to add test specific unique metadata in the future.

Resolves: #1007 (comment)
inancgumus added a commit that referenced this pull request Aug 31, 2023
After having a discussion, we decided to make this parallel. See the
related discussion here:
#1007 (comment)
inancgumus added a commit that referenced this pull request Aug 31, 2023
After having a discussion, we decided to make this parallel. See the
related discussion here:
#1007 (comment)
inancgumus added a commit that referenced this pull request Aug 31, 2023
After having a discussion, we decided to make this parallel. See the
related discussion here:
#1007 (comment)
inancgumus added a commit that referenced this pull request Aug 31, 2023
After having a discussion, we decided to make this parallel. See the
related discussion here:
#1007 (comment)
inancgumus added a commit that referenced this pull request Aug 31, 2023
After having a discussion, we decided to make this parallel. See the
related discussion here:
#1007 (comment)
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.

POC: Inject metadata to k6 browser session such as testId (Grafana Faro x k6 browser)
3 participants