背景:我将Jasmine2用作我的量角器测试框架,并试图在该框架中使用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
下面是相应的文件供参考:
Scenario_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();
});
});
Login.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();
答案 0 :(得分:1)
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
中,并且调用起来更简单。该代码也更易于维护。
winston.log()
方法仅接受日志对象,而不接受字符串。
// logs "I am a log message."
winston.log({
level: 'info',
message: 'I am a log message.'
}
要记录纯字符串,可以使用logger.info()
。
工作日志示例。以下代码可在我的machinde上为我工作:
var winston = require('winston');
module.exports = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: 'TodaysLog.log'
})
]
});
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);
{"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!"}