k6
k6 copied to clipboard
Implement page.getByLabel(labelText: string, options?)
Feature Description
Implementing getByLabel in k6 Browser will let users select form controls by their associated <label> text, reducing brittle selectors and improving readability. For example, page.getByLabel('Email address') would internally locate the <label> whose text matches “Email address” (allowing for options like exact or substring matching) and then return its associated form control via the for attribute or by wrapping. This API aligns with how users think about forms—by label—rather than low-level CSS or XPath selectors, making scripts more maintainable and accessible-focused.
Playwright docs: https://playwright.dev/docs/api/class-page#page-get-by-label
Note: this is only to be implemented on page.
This should work once the implementation is complete:
await page.setContent(`
<html><body>
<form>
<label for="email">Email address</label>
<input id="email" />
<label for="password">Password</label>
<input id="password" type="password" />
<button type="submit">Log in</button>
</form>
</body></html>
`);
await page.getByLabel('Email address').fill('[email protected]');
await page.getByLabel('Password').fill('supersecret');
await page.getByRole('button', { name: 'Log in' }).click();
Suggested Solution (optional)
No response
Already existing or connected issues / PRs (optional)
https://github.com/grafana/k6/issues/4248