Using environment variables in browser thresholds

Hey all, Im using thresholds and the k6 browser to capture core web vital metrics for each individual page in my test. To do so, I am adding a threshold for each page and for each web vitals I want to track. Is there a way I can at least use an env variable for baseurl for each of the thresholds?

Hi @justinyoon1,

If i understand you correctly, you would like to pass in a constant as an env variable (which is the base url of the website under test e.g. https://test.k6.io), and define thresholds using that env variable in the options block where the base url is used as part of the threshold name, so something like the following:

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

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
            type: 'chromium',
        },
      },
    },
  },
  thresholds: {
    [`browser_http_req_duration{url:${__ENV.BASE_URL}/static/css/site.css}`]: ['p(90) < 100'],
  }
}

export default async function() {
  const context = browser.newContext();
  const page = context.newPage();

  await page.goto(`${__ENV.BASE_URL}`, { waitUntil: 'networkidle' });

  page.close();
}

And you would run this test script with k6 run -e BASE_URL=https://test.k6.io test.js

Let me know if this helps or not.

Cheers,
Ankur

1 Like

that worked! i knew there was a way, but i wasnt quite getting it.

thanks again.

2 Likes