return mocha 的 puppeteer 测试结果如何?
How do you return mocha's test results from puppeteer?
我想 运行 我的 mochajs front-end unit tests in a headless browser, specifically using puppeteer. So, following the very simple example on the mochajs
page here,我正在 运行 进行基本单元测试,正在查看 mocha
的结果,它们呈现在 page 因为它们应该是。我看到当我在真实浏览器中加载简单示例时,它们也会呈现为铬的 console
。一切都很棒。但是,我想 return 这些来自调用此脚本的结果。我如何return这些来自运行宁mochajs
的测试结果在puppeteer
?换句话说:
$ node my-script-running-mocha-in-puppeteer.js
Array
#indexOf()
✓ should return -1 when the value is not present
1 passing (9ms)
我的代码基本上是这样的:
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.setContent(`
/*
HTML page with mocha, unit tests and source.
*/
`);
// Get page content
const content = await page.content();
// I see the mocha test result in the page but now I
// want to return them from this script.
console.log(content);
await browser.close();
});
正如您所说,mocha 也在将数据打印到控制台中,这可能是获取测试结果的最简单方法。
简单的方法
要简单地反映浏览器的 IO,包括记录到控制台的任何数据,您可以在启动浏览器时启用选项 dumpio
:
puppeteer.launch({ dumpio: true })
引自 puppeteer.launch
的文档:
dumpio
<boolean
> Whether to pipe the browser process stdout and stderr into process.stdout
and process.stderr
. Defaults to false
.
请注意,这还会将其他信息从浏览器打印到控制台。如果您只查找控制台数据,则可以使用第二个选项。
仅记录对 console.log
等的调用
要仅获取记录到 console.log
或 console.info
等的数据,您可以监听 console
事件。
page.on('console', consoleMessage => {
console.log(consoleMessage.text());
if (consoleMessage.type() === 'error') {
// handle error output
}
});
这样,您就可以抓取所有进入控制台的输出。您甚至可以使用 consoleMessage.type()
来区分错误、警告和正常输出。
我想 运行 我的 mochajs front-end unit tests in a headless browser, specifically using puppeteer. So, following the very simple example on the mochajs
page here,我正在 运行 进行基本单元测试,正在查看 mocha
的结果,它们呈现在 page 因为它们应该是。我看到当我在真实浏览器中加载简单示例时,它们也会呈现为铬的 console
。一切都很棒。但是,我想 return 这些来自调用此脚本的结果。我如何return这些来自运行宁mochajs
的测试结果在puppeteer
?换句话说:
$ node my-script-running-mocha-in-puppeteer.js
Array
#indexOf()
✓ should return -1 when the value is not present
1 passing (9ms)
我的代码基本上是这样的:
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.setContent(`
/*
HTML page with mocha, unit tests and source.
*/
`);
// Get page content
const content = await page.content();
// I see the mocha test result in the page but now I
// want to return them from this script.
console.log(content);
await browser.close();
});
正如您所说,mocha 也在将数据打印到控制台中,这可能是获取测试结果的最简单方法。
简单的方法
要简单地反映浏览器的 IO,包括记录到控制台的任何数据,您可以在启动浏览器时启用选项 dumpio
:
puppeteer.launch({ dumpio: true })
引自 puppeteer.launch
的文档:
dumpio
<boolean
> Whether to pipe the browser process stdout and stderr intoprocess.stdout
andprocess.stderr
. Defaults tofalse
.
请注意,这还会将其他信息从浏览器打印到控制台。如果您只查找控制台数据,则可以使用第二个选项。
仅记录对 console.log
等的调用
要仅获取记录到 console.log
或 console.info
等的数据,您可以监听 console
事件。
page.on('console', consoleMessage => {
console.log(consoleMessage.text());
if (consoleMessage.type() === 'error') {
// handle error output
}
});
这样,您就可以抓取所有进入控制台的输出。您甚至可以使用 consoleMessage.type()
来区分错误、警告和正常输出。