【问题标题】:In Playwright, how do I fix the error locator.evaluateAll: Evaluation failed: ReferenceError: _test is not defined?在 Playwright 中,如何修复错误 locator.evaluateAll: Evaluation failed: ReferenceError: _test is not defined?
【发布时间】:2021-11-13 20:31:21
【问题描述】:

我刚开始接触 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 仍然不会触发。

【问题讨论】:

  • input.evaluateAll 在浏览器中执行,这是一个不同的执行上下文,其中 expect 不可用(Node.js 与例如 Chromium)。见这里:playwright.dev/docs/core-concepts/…
  • @MaxSchmitt 明白了。这就说得通了。如果你回答这个问题,我会结束这个问题。谢谢你的帮助。

标签: playwright playwright-test


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
  • 2014-03-03
相关资源
最近更新 更多