Saving console log when test fail

398 views
Skip to first unread message

Jean

unread,
Nov 5, 2015, 3:09:56 PM11/5/15
to NightwatchJs
Is it possible to save the console.log output of the browser when the tests fails?

I did find this api: http://nightwatchjs.org/api#getLog , wich indicate how to retrieve the logs of the browser. But how to get them only when the test is failing? And to get that logs just before the browser quit?

And how to save that log with the name of the failed test?

Thanks,
Jean

Rahul Kavalapara

unread,
Nov 6, 2015, 12:21:32 AM11/6/15
to NightwatchJs
Jean,

Something like below should do the job. I believe currentTest object has the details of no of test cases which have passed, failed etc. currentTest object refer's to a test suite i.e a file containing different steps.You can add an if condition just for failing scenarios. 
You need to add loggingPrefs property within your configuration as well for this to work. 

Hope this helps. 

       var executingTestCaseName = browser.currentTest.module.substring(browser.currentTest.module.lastIndexOf("\/") + 1, browser.currentTest.module.length);

var seleniumLogPath = folderlogPath + "\/" + executingTestCaseName + "-selenium-driver.log";
var browserLogPath = folderlogPath + "\/" + executingTestCaseName + "-browser.log";

var log4js = require("log4js");
log4js.configure({
   appenders: [
        { type: 'file', filename: seleniumLogPath, category: 'seleniumLogger', absolute: true, "layout": { "type": "messagePassThrough" } },
        { type: 'file', filename: browserLogPath, category: 'browserLogger', absolute: true, "layout": { "type": "messagePassThrough" } }
]
});

var seleniumLogger = log4js.getLogger("seleniumLogger");
browser.getLog('driver', function (logEntriesArray) {
console.log("LogEntriesArray.Length: " + logEntriesArray.length);
logEntriesArray.forEach(function (log) {
        seleniumLogger.info(new Date(log.timestamp).toISOString() + '[' + log.level + '] ' + ' : ' + log.message);
});
});

var browserLogger = log4js.getLogger("browserLogger");
browser.getLog('browser', function (logEntriesArray) {
console.log("LogEntriesArray.Length: " + logEntriesArray.length);
logEntriesArray.forEach(function (log) {
        browserLogger.info(new Date(log.timestamp).toISOString() + '[' + log.level + '] ' + ' : ' + log.message);
});
});

browser.end();

Jean

unread,
Nov 6, 2015, 3:19:54 PM11/6/15
to NightwatchJs
Thank you very much for your proposition...

But where do you place that code?

I tried to put it as a custom reporter, but it run too late (the browser session is ended since a long time), in afterEach, but it seems that the session is closed too, or at least, I don't see my logs in it (console.log made in by application in the navigated page).

Any clue?
Jean

Rahul Kavalapara

unread,
Nov 6, 2015, 4:13:11 PM11/6/15
to NightwatchJs
Jean,

Please take a look at this sample project. globalsModule.js  https://github.com/rkavalap/NightWatchTest
You might also want to take a look issue #704 which I recently updated few hours ago.
You should definitely have the log if your test has passed. 

Rahul

Jean

unread,
Nov 7, 2015, 7:16:25 AM11/7/15
to NightwatchJs
Hello

I tried to make your example work locally... There is a problem, the forEach function is unknown. So I tried different things.

To make my tests, I changed the test1 with one test and one line:
            .assert.title("Google")
            .execute(function() { console.log("test console")} )

Whatever I do, I don't see the "test console" in my bash output...

Any help?

PS: I took a look at your bug, and I think it will be blocking for my problem :-(

Jean

Rahul Kavalapara

unread,
Nov 8, 2015, 12:40:59 AM11/8/15
to NightwatchJs
Jean,

If I am not mistaken, the console.log is within the execute function and hence should appear within the browser's console and not the bash console. So if your test has executed successfully until the global afterEach hook, your "test console" statement should appear within the browser.log file as per my previous code snippet and not the bash console. 
.assert.title("Google") should have printed something on your bash console.

Rahul

Marie-Line et Jean Honlet

unread,
Nov 8, 2015, 6:08:39 AM11/8/15
to nightw...@googlegroups.com
hello

indeed:
- the assert.title show something in the bash console.
- the console.log "test console" show in the browser console (I mean in the opened window of firefox, I see the message in the debugger console)

but
- the "test console" does not show up in the bash window

By the way, I seems to me that our configs are different, since your code does not work at my side. "forEeach" cause an error in my bash console. I replaced it with "for ... in ... "

Thanks for your help
Jean


--
You received this message because you are subscribed to a topic in the Google Groups "NightwatchJs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nightwatchjs/he7EmAqjjU8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nightwatchjs...@googlegroups.com.
To post to this group, send email to nightw...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/0b1601be-9ace-41dc-9a61-fdba90c7a6e3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marie-Line et Jean
Reply all
Reply to author
Forward
0 new messages