jasmine - 在量角器中使用 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();
解决方案
问题
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!"}
推荐阅读
- go - 纹理如何在OpenGL3中显示为颜色而不是灰度?
- javascript - 当我尝试创建一个供事件侦听器读取的元素时,控制台抛出未定义
- visual-studio - Visual Studio 2019 社区版 16.7.0 挂起
- python - 如何在python tkinter中向复选框添加垂直滚动条?
- php - Laravel 查询生成器仅获取具有特定进度的项目
- java - 如何根据camunda中的processInstanceId获取taskId
- java - 从java发送重定向到Angular 8
- visual-studio - 在 Visual Studio 2019 上,如何在 C++ Debug 程序中调试纯 C++ 和 CLI C++?
- python - 从字典列表中添加新列
- r - 从 R Shiny 下载 matplotlib 绘图