Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Documentation
Grafana k6
Using k6 browser
How to write browser tests
Interact with elements on your webpage
Open source
Interact with elements on your webpage
You can use page.locator() and pass in the element’s selector you want to find on the page. page.locator() will create and return a
Locator object, which you can later use to interact with the element.
To find out which selectors the browser module supports, check out Selecting Elements.
Note
You can also use
page.$()instead ofpage.locator(). You can find the differences betweenpage.locator()andpage.$in the Locator API documentation.
JavaScript
JavaScript
import { browser } from 'k6/browser';
export const options = {
scenarios: {
ui: {
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/my_messages.php');
// Enter login credentials
await page.locator('input[name="login"]').type('admin');
await page.locator('input[name="password"]').type('123');
await page.screenshot({ path: 'screenshots/screenshot.png' });
} finally {
await page.close();
}
}The preceding code creates and returns a Locator object with the selectors for both login and password passed as arguments.
Within the Locator API, various methods such as type() can be used to interact with the elements. The type() method types a text to an input field.
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.