First and foremost, the argument you presented is invalid as it consistently passes due to its evaluation of Playwright locator objects for truthiness (page.locator()
does not return a promise, so awaiting it has no effect). It's crucial to deliberately make your tests fail (e.g., by deleting the element from the page) before ensuring they pass again (by re-adding the element) to confirm that there are no false positives and that the test accurately assesses what it intends to. Additionally, it is advisable to employ web first assertions in general to mitigate such errors.
toBeTruthy()
tends to be too broad for most assertion purposes. A more suitable alternative would be to utilize toHaveValue()
since this precisely defines the condition being tested, conveying your intent clearly and providing a more understandable error message in case of failure. This approach also lowers the risk of false positives.
import {expect, test} from "@playwright/test"; // ^1.39.0
const html = `::the HTML content from your post::`;
test('verifies presence of an input named "test_name_1" with value "John"', async ({page}) => {
await page.setContent(html);
await expect(page.locator('td input[name="test_name_1"]')).toHaveValue("John");
});
To populate the input field, you can use .fill()
:
test('can modify the value of input named "test_name_1"', async ({page}) => {
await page.setContent(html);
const input = page.locator('td input[name="test_name_1"]');
await input.fill("whatever");
await expect(input).toHaveValue("whatever");
});