Reputation: 11323
I'm just getting started with Playwright so I don't know if there is something I'm missing. I do not have any other testing framework wired up to it. I'm using @playwright/test v1.14.1
.
This test:
import { test, expect } from "@playwright/test";
test("focus sets tab indexes appropriately", async ({ page }) => {
await page.goto("http://localhost:3000/test");
const inputs = page.locator("input");
await expect(inputs).toHaveCount(5);
await inputs.evaluateAll(async (nodes) => {
console.log(nodes);
for (const node of nodes) {
console.log(node);
await expect(node.tabIndex).toBe(0);
}
});
});
is producing the following error:
locator.evaluateAll: Evaluation failed: ReferenceError: _test is not defined
at eval (eval at evaluate (:3:1339), <anonymous>:6:7)
at t.default.evaluate (<anonymous>:3:1362)
at t.default.<anonymous> (<anonymous>:1:44)
5 | const inputs = page.locator("input");
6 | await expect(inputs).toHaveCount(5);
> 7 | await inputs.evaluateAll(async (nodes) => {
| ^
8 | console.log(nodes);
9 | for (const node of nodes) {
10 | console.log(node);
If I remove the call to expect
, the test passes but the console.log
still will not fire off.
Upvotes: 3
Views: 5083
Reputation: 4222
Simple Code Example using evaluateAll
with expect
:
// Evaluate all elements with a specific selector
const elementHandles= await page.evaluateAll(() => {
const elements = document.querySelectorAll('.my-selector');
return Array.from(elements);
});
// Assert that each element has a specific text content
for (const elementHandle of elementHandles) {
await expect(elementHandle).toHaveText('Expected Text');
}
Upvotes: 0
Reputation: 3222
input.evaluateAll
gets executed inside the browser which is a different execution context in which expect
is not available (Node.js vs. e.g. Chromium).
See here: https://playwright.dev/docs/evaluating
Upvotes: 4