You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright JavaScript自动化报错:ReferenceError - test未定义

解决Playwright中"test未定义"的引用错误
  • 核心问题:test未导入
    你代码里用了test函数但没从@playwright/test包中导入,这是触发引用错误的直接原因。修改import语句,把test加入解构导入列表即可。

  • 顺带修复其他代码问题
    除了test未定义,代码里还有两个小问题:

    1. expect.page.toHaveURL写法错误,正确的断言方式是await expect(page).toHaveURL()
    2. Playwright在测试结束后会自动关闭页面,无需手动调用page.close(),可以删掉这行

修正后的完整代码:

import { test, expect } from '@playwright/test';

test('Home Page', async ({page}) => {
    await page.goto('https://www.myntra.com/');
    const pageTitle = await page.title();
    console.log('Page title is:', pageTitle);
    await expect(page).toHaveTitle('Online Shopping for Women, Men, Kids Fashion & Lifestyle - Myntra');
    const pageURL = page.url();
    console.log('Page URL is:', pageURL);
    await expect(page).toHaveURL('https://www.myntra.com/');
})

内容的提问来源于stack exchange,提问作者Krrish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:17:00