javascript 保存控制台消息以在 nightwatch.js 中进行调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29443804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save console messages for debugging in nightwatch.js
提问by Vasiliy Vanchuk
How can I get all console messages on nightwatch.js debugging?
如何在 nightwatch.js 调试中获取所有控制台消息?
At phantom it's possible to use page.onError
handler. Can I do the same with nightwatch?
在幻像中可以使用page.onError
处理程序。我可以用夜视仪做同样的事情吗?
I know about window.onerror
but is there way to save all console messages?
我知道window.onerror
但是有没有办法保存所有控制台消息?
Can anyone share working config / code ?
任何人都可以分享工作配置/代码吗?
回答by Vasiliy Vanchuk
Solution:
解决方案:
module.exports = {
'Check getting log messages' : function (client) {
client
.url('http://jsbin.com/rohilugegi/1/')
.getLogTypes(function(result) {
console.log(result);
})
.getLog('browser', function(result) {
console.log(result);
})
;
return client;
},
It will give output
它会给输出
[Start] Test Suite ================== Running: Check getting log messages [ 'har', 'browser', 'client', 'server' ] [ { message: 'Test error\n error (:0)', timestamp: 1428447687315, level: 'WARNING' }, { message: 'Test log (:)', timestamp: 1428447687315, level: 'INFO' } ] No assertions ran.
[Start] Test Suite ================== Running: Check getting log messages [ 'har', 'browser', 'client', 'server' ] [ { message: 'Test error\n error (:0)', timestamp: 1428447687315, level: 'WARNING' }, { message: 'Test log (:)', timestamp: 1428447687315, level: 'INFO' } ] No assertions ran.
Commands getLogTypesand getLogare already implemented at client commandsbut their description are absent at site API
命令getLogTypes和getLog已经在客户端命令中实现,但在站点 API 中没有它们的描述
Looks like it worth read source code instead of documentation
看起来值得阅读源代码而不是文档
Below jsdoc for this functions from source code :
下面来自源代码的这个函数的jsdoc:
/**
* Gets the available log types
*
* ```
* this.demoTest = function(client) {
* this.getLogTypes(function( typesArray ) {
*
* });
* };
* ```
*
* @method getLogTypes
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api commands
* @see logTypes
*/
and
和
/**
* Gets a log from selenium
*
* ```
* this.demoTest = function(client) {
* this.getLog( 'browser', function( logEntriesArray ) {
* console.log( "Log length: " + logEntriesArray.length );
* logEntriesArray.forEach( function( log ) {
* console.log( "[" + log.level + "] " + log.timestamp + " : " + log.message );
* } );
* });
* };
* ```
*
* @method getLog
* @param {string} typeString Log type to request
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api commands
* @see log
*/
回答by Liviu Trifoi
First make sure you set loggingPrefsto { 'browser' : 'ALL' } in your nightwatch configuration otherwise you won't see entries in the browser console that were written with console.log. My nightwatch.conf.js looks something like:
首先确保在你的守夜配置中将loggingPrefs设置为 { 'browser' : 'ALL' } 否则你将不会在浏览器控制台中看到使用 console.log 编写的条目。我的 nightwatch.conf.js 看起来像:
desiredCapabilities: {
browserName: 'chrome',
handlesAlerts: true,
loggingPrefs: { 'browser': 'ALL' }
}
Next, like the others have said you just have to use the built-in getLogfunction on the client. e.g.
接下来,就像其他人所说的那样,您只需要在客户端使用内置的getLog函数即可。例如
browser.getLog('browser', function(logEntriesArray) {
console.log('Log length: ' + logEntriesArray.length);
logEntriesArray.forEach(function(log) {
console.log('[' + log.level + '] ' + log.timestamp + ' : ' + log.message);
});
});