1

我刚开始接触 Playwright,所以我不知道我是否缺少一些东西。我没有任何其他测试框架连接到它。我正在使用@playwright/test v1.14.1.

本次测试:

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);
        }
    });
});

正在产生以下错误:

   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);

如果我删除对 的调用expect,则测试通过,但console.log仍然不会触发。

4

1 回答 1

1

input.evaluateAll在浏览器中执行,这是一个不同的执行上下文,其中expect不可用(Node.js 与例如 Chromium)。

见这里:https ://playwright.dev/docs/core-concepts/#execution-contexts-playwright-and-browser

于 2021-09-21T09:16:32.407 回答