From 09e9c2c7d34b8683bcc0689c4eea4c3dde4f8f46 Mon Sep 17 00:00:00 2001 From: Peter Schretlen Date: Fri, 3 Oct 2025 18:24:05 -0400 Subject: [PATCH 1/6] chore: update default script examples --- src/components/constants.ts | 94 +++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index 03d44c3c3..14b2bd7f6 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -148,64 +148,78 @@ export const TIME_UNIT_OPTIONS = [ export const TEN_MINUTES_IN_MS = 1000 * 60 * 10; export const FIVE_MINUTES_IN_MS = 1000 * 60 * 5; -const EXAMPLE_SCRIPT_SCRIPTED = btoa(`import { check, fail } from 'k6' +const EXAMPLE_SCRIPT_SCRIPTED = btoa(`import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/index.js'; +import { check } from 'k6' import http from 'k6/http' +import secrets from 'k6/secrets'; -export default function main() { - const result = http.get('http://test.k6.io/'); - - // console.log will be represented as logs in Loki - console.log('got a response'); - - // Use check() to test conditions. These show as 'assertions' in the dashboard - // Note: failed check() calls do not impact uptime and reachability - const pass = check(result, { - 'is status 200': (r) => r.status === 200, - }); - - // Use fail() to abort and fail a test, impacting uptime and reachability - if(!pass){ - fail(\`non 200 result \${result.status}\`); +// This example authenticates against the quick pizza API +export default async function main() { + const authURL = 'https://quickpizza.grafana.com/api/users/token/login'; + + // TIP: Secure your credentials using secrets.get() + // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ + const authInfo = { + username: 'default', // username: await secrets.get('quickpizza-username'); + password: '12345678' // password: await secrets.get('quickpizza-password'); } + + const resp = http.post( authURL, JSON.stringify(authInfo), { headers: { 'Content-Type': 'application/json' }}); + + console.log('got a response: ', resp.status); // will appear as logs in Loki + + // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability) + expect(resp.status, 'status should be 200').toBe(200); + + // TIP: Use check() to report test results in the 'assertions' dashboard panel + // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability + check(resp, { 'status should be 200': (r) => r.status === 200 }); + }`); -const EXAMPLE_SCRIPT_BROWSER = btoa(`import { browser } from 'k6/browser'; +const EXAMPLE_SCRIPT_BROWSER = btoa(`import { browser } from "k6/browser"; +import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js"; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; +import secrets from 'k6/secrets'; + +// ------------------------------------------------------------// +// Record your browser script using Grafana k6 Studio! // +// Visit https://grafana.com/docs/k6-studio/set-up/install/ // +// ------------------------------------------------------------// export const options = { scenarios: { - ui: { - executor: 'shared-iterations', - options: { - browser: { - type: 'chromium', - }, - }, + default: { + executor: "shared-iterations", + options: { browser: { type: "chromium" } }, }, }, - thresholds: { - checks: ['rate==1.0'], - }, }; +// This example logs into quickpizza.grafana.com export default async function () { - const context = await browser.newContext(); - const page = await context.newPage(); - + const page = await browser.newPage(); try { - await page.goto("https://test.k6.io/my_messages.php"); + await page.goto("https://quickpizza.grafana.com/login"); + + // TIP: Secure your credentials using secrets.get() + // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ + const username = 'default'; // username = await secrets.get('quickpizza-username'); + const password = '12345678'; // password = await secrets.get('quickpizza-password'); + + await page.locator("#username").fill(username); + await page.locator("#password").fill(password); + await page.locator("button").click(); + + console.log('H2 header: ',await page.locator("//h2").textContent()) // will appear as logs in Loki - await page.locator('input[name="login"]').type("admin"); - await page.locator('input[name="password"]').type("123"); + // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability) + await expect(page.locator("//h2")).toContainText("Your Pizza Ratings:"); - await Promise.all([ - page.waitForNavigation(), - page.locator('input[type="submit"]').click(), - ]); + // TIP: Use check() to report test results in the 'assertions' dashboard panel + // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability + await check(page.locator("//h2"), async (l) => (await l.textContent()) == "Your Pizza Ratings:"); - await check(page.locator("h2"), { - header: async (locator) => (await locator.textContent()) == "Welcome, admin!", - }); } catch (e) { console.log('Error during execution:', e); throw e; From 241b8fbe60b12ebc041a3f978d5a1bca6671f673 Mon Sep 17 00:00:00 2001 From: Peter Schretlen Date: Fri, 3 Oct 2025 19:26:53 -0400 Subject: [PATCH 2/6] chore: switch browser script to admin url --- src/components/constants.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index 14b2bd7f6..774f83659 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -198,27 +198,31 @@ export const options = { // This example logs into quickpizza.grafana.com export default async function () { - const page = await browser.newPage(); + const context = await browser.newContext(); + const page = await context.newPage(); try { - await page.goto("https://quickpizza.grafana.com/login"); + await page.goto("https://quickpizza.grafana.com/admin"); // TIP: Secure your credentials using secrets.get() // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ - const username = 'default'; // username = await secrets.get('quickpizza-username'); - const password = '12345678'; // password = await secrets.get('quickpizza-password'); + const username = 'admin'; // username = await secrets.get('quickpizza-username'); + const password = 'admin'; // password = await secrets.get('quickpizza-password'); await page.locator("#username").fill(username); await page.locator("#password").fill(password); await page.locator("button").click(); - console.log('H2 header: ',await page.locator("//h2").textContent()) // will appear as logs in Loki + const heading = page.locator('//h2'); + await heading.waitFor({ state: "visible", timeout: 5000 }); + + console.log('H2 header: ', await heading.textContent()); // will appear as logs in Loki // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability) - await expect(page.locator("//h2")).toContainText("Your Pizza Ratings:"); + await expect(heading).toContainText("Latest pizza recommendations"); // TIP: Use check() to report test results in the 'assertions' dashboard panel // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability - await check(page.locator("//h2"), async (l) => (await l.textContent()) == "Your Pizza Ratings:"); + await check(heading, async (h) => (await h.textContent()) == "Latest pizza recommendations"); } catch (e) { console.log('Error during execution:', e); From 888990cb156f9fb75f49246e23c3b7ffda2b4e1f Mon Sep 17 00:00:00 2001 From: Peter Schretlen Date: Mon, 6 Oct 2025 12:34:29 -0400 Subject: [PATCH 3/6] chore: comment out unused secrets module import --- src/components/constants.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index 774f83659..6cab34c02 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -151,7 +151,7 @@ export const FIVE_MINUTES_IN_MS = 1000 * 60 * 5; const EXAMPLE_SCRIPT_SCRIPTED = btoa(`import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/index.js'; import { check } from 'k6' import http from 'k6/http' -import secrets from 'k6/secrets'; +// import secrets from 'k6/secrets'; // This example authenticates against the quick pizza API export default async function main() { @@ -160,11 +160,14 @@ export default async function main() { // TIP: Secure your credentials using secrets.get() // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ const authInfo = { - username: 'default', // username: await secrets.get('quickpizza-username'); - password: '12345678' // password: await secrets.get('quickpizza-password'); + username: 'default', // username: await secrets.get('quickpizza-username'), + password: '12345678' // password: await secrets.get('quickpizza-password'), } + const headers = { + headers: { 'Content-Type': 'application/json' }, + }; - const resp = http.post( authURL, JSON.stringify(authInfo), { headers: { 'Content-Type': 'application/json' }}); + const resp = http.post( authURL, JSON.stringify(authInfo), headers); console.log('got a response: ', resp.status); // will appear as logs in Loki @@ -180,7 +183,7 @@ export default async function main() { const EXAMPLE_SCRIPT_BROWSER = btoa(`import { browser } from "k6/browser"; import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js"; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; -import secrets from 'k6/secrets'; +// import secrets from 'k6/secrets'; // ------------------------------------------------------------// // Record your browser script using Grafana k6 Studio! // From d6dff276a64ff006748ba6bffdd262b0d5c4c763 Mon Sep 17 00:00:00 2001 From: Peter Schretlen Date: Wed, 8 Oct 2025 13:22:30 -0400 Subject: [PATCH 4/6] chore: use pizza endpoint instead of auth endpoint. remove expect error message (unused) --- src/components/constants.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index 6cab34c02..d4ebac367 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -153,26 +153,27 @@ import { check } from 'k6' import http from 'k6/http' // import secrets from 'k6/secrets'; -// This example authenticates against the quick pizza API +// This example fetches a pizza recommendation using an auth token export default async function main() { - const authURL = 'https://quickpizza.grafana.com/api/users/token/login'; + const authURL = 'https://quickpizza.grafana.com/api/pizza'; // TIP: Secure your credentials using secrets.get() // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ - const authInfo = { - username: 'default', // username: await secrets.get('quickpizza-username'), - password: '12345678' // password: await secrets.get('quickpizza-password'), - } + const authToken = 'token abcdef0123456789' // await secrets.get('quickpizza-auth-token'), + const headers = { - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + 'Authorization': authToken, + }, }; - const resp = http.post( authURL, JSON.stringify(authInfo), headers); + const resp = http.post( authURL, JSON.stringify({minNumberOfToppings: 2}), headers); console.log('got a response: ', resp.status); // will appear as logs in Loki // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability) - expect(resp.status, 'status should be 200').toBe(200); + expect(resp.status).toBe(200); // TIP: Use check() to report test results in the 'assertions' dashboard panel // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability From c6ac99d6ba2f79b4b0abc81a2cc319894e0d5fce Mon Sep 17 00:00:00 2001 From: Peter Schretlen Date: Wed, 8 Oct 2025 13:35:24 -0400 Subject: [PATCH 5/6] chore: fix variable name for URL --- src/components/constants.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index d4ebac367..720cea532 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -155,7 +155,7 @@ import http from 'k6/http' // This example fetches a pizza recommendation using an auth token export default async function main() { - const authURL = 'https://quickpizza.grafana.com/api/pizza'; + const pizzaURL = 'https://quickpizza.grafana.com/api/pizza'; // TIP: Secure your credentials using secrets.get() // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/ @@ -168,7 +168,7 @@ export default async function main() { }, }; - const resp = http.post( authURL, JSON.stringify({minNumberOfToppings: 2}), headers); + const resp = http.post( pizzaURL, JSON.stringify({minNumberOfToppings: 2}), headers); console.log('got a response: ', resp.status); // will appear as logs in Loki From f013ef8a103c322e95ce2c75ad4d62454a93b0ec Mon Sep 17 00:00:00 2001 From: Chris Bedwell Date: Mon, 13 Oct 2025 11:02:35 +0100 Subject: [PATCH 6/6] fix: check in browser check example to follow correct syntax --- src/components/constants.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/constants.ts b/src/components/constants.ts index 720cea532..eb21bda82 100644 --- a/src/components/constants.ts +++ b/src/components/constants.ts @@ -226,7 +226,9 @@ export default async function () { // TIP: Use check() to report test results in the 'assertions' dashboard panel // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability - await check(heading, async (h) => (await h.textContent()) == "Latest pizza recommendations"); + await check(heading, { + ["Header is present"]: async (h) => (await h.textContent()) == "Latest pizza recommendations" + }); } catch (e) { console.log('Error during execution:', e);