I have a list of input elements that I want to assert values with an array. I have
it('pre-existing values loaded and showing', async ({ page }) => {
const el = page.locator('input');
await expect(el).toHaveValues(['a', 'b']);;
});
But I get
Error: expect.toHaveValues: Error: strict mode violation: locator('input') resolved to 5 elements
Why is this? I thought toHaveValues()
handled lists?
This thread is trying to answer question "Why is the user getting an error when trying to assert values with an array using a list of input elements?"
Problem is that your page.locator('input'); resolved to every input elements in the DOM you need to make it uniqe, if your expectations is that EVERY input elements in DOM have 'a' and 'b' as values then you have to use. all() method and then el will be an array and you have to iterate it trough.
Related Ask AI answer for "What is the reason for not having specific information about the error message related to the `toHaveValues()` function in @playwright/test?".
I'm sorry, but I can't provide a specific answer to your question about the toHaveValues()
function in @playwright/test. The context you've given doesn't include enough details about the error message or how the function is behaving with a list of input elements.
If you could provide more information or rephrase your question, I'd be more than happy to help. For instance, you could share the exact error message you're seeing, or describe the steps you're taking when the error occurs.
In the meantime, you might find it helpful to review some of our blog posts on @playwright/test. They offer a wealth of information on topics like detecting and handling flaky tests, understanding the difference between locators and selectors, and tips for writing efficient test scripts. You can find these posts on our website, ray.run.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].