首页 > 解决方案 > 在量角器中使用 Winston 记录器实现测试失败

问题描述

背景:我使用 Jasmine2 作为 Protractor 的测试框架,并尝试使用 winston 包在框架中实现记录器机制以实现更好的记录。

问题:测试失败,cmd 中出现以下非零错误,在脚本中包含 winston 之前该错误运行良好。您能否帮助我正确实施它。

非零错误:

Report destination:   target\e2e\screenshots\my-report.html
[20:28:52] I/launcher - Running 1 instances of WebDriver
[20:28:52] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

[20:28:56] I/launcher - 0 instance(s) of WebDriver still running
[20:28:56] I/launcher - chrome #01 failed 1 test(s)
[20:28:56] I/launcher - overall: 1 failed spec(s)
[20:28:56] E/launcher - Process exited with error code 1

以下是相应的文件供参考:

场景_01.js:

describe('Scenario_01', function() {

 var Logging = require('./scripts/LoggingMech.js');
 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 


 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd')
          .catch(error => Logging.Logger.log(error));
     });

afterAll(function(){
    common.closeBrowsers();
});
});

登录.js:

var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();

LoggingMech.js

const winston = require('winston');
var Logging = function() {

     this.Logger = function(){
      const logger = winston.createLogger({

           level: 'info',
           format: format.simple(),
            transports: [
                new winston.transports.Console(),
                new winston.transports.File({ filename: 'TodaysLog.log' })
                        ]
});

};
}
module.exports = new Logging();

标签: jasmineprotractorwinstonjasmine2.0

解决方案


问题

const logger = winston.createLogger...您正在创建一个 winston 引用,但它从未在任何地方传递。你只是将它存储在一个常量中。您必须使其成为属性的Logger属性,然后创建一个新实例Logging.Logger并调用log()它。

使固定

您必须使其成为 Logger 属性的属性。

LoggingMech.js你做this.logger = winston.createLogger而不是const logger = winston.createLogger

然后创建一个新的 Logging.Logger 实例并在其上调用 log()。

var Logging = require('./scripts/LoggingMech.js');
// logs foo
(new Logging.Logger()).log('foo');

但我认为你正在根据需要做更复杂的事情。为了实现记录器,您只需将LoggingMech.js文件更改为以下内容:

const winston = require('winston');
module.exports = winston.createLogger({
    level: 'info',
    format: format.simple(),
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({
            filename: 'TodaysLog.log'
        })
    ]
});

并称它为:

var Logging = require('./scripts/LoggingMech.js');
Logging.log('foo');

记录器的配置仍然封装在LoggingMech.js其中,并且调用起来更简单。代码也更容易维护。

更新 1:使用 winston 记录

winston.log()方法只接受一个日志对象而不是一个字符串。

// logs "I am a log message."
winston.log({
  level: 'info',
  message: 'I am a log message.'
}

要记录纯字符串,您可以使用logger.info().

更新 2:完整的 winston 日志记录示例

一个工作日志示例。以下代码适用于我的机器:

温斯顿.js

var winston = require('winston');
module.exports = winston.createLogger({
    level: 'info',
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({
            filename: 'TodaysLog.log'
        })
    ]
});

index.js

var Logging = require('./winston');
var message = {
    level: 'info',
    message: 'Hello distributed log files!'
  };

  Logging.log(message);
  Logging.info('sdf');
  Logging.error(message);
  Logging.warn(message);

TodaysLog.log 中记录的内容

{"level":"info","message":"Hello distributed log files!"}
{"message":"sdf","level":"info"}
{"level":"error","message":"Hello distributed log files!"}
{"level":"warn","message":"Hello distributed log files!"}

推荐阅读