我正在使用 log4js 将测试结果记录到文件中。我有四个测试文件,每个文件都会创建自己的日志文件。所以这应该是总共 4 个日志文件,但我总共得到 6 个日志文件。不知何故,它只将前两行记录到日志文件中,但其余行被复制到新的日志文件中。有人可以帮忙谢谢吗?
文件:conf.js
exports.config = {
directConnect: true,
//capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
chromeOptions: {
useAutomationExtension: false
},
},
//framework to use. Jasmine is recommended.
framework: 'jasmine',
specs: [
'../tests/testGoogle.js',
'../tests/testCalculator.js'
],
//options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
}
文件:log4js.json
{
"appenders": {
"test": {
"type": "multiFile",
"base": "testOutput/",
"property": "categoryName",
"flags": "w",
"pattern": "yyyy-MM-dd-hh-mm-ss",
"compress": false,
"alwaysIncludePattern": true,
"extension": ".log",
"keepFileExt": true
}
},
"categories": {
"default": {
"appenders": ["test"],
"level": "info"
}
}
}
文件:log4jsConfig.js
const log4jsLogger = require('log4js');
log4jsLogger.configure("./helpers/logger/log4js.json");
let getLoggerHelper = function(){
this.createLogFile = function(filename){
return log4jsLogger.getLogger(filename);
}
}
module.exports = new getLoggerHelper();
文件:testCalculator.js
let calculator = require("../pages/superCal/calculator");
const log4jsConfig = require("../helpers/logger/log4jsConfig.js");
describe("Test Suite- Calculator", function(){
var testLogger = log4jsConfig.createLogFile("logTestCalculator");
testLogger.info("logTestCalculator is created");
testLogger.info("testCalculator.js, describe() log to logTestCalculator");
it("addition test", function(){
testLogger.info("testCalculator.js, describe(), it(), launch URL");
calculator.get("http://juliemr.github.io/protractor-demo/");
testLogger.info("testCalculator.js, describe(), it(), sending first value 2");
calculator.enterFirstNumber('2');
browser.sleep(2000);
testLogger.info("testCalculator.js, describe(), it(), sending second value 3");
calculator.enterSecondNumber('3');
testLogger.info("testCalculator.js, describe(), it(), sending operand MULTIPLICATION");
calculator.enterOperator("MULTIPLICATION");
browser.sleep(2000);
testLogger.info("testCalculator.js, describe(), it(), sending click on Go! button");
calculator.clickingGo();
testLogger.info("testCalculator.js, describe(), it(), verify result");
expect("6").toEqual(calculator.getResult("6").getText());
browser.sleep(2000);
});
});
文件:calculator.js
const log4jsConfig = require("../../helpers/logger/log4jsConfig.js");
let homepage = function(){
var testLogger = log4jsConfig.createLogFile("logCalculator");
testLogger.info("logCalculator is created");
testLogger.info("calculator.js, describe() log to logCalculator");
this.myConsoleLog = function(){
console.log("at myConsoleLog()");
};
this.get = function(url){
testLogger.info("calculator.js, get");
browser.get(url);
browser.sleep(3000);
};
this.enterFirstNumber = function(value1){
testLogger.info("calculator.js, enterFirstNumber");
element(by.model("first")).sendKeys(value1);
};
this.enterSecondNumber = function(value2){
testLogger.info("calculator.js, enterSecondNumber");
element(by.model("second")).sendKeys(value2);
};
this.enterOperator = function(oper){
testLogger.info("calculator.js, enterOperator");
element(by.model("operator")).$('[value='+ oper).click();
};
this.clickingGo = function(){
testLogger.info("calculator.js, clickingGo");
element(by.css('[ng-click="doAddition()"]')).click();
};
this.getResult = function(result){
testLogger.info("calculator.js, getResult");
var retVal = element(by.cssContainingText(".ng-binding", result));
return retVal;
};
};
module.exports = new homepage();
文件:testGoogle.js
let googleSite = require("../pages/googleSite/google");
const log4jsConfig = require("../helpers/logger/log4jsConfig.js");
describe("Test Suite- Google", function(){
var testLogger = log4jsConfig.createLogFile("logTestGoogle");
testLogger.info("logTestGoogle is created");
testLogger.info("testGoogle.js, describe() log to logTestGoogle");
it("Launch Google site", function(){
testLogger.info("testGoogle.js, describe(), launch URL");
googleSite.get("http://www.google.com");
expect("Google").toEqual(googleSite.getGoogleText("Google"));
browser.sleep(2000);
});
});
文件:google.js
const log4jsConfig = require("../../helpers/logger/log4jsConfig.js");
let google = function(){
var testLogger = log4jsConfig.createLogFile("logGoogle");
testLogger.info("logGoogle is created");
testLogger.info("google.js, describe() log to logGoogle");
this.get = function(url){
testLogger.info("google.js, get");
browser.driver.get(url);
browser.sleep(3000);
};
this.getGoogleText = function(text){
testLogger.info("google.js, getGoogleText");
return text;
browser.sleep(3000);
};
};
module.exports = new google();
以及输出日志文件:
例如,这两个被拆分的日志文件:
“logCalculator.2020-07-29-12-34-19.log”
“logCalculator.2020-07-29-12-34-25.log”
此日志文件“logCalculator.2020-07-29-12-34-19.log”具有:
[2020-07-29T12:34:19.687] [INFO] logCalculator - logCalculator is created
[2020-07-29T12:34:19.688] [INFO] logCalculator - calculator.js, describe() log to logCalculator
此日志文件“logCalculator.2020-07-29-12-34-25.log”具有:
[2020-07-29T12:34:25.843] [INFO] logCalculator - calculator.js, get
[2020-07-29T12:34:25.846] [INFO] logCalculator - calculator.js, enterFirstNumber
[2020-07-29T12:34:25.848] [INFO] logCalculator - calculator.js, enterSecondNumber
[2020-07-29T12:34:25.849] [INFO] logCalculator - calculator.js, enterOperator
[2020-07-29T12:34:25.850] [INFO] logCalculator - calculator.js, clickingGo
[2020-07-29T12:34:25.850] [INFO] logCalculator - calculator.js, getResult