我使用 puppeteer 进行 UI 自动化,并使用 Jest 作为测试运行程序。我可以按顺序运行测试,但在并行运行测试时遇到问题。 我的页面启动
beforeEach(async () => {
browser = await puppeteer.launch({
headless: false,
//slowMo: 80,
args: [`--window-size=${width},${height}`]
});
page = await browser.newPage();
await page.setViewport({width, height});
});
// and my tests are
test.concurrent("description", async () => {
await page.goto('xxxx.com');
//test code goes here
}, timeout)
test.concurrent("description ", async () => {
//test code goes here
}, timeout)
我收到以下错误,
TypeError: Cannot read property 'goto' of undefined
(node:9552) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addExpectationResult' of undefined
(node:9552) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:9552) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre cated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
npm ERR! Test failed. See above for more details.
在这方面的任何帮助将不胜感激。 问候, 周杰伦
最佳答案
您面临这个问题是因为当您使用箭头函数时,页面变量的范围仅限于 beforeEach block 。该 block 不会将其定义的变量传递给测试或描述 block 。我通过为测试设置一个全局变量解决了这个问题,并在测试设置中使用 global.PAGE = wait browser.newPage()
添加了页面对象。所以在它运行测试之前。
要检查是否是这种情况,您只需在 test.concurrent() block 中尝试 console.log(page);
即可。
查看官方 Jest 文档。他们已经有了一个工作设置,其中包含 Jest 和 puperteer。
希望这有帮助。 干杯。
关于javascript - 使用 Jest 和 Puppeteer 进行并行支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934994/