Menu
Scroll for more
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Documentation
Grafana k6
Using k6 browser
Recommended practices
Use Page.close to clean up test resources
Open source
Use Page.close to clean up test resources
When running k6 browser tests with event-based APIs, such as page.on, tests may not always properly flush metrics or clean up resources automatically, which can lead to incomplete metric reporting or leftover event handlers.
To prevent this, always call
page.close() at the end of your browser tests. Doing so ensures accurate and complete metric collection, cleans up event listeners to prevent resource leaks, and simplifies test teardown for improved reliability.
Example
JavaScript
import { browser } from "k6/browser";
import { check } from "k6";
const BASE_URL = __ENV.BASE_URL || "https://quickpizza.grafana.com/";
export const options = {
scenarios: {
ui: {
executor: "shared-iterations",
options: {
browser: {
type: "chromium",
},
},
},
},
};
export default async function () {
let checkData;
const page = await browser.newPage();
try {
await page.goto(BASE_URL);
checkData = await page.locator("h1").textContent();
check(page, {
header: checkData == "Looking to break out of your pizza routine?",
});
await page.locator('//button[. = "Pizza, Please!"]').click();
await page.waitForTimeout(500);
await page.screenshot({ path: "screenshot.png" });
checkData = await page.locator("div#recommendations").textContent();
check(page, {
recommendation: checkData != "",
});
} finally {
//Always close the page at the end
await page.close();
}
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.