我可以从mocha / phantomjs单元测试中获取页面截图吗?

时间:2013-03-28 11:35:32

标签: javascript unit-testing phantomjs mocha

我正在使用grunt-mocha通过phantomJS运行单元测试。

我知道phantomJS有很多有用的功能。我可以在摩卡测试中访问它吗?

我查看了window对象这些显而易见的地方,看看我是否可以以某种方式访问​​页面对象,但似乎没有什么显而易见的。

具体来说,我想呈现正在测试的页面的屏幕截图。

2 个答案:

答案 0 :(得分:1)

这称为"Screen Capture" in the documentation

重要的是,您需要引用WebPage,而不是浏览器window对象(这只是模拟JavaScript通常可以在浏览器中看到的内容)。

var page = require('webpage').create();
page.open('http://github.com/', function () {
    page.render('github.png');
    phantom.exit();
});

答案 1 :(得分:1)

我已经为grunt-mocha分叉并添加了屏幕截图支持。请参阅https://github.com/bjfletcher/grunt-mocha中的“屏幕截图”部分。

这有望被拉入Kelly的grunt-mocha并最终进入npmjs.org:https://github.com/kmiyashiro/grunt-mocha/pull/109