1

I'm using log4js to log the test results to files. I have four test files and each file will create its own log file. So that should be 4 log files total, but I'm getting 6 log files total. Somehow it only logs the first two lines into the log file, but the rest of the lines are copied to new log files. Can someone help thanks?

file: 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
    }
}

file: 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"
        }
    }
}

file: 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();

file: 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);
  });
});

file: 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();

file: 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);
    });
});

file: 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();

And the output log files:

enter image description here

For an example, these two log files that got split:

"logCalculator.2020-07-29-12-34-19.log"

"logCalculator.2020-07-29-12-34-25.log"

this log file "logCalculator.2020-07-29-12-34-19.log", has:

[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

this log file "logCalculator.2020-07-29-12-34-25.log", has:

[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
Phil
  • 339
  • 2
  • 13
  • Most likely unrelated and maybe I'm misunderstanding how it work, but I don't see the `browser.sleep` anywhere in the log4js output. I would expect to see something when set to 3000 ms, not a 3 ms difference. – Fullslack Jul 29 '20 at 21:07
  • That one, I'm not quite sure either. Does "browser.sleep" get logged onto the log? sorry, I'm still learning log4js, javascript and protractor. – Phil Jul 29 '20 at 21:59
  • Searching on Google isn't helpful either I found out. It seems more people have the same issue(s) with `browser.sleep` and `browser.wait` (even unclear if there is any difference). One thing I found is this: https://stackoverflow.com/a/56147706/13211030. Maybe it works, maybe it won't (I hope it does). – Fullslack Jul 30 '20 at 07:55
  • Wow that's nice of you to search on google to help me on this issue. Very appreciate that – Phil Jul 31 '20 at 00:52
  • however the error that I was told, it's related to the pattern configuration that I use in log4js.json. I will need to remove that property. – Phil Jul 31 '20 at 00:53
  • Strange thing is the inconsistency of the fault. Why only the calculator and not also the google files? You expect a error in the config/json file to appear on all logs generated, not 2/3 of them. – Fullslack Jul 31 '20 at 06:56
  • sorry, the error does appear in google file too but I just gave an example for the calculator already. – Phil Jul 31 '20 at 20:07

0 Answers0