Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/jslib/testing/retrying-assertions/tohavevalue.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/jslib/testing/retrying-assertions/tohavevalue/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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
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
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

