Playwright JavaScript自动化报错:ReferenceError - test未定义
解决Playwright中"test未定义"的引用错误
核心问题:test未导入
你代码里用了test函数但没从@playwright/test包中导入,这是触发引用错误的直接原因。修改import语句,把test加入解构导入列表即可。顺带修复其他代码问题
除了test未定义,代码里还有两个小问题:expect.page.toHaveURL写法错误,正确的断言方式是await expect(page).toHaveURL()- 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
相关产品推荐
相关产品推荐

