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.
Open source
toHaveValue()
The toHaveValue() method asserts that an element has a specific value. This is a retrying assertion that automatically waits for the element to have the expected value.
Syntax
JavaScript
await expect(locator).toHaveValue(value);
await expect(locator).not.toHaveValue(value);
await expect(locator).toHaveValue(value, options);Parameters
| Parameter | Type | Description |
|---|---|---|
| value | string | The expected value |
| options | RetryConfig | Optional configuration options |
Returns
| Type | Description |
|---|---|
Promise<void> | A promise that resolves when the assertion passes |
Description
The toHaveValue() method checks if an element has a specific value. This is primarily used for form elements like input fields, textareas, and select elements. The method compares the element’s current value with the expected value.
This is a retrying assertion that will automatically re-check the element’s value until it matches the expected value or the timeout is reached.
Usage
JavaScript
import { browser } from 'k6/browser';
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
}
};
export default async function () {
const page = await browser.newPage();
// Go to a test page that demonstrates input functionality
await page.goto('https://quickpizza.grafana.com/login');
// Fill input and check value
await page.locator('#username').fill('testuser');
await expect(page.locator('#username')).toHaveValue('testuser');
// Check textarea value
await page.locator('#message').fill('Hello world!');
await expect(page.locator('#message')).toHaveValue('Hello world!');
// Check select option value
await page.locator('#country').selectOption('us');
await expect(page.locator('#country')).toHaveValue('us');
// Check initial empty values
await expect(page.locator('#empty-field')).toHaveValue('');
// Check that field doesn't have specific value
await expect(page.locator('#username')).not.toHaveValue('wronguser');
}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.