@@ -148,64 +148,88 @@ export const TIME_UNIT_OPTIONS = [
148148export const TEN_MINUTES_IN_MS = 1000 * 60 * 10 ;
149149export const FIVE_MINUTES_IN_MS = 1000 * 60 * 5 ;
150150
151- const EXAMPLE_SCRIPT_SCRIPTED = btoa ( `import { check, fail } from 'k6'
151+ const EXAMPLE_SCRIPT_SCRIPTED = btoa ( `import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/index.js';
152+ import { check } from 'k6'
152153import http from 'k6/http'
154+ // import secrets from 'k6/secrets';
153155
154- export default function main() {
155- const result = http.get('http://test.k6.io/');
156+ // This example fetches a pizza recommendation using an auth token
157+ export default async function main() {
158+ const pizzaURL = 'https://quickpizza.grafana.com/api/pizza';
156159
157- // console.log will be represented as logs in Loki
158- console.log('got a response');
160+ // TIP: Secure your credentials using secrets.get()
161+ // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/
162+ const authToken = 'token abcdef0123456789' // await secrets.get('quickpizza-auth-token'),
159163
160- // Use check() to test conditions. These show as 'assertions' in the dashboard
161- // Note: failed check() calls do not impact uptime and reachability
162- const pass = check(result, {
163- 'is status 200': (r) => r.status === 200,
164- });
165-
166- // Use fail() to abort and fail a test, impacting uptime and reachability
167- if(!pass){
168- fail(\`non 200 result \${result.status}\`);
169- }
164+ const headers = {
165+ headers: {
166+ 'Content-Type': 'application/json',
167+ 'Authorization': authToken,
168+ },
169+ };
170+
171+ const resp = http.post( pizzaURL, JSON.stringify({minNumberOfToppings: 2}), headers);
172+
173+ console.log('got a response: ', resp.status); // will appear as logs in Loki
174+
175+ // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability)
176+ expect(resp.status).toBe(200);
177+
178+ // TIP: Use check() to report test results in the 'assertions' dashboard panel
179+ // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability
180+ check(resp, { 'status should be 200': (r) => r.status === 200 });
181+
170182}` ) ;
171183
172- const EXAMPLE_SCRIPT_BROWSER = btoa ( `import { browser } from 'k6/browser';
184+ const EXAMPLE_SCRIPT_BROWSER = btoa ( `import { browser } from "k6/browser";
185+ import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";
173186import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
187+ // import secrets from 'k6/secrets';
188+
189+ // ------------------------------------------------------------//
190+ // Record your browser script using Grafana k6 Studio! //
191+ // Visit https://grafana.com/docs/k6-studio/set-up/install/ //
192+ // ------------------------------------------------------------//
174193
175194export const options = {
176195 scenarios: {
177- ui: {
178- executor: 'shared-iterations',
179- options: {
180- browser: {
181- type: 'chromium',
182- },
183- },
196+ default: {
197+ executor: "shared-iterations",
198+ options: { browser: { type: "chromium" } },
184199 },
185200 },
186- thresholds: {
187- checks: ['rate==1.0'],
188- },
189201};
190202
203+ // This example logs into quickpizza.grafana.com
191204export default async function () {
192205 const context = await browser.newContext();
193206 const page = await context.newPage();
194-
195207 try {
196- await page.goto("https://test.k6.io/my_messages.php ");
208+ await page.goto("https://quickpizza.grafana.com/admin ");
197209
198- await page.locator('input[name="login"]').type("admin");
199- await page.locator('input[name="password"]').type("123");
210+ // TIP: Secure your credentials using secrets.get()
211+ // https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/manage-secrets/
212+ const username = 'admin'; // username = await secrets.get('quickpizza-username');
213+ const password = 'admin'; // password = await secrets.get('quickpizza-password');
200214
201- await Promise.all([
202- page.waitForNavigation(),
203- page.locator('input[type="submit"]').click(),
204- ]);
215+ await page.locator("#username").fill(username);
216+ await page.locator("#password").fill(password);
217+ await page.locator("button").click();
205218
206- await check(page.locator("h2"), {
207- header: async (locator) => (await locator.textContent()) == "Welcome, admin!",
219+ const heading = page.locator('//h2');
220+ await heading.waitFor({ state: "visible", timeout: 5000 });
221+
222+ console.log('H2 header: ', await heading.textContent()); // will appear as logs in Loki
223+
224+ // TIP: Use expect() to immediately abort execution and fail a test (impacts uptime/reachability)
225+ await expect(heading).toContainText("Latest pizza recommendations");
226+
227+ // TIP: Use check() to report test results in the 'assertions' dashboard panel
228+ // Scripts continue to run even if a check fails. Failed checks don't impact uptime and reachability
229+ await check(heading, {
230+ ["Header is present"]: async (h) => (await h.textContent()) == "Latest pizza recommendations"
208231 });
232+
209233 } catch (e) {
210234 console.log('Error during execution:', e);
211235 throw e;
0 commit comments