问题
I'm aware that if you were to do something like protractor config.js > file.log
all the console output would be written to file.log.
Is there a way to do this/access that output from within the tests though so that I can use a dynamic path that's created with my reporting tool? And also in a way that I don't lose the console output.
Edits:
To elaborate a little further, I'm not only interested in console.log
output. I'm interested in everything that comes from what seems to be protractors testLogger
. For example, at the end of a suite's execution I am presented with:
[13:19:37] I/launcher - 0 instance(s) of WebDriver still running
[13:19:37] I/launcher - internet explorer #01-0 failed 4 test(s)
[13:19:37] I/launcher - internet explorer #01-1 failed 1 test(s)
[13:19:37] I/launcher - internet explorer #01-2 failed 1 test(s)
[13:19:37] I/launcher - internet explorer #01-3 failed 1 test(s)
[13:19:37] I/launcher - overall: 7 failed spec(s)
I would like that in addition to the other console output.
回答1:
What I would suggest is instead of writing any required information to console and trying to make your reporter read it, I would suggest you to implement logging from your tests to get better control.
Instead of a console.log()
- have something like logger().info("Testing Log4js")
and then make your reporter operate on it.
If you are further interested there is js version of log4j
called - log4js that I tried with Protractor and its works fine.
In case you are further interested here is the link to implementation examples
回答2:
Complete code example for reference:
Steps to setup log4js for Protractor framework
Step 1: Install log4js npm module
Step 2: Create a helper js file – which will create the logger object which can be imported to tests
'use strict';
var log4js = require('log4js');
var log4jsGen = {
getLogger: function getLogger() {
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('./logs/ExecutionLog.log'), 'logs');
var logger = log4js.getLogger('logs');
return logger;
}
};
module.exports = log4jsGen;
Step 3: Import the helper file and call the getlogger() method to add logging statements to your tests
var log4jsGen = require("../Utilities/log4jsGen");
log4jsGen.getLogger().info("Testing Log4js");
来源:https://stackoverflow.com/questions/41700764/writing-a-log-file-for-protractor-jasmine-tests-without-using-command-line