browser
The browser module APIs aim for rough compatibility with the Playwright API for NodeJS.
Note that because k6 does not run in NodeJS, the browser module APIs will slightly differ from their Playwright counterparts.
You can find examples of using the browser module API in our getting started guide.
Note
To work with the browser module, make sure you are using the latest k6 version.
Properties
The table below lists the properties you can import from the browser module ('k6/browser'
).
Browser Module API
The browser module is the entry point for all your tests, and it is what interacts with the actual web browser via Chrome DevTools Protocol (CDP). It manages:
- BrowserContext which is where you can set a variety of attributes to control the behavior of pages;
- and Page which is where your rendered site is displayed.
Example
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
thresholds: {
checks: ['rate==1.0'],
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://test.k6.io/');
} finally {
await page.close();
}
}
Then, you can run the test with this command. Also, see the browser module options for customizing the browser module’s behavior using environment variables.
$ k6 run script.js
Devices example
To emulate the browser behaviour on a mobile device and approximately measure the browser performance, you can import devices
from k6/browser
.
import { browser, devices } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
thresholds: {
checks: ['rate==1.0'],
},
};
export default async function () {
const iphoneX = devices['iPhone X'];
const context = await browser.newContext(iphoneX);
const page = await context.newPage();
try {
await page.goto('https://test.k6.io/');
} finally {
page.close();
}
}
Browser-level APIs
Browser module options
You can customize the behavior of the browser module by providing browser options as environment variables.
The following command passes the browser options as environment variables to launch a headful browser with custom arguments.
$ K6_BROWSER_HEADLESS=false K6_BROWSER_ARGS='show-property-changed-rects' k6 run script.js