背景:,我使用Jasmine2作为我的Protractor测试框架,并尝试在框架中实现一个记录器机制,使用winston进行更好的日志记录。
问题:测试失败,在cmd中出现以下非零错误,在脚本中包含之前它运行良好。你能帮我正确地实施它吗?
非零错误:
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();发布于 2018-09-13 20:51:53
问题
const logger = winston.createLogger...,您正在创建一个温斯顿引用,但它从未在任何地方传递。你只是把它储存在一个常数里。您必须使其成为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.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!"}发布于 2018-09-14 15:06:03
@sylvanBregy:下面是建议修改的文件,以供参考。
我可以亲眼目睹测试成功地完成登录,但不确定为什么控制台中会触发错误。
而且,logFile中也没有写任何东西:(
Scenario_01
describe('Scenario_01', function() {
console.log("Into Scenario1");
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')
Logging.info("foo"); //Neither this worked nor the below
Logging.info();
});
afterAll(function(){
common.closeBrowsers();
});
});Login.js
var Login = function() {
console.log("Into 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('C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\winston\\lib\\winston');
//const winston = require('winston');
module.exports = winston.createLogger({
level: 'info',
format: 'simple',
transports: [
new winston.transports.Console(),
new winston.transports.File({filename:'./logs/TodaysLog.log',level: 'info'})
],
});控制台日志
Report destination: target\e2e\screenshots\my-report.html
[20:22:58] I/launcher - Running 1 instances of WebDriver
[20:22:58] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub
Into Scenario1
Into Login Function
[20:23:03] I/launcher - 0 instance(s) of WebDriver still running
[20:23:03] I/launcher - chrome #01 failed 1 test(s)
[20:23:03] I/launcher - overall: 1 failed spec(s)
[20:23:03] E/launcher - Process exited with error code 1https://stackoverflow.com/questions/52298621
复制相似问题