How to create Selenium Cucumber html reports with Node JS - node.js

I want to create cucumber html reports and I am new to Node JS and I tried searching for it and I used the following
this.registerHandler('AfterFeatures', function(callback) {
try {
var options = {
theme: "bootstrap",
jsonFile: "/report/cucumber.json",
output: "/report/cucumber_report.html",
reportSuiteAsScenarios: true,
launchReport: true,
metadata: {
"App Version": "0.0.1"
}
};
reporter.generate(options);
} catch (e) {
console.log(e);
}
callback();
});
But when I run my code, The cucumber feature scenarios gets executed and it finally gives me an error stating,
Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json'
at Object.fs.openSync (fs.js:652:18)
at Object.fs.readFileSync (fs.js:553:33)
at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22)
at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48)
at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9)
at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21)
at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/report/cucumber.json' }
Do the above code automatically generates .json and .html file or we need to manually create a .json file and converts that into a html report.
I have worked on Java and it automatically creates the json and html reports at the end the execution.
As this is very new I am not able to figure out whats the missing part
Thanks

Your code to generate HTML report will expect the json file: /report/cucumber.json had been exist.
So you need other code to help to generate the json file during test running, I will give code used in my project for your reference.
Note: below code can only work on Cucumber 1, can't work on Cucumver 2, below is the version I used:
"dependencies": {
"cucumber": "1.2.1",
"cucumber-html-reporter": "0.2.6",
1) cucumber-json-report.js to generate Cucumber JSON report during running.
var fs = require('fs-extra');
var path = require('path');
var moment = require('moment');
var Cucumber = require('cucumber');
module.exports = function() {
var JsonFormatter = Cucumber.Listener.JsonFormatter();
JsonFormatter.log = function(string) {
var outputDir = './reports';
var targetJson = outputDir + '/cucumber_report.json';
if (fs.existsSync(outputDir)) {
fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), {
overwrite: true
});
}
fs.mkdirSync(outputDir);
fs.writeFileSync(targetJson, string);
};
this.registerListener(JsonFormatter);
};
2) screenshot.js to take screenshot when failure
module.exports = function() {
this.After(function(scenario, callback) {
if (scenario.isFailed()) {
browser.takeScreenshot().then(function(buffer) {
var decodedImage = new Buffer(buffer, 'base64');
scenario.attach(decodedImage, 'image/png');
callback();
}, function(err) {
callback(err);
});
} else {
callback();
}
});
};
3) cucumber-html-report.js to generate Cucumber HTML report after all features running end.
var reporter = require('cucumber-html-reporter');
module.exports = function() {
this.AfterFeatures(function(features, callback) {
var options = {
theme: 'bootstrap',
jsonFile: 'reports/cucumber_report.json',
output: 'reports/cucumber_report.html',
reportSuiteAsScenarios: true
};
reporter.generate(options);
callback();
});
};
4) Protractor conf.js to include above three files in cucumberOpts.require
cucumberOpts: {
monochrome: true,
strict: true,
plugin: ["pretty"],
require:[
'./step_definitions/*step.js',
'./support/screenshot.js',
'./support/cucumber-json-report.js',
'./support/cucumber-html-report.js'
],
tags: '',
},

Related

How do I output webpack as a string using Node

I am trying to use Webpack to bundle a bunch of files. I have the following in my node code...
webpack({
entry: "./src/test",
output: {
path: __dirname,
filename: "bundle.js"
},
}, function(err, stats){
console.log("I would like to output the created js here");
})
This works fine creating a file called bundle.js but I can't figure out how to output as a string instead.
Basically what you can do is to read the file, and then work with it as you want.
e.g.
import webpack from 'webpack';
const config = require('../webpack.config');
const compiler = webpack(config);
compiler.run((err, stats) => {
const data = stats.toJson();
const app = data.assetsByChunkName.app[0] //here you can get the file name
// if you don't have chunks then you should use data.assets;
const file = fs.readFileSync('path to your output ' + app); //read the file
//now you can work with the file as you want.
});
//Basic webpack.config.js
module.exports = {
devtool: 'source-map',
entry: {
app: 'Some path' // you can have different entries.
entrie2 : ''
.... more entries
},
output: {
path: 'Some path'
}
}
Hope this help.

running e2e testing with aurelia cli

I'm trying to implement a few e2e tests in my aurelia-cli app. I've tried looking for docs or blogs but haven't found anything on e2e setup for the cli. I've made the following adjustments to the project.
first I added this to aurelia.json
"e2eTestRunner": {
"id": "protractor",
"displayName": "Protractor",
"source": "test/e2e/src/**/*.ts",
"dist": "test/e2e/dist/",
"typingsSource": [
"typings/**/*.d.ts",
"custom_typings/**/*.d.ts"
]
},
Also added the e2e tasks on aurelia_project/tasks:
e2e.ts
import * as project from '../aurelia.json';
import * as gulp from 'gulp';
import * as del from 'del';
import * as typescript from 'gulp-typescript';
import * as tsConfig from '../../tsconfig.json';
import {CLIOptions} from 'aurelia-cli';
import { webdriver_update, protractor } from 'gulp-protractor';
function clean() {
return del(project.e2eTestRunner.dist + '*');
}
function build() {
var typescriptCompiler = typescriptCompiler || null;
if ( !typescriptCompiler ) {
delete tsConfig.compilerOptions.lib;
typescriptCompiler = typescript.createProject(Object.assign({}, tsConfig.compilerOptions, {
// Add any special overrides for the compiler here
module: 'commonjs'
}));
}
return gulp.src(project.e2eTestRunner.typingsSource.concat(project.e2eTestRunner.source))
.pipe(typescript(typescriptCompiler))
.pipe(gulp.dest(project.e2eTestRunner.dist));
}
// runs build-e2e task
// then runs end to end tasks
// using Protractor: http://angular.github.io/protractor/
function e2e() {
return gulp.src(project.e2eTestRunner.dist + '**/*.js')
.pipe(protractor({
configFile: 'protractor.conf.js',
args: ['--baseUrl', 'http://127.0.0.1:9000']
}))
.on('end', function() { process.exit(); })
.on('error', function(e) { throw e; });
}
export default gulp.series(
webdriver_update,
clean,
build,
e2e
);
and the e2e.json
{
"name": "e2e",
"description": "Runs all e2e tests and reports the results.",
"flags": []
}
I've added a protractor.conf file and aurelia.protractor to the root of my project
protractor.conf.js
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
//seleniumAddress: 'http://0.0.0.0:4444',
specs: ['test/e2e/dist/*.js'],
plugins: [{
path: 'aurelia.protractor.js'
}],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
aurelia.protractor.js
/* Aurelia Protractor Plugin */
function addValueBindLocator() {
by.addLocator('valueBind', function (bindingModel, opt_parentElement) {
var using = opt_parentElement || document;
var matches = using.querySelectorAll('*[value\\.bind="' + bindingModel +'"]');
var result;
if (matches.length === 0) {
result = null;
} else if (matches.length === 1) {
result = matches[0];
} else {
result = matches;
}
return result;
});
}
function loadAndWaitForAureliaPage(pageUrl) {
browser.get(pageUrl);
return browser.executeAsyncScript(
'var cb = arguments[arguments.length - 1];' +
'document.addEventListener("aurelia-composed", function (e) {' +
' cb("Aurelia App composed")' +
'}, false);'
).then(function(result){
console.log(result);
return result;
});
}
function waitForRouterComplete() {
return browser.executeAsyncScript(
'var cb = arguments[arguments.length - 1];' +
'document.querySelector("[aurelia-app]")' +
'.aurelia.subscribeOnce("router:navigation:complete", function() {' +
' cb(true)' +
'});'
).then(function(result){
return result;
});
}
/* Plugin hooks */
exports.setup = function(config) {
// Ignore the default Angular synchronization helpers
browser.ignoreSynchronization = true;
// add the aurelia specific valueBind locator
addValueBindLocator();
// attach a new way to browser.get a page and wait for Aurelia to complete loading
browser.loadAndWaitForAureliaPage = loadAndWaitForAureliaPage;
// wait for router navigations to complete
browser.waitForRouterComplete = waitForRouterComplete;
};
exports.teardown = function(config) {};
exports.postResults = function(config) {};
and I added a sample test in my test/e2e/src folder it doesn't get executed. I've also tried implementing a e2e test within the unit test folder since when I run au test I see that a chrome browser opens up.
describe('aurelia homepage', function() {
it('should load page', function() {
browser.get('http://www.aurelia.io');
expect(browser.getTitle()).toEqual('Home | Aurelia');
});
});
But this throws the error browser is undefined. Am I missing something with e2e testing with the cli? I know aurelia-protractor comes pre-installed but I don't see any way to run it.
I know this is a very late answer, but perhaps for others looking for an answer, you could try to import from the aurelia-protractor plugin
import {browser} from 'aurelia-protractor-plugin/protractor';

Gulp Images Failing in Ubuntu 16

I'm running my own Ubuntu 16.04.1 droplet and have just installed 'nodejs', npm, and bower globally. Then installed npm and bower on the Sage theme I've been working with. I'm able to gulp almost everything but once I get to images, I'm thrown with an error I've been having trouble diagnosing. Any help is always much appreciated!
root#myserver-ubuntu:/var/www/html/mydomain.com/public_html/bedrock/web/app/themes/mytheme# gulp images
[00:46:45] Using gulpfile /var/www/html/mydomain.com/public_html/bedrock/web/app/themes/mytheme/gulpfile.js
[00:46:45] Starting 'images'...
events.js:141
throw er; // Unhandled 'error' event
^
Error: Command failed: /var/www/html/mydomain.com/public_html/bedrock/web/app/themes/mytheme/node_modules/optipng-bin/vendor/optipng -strip all -clobber -force -fix -o 2 -out /tmp/2fd7e3c1-977c-4ac4-9886-a84ecd44e9e8 /tmp/49ebf02e-f383-46e2-bc40-d6b03b441380
/var/www/html/mydomain.com/public_html/bedrock/web/app/themes/mytheme/node_modules/optipng-bin/vendor/optipng: 1: /var/www/html/mydomain.com/public_html/bedrock/web/app/themes/mytheme/node_modules/optipng-bin/vendor/optipng: Syntax error: "(" unexpected
at ChildProcess.exithandler (child_process.js:213:12)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:821:16)
at Socket.<anonymous> (internal/child_process.js:319:11)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Pipe._onclose (net.js:469:12)
This is the gulpfile.js -
// ## Globals
var argv = require('minimist')(process.argv.slice(2));
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var flatten = require('gulp-flatten');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var lazypipe = require('lazypipe');
var less = require('gulp-less');
var merge = require('merge-stream');
var cssNano = require('gulp-cssnano');
var plumber = require('gulp-plumber');
var rev = require('gulp-rev');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
// See https://github.com/austinpray/asset-builder
var manifest = require('asset-builder')('./assets/manifest.json');
// `path` - Paths to base asset directories. With trailing slashes.
// - `path.source` - Path to the source files. Default: `assets/`
// - `path.dist` - Path to the build directory. Default: `dist/`
var path = manifest.paths;
// `config` - Store arbitrary configuration values here.
var config = manifest.config || {};
// `globs` - These ultimately end up in their respective `gulp.src`.
// - `globs.js` - Array of asset-builder JS dependency objects. Example:
// ```
// {type: 'js', name: 'main.js', globs: []}
// ```
// - `globs.css` - Array of asset-builder CSS dependency objects. Example:
// ```
// {type: 'css', name: 'main.css', globs: []}
// ```
// - `globs.fonts` - Array of font path globs.
// - `globs.images` - Array of image path globs.
// - `globs.bower` - Array of all the main Bower files.
var globs = manifest.globs;
// `project` - paths to first-party assets.
// - `project.js` - Array of first-party JS assets.
// - `project.css` - Array of first-party CSS assets.
var project = manifest.getProjectGlobs();
// CLI options
var enabled = {
// Enable static asset revisioning when `--production`
rev: argv.production,
// Disable source maps when `--production`
maps: !argv.production,
// Fail styles task on error when `--production`
failStyleTask: argv.production,
// Fail due to JSHint warnings only when `--production`
failJSHint: argv.production,
// Strip debug statments from javascript when `--production`
stripJSDebug: argv.production
};
// Path to the compiled assets manifest in the dist directory
var revManifest = path.dist + 'assets.json';
// ## Reusable Pipelines
// See https://github.com/OverZealous/lazypipe
// ### CSS processing pipeline
// Example
// ```
// gulp.src(cssFiles)
// .pipe(cssTasks('main.css')
// .pipe(gulp.dest(path.dist + 'styles'))
// ```
var cssTasks = function(filename) {
return lazypipe()
.pipe(function() {
return gulpif(!enabled.failStyleTask, plumber());
})
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.init());
})
.pipe(function() {
return gulpif('*.less', less());
})
.pipe(function() {
return gulpif('*.scss', sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
errLogToConsole: !enabled.failStyleTask
}));
})
.pipe(concat, filename)
.pipe(autoprefixer, {
browsers: [
'last 2 versions',
'android 4',
'opera 12'
]
})
.pipe(cssNano, {
safe: true
})
.pipe(function() {
return gulpif(enabled.rev, rev());
})
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.write('.', {
sourceRoot: 'assets/styles/'
}));
})();
};
// ### JS processing pipeline
// Example
// ```
// gulp.src(jsFiles)
// .pipe(jsTasks('main.js')
// .pipe(gulp.dest(path.dist + 'scripts'))
// ```
var jsTasks = function(filename) {
return lazypipe()
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.init());
})
.pipe(concat, filename)
.pipe(uglify, {
compress: {
'drop_debugger': enabled.stripJSDebug
}
})
.pipe(function() {
return gulpif(enabled.rev, rev());
})
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.write('.', {
sourceRoot: 'assets/scripts/'
}));
})();
};
// ### Write to rev manifest
// If there are any revved files then write them to the rev manifest.
// See https://github.com/sindresorhus/gulp-rev
var writeToManifest = function(directory) {
return lazypipe()
.pipe(gulp.dest, path.dist + directory)
.pipe(browserSync.stream, {match: '**/*.{js,css}'})
.pipe(rev.manifest, revManifest, {
base: path.dist,
merge: true
})
.pipe(gulp.dest, path.dist)();
};
// ## Gulp tasks
// Run `gulp -T` for a task summary
// ### Styles
// `gulp styles` - Compiles, combines, and optimizes Bower CSS and project CSS.
// By default this task will only log a warning if a precompiler error is
// raised. If the `--production` flag is set: this task will fail outright.
gulp.task('styles', ['wiredep'], function() {
var merged = merge();
manifest.forEachDependency('css', function(dep) {
var cssTasksInstance = cssTasks(dep.name);
if (!enabled.failStyleTask) {
cssTasksInstance.on('error', function(err) {
console.error(err.message);
this.emit('end');
});
}
merged.add(gulp.src(dep.globs, {base: 'styles'})
.pipe(cssTasksInstance));
});
return merged
.pipe(writeToManifest('styles'));
});
// ### Scripts
// `gulp scripts` - Runs JSHint then compiles, combines, and optimizes Bower JS
// and project JS.
gulp.task('scripts', ['jshint'], function() {
var merged = merge();
manifest.forEachDependency('js', function(dep) {
merged.add(
gulp.src(dep.globs, {base: 'scripts'})
.pipe(jsTasks(dep.name))
);
});
return merged
.pipe(writeToManifest('scripts'));
});
// ### Fonts
// `gulp fonts` - Grabs all the fonts and outputs them in a flattened directory
// structure. See: https://github.com/armed/gulp-flatten
gulp.task('fonts', function() {
return gulp.src(globs.fonts)
.pipe(flatten())
.pipe(gulp.dest(path.dist + 'fonts'))
.pipe(browserSync.stream());
});
// ### Images
// `gulp images` - Run lossless compression on all the images.
gulp.task('images', function() {
return gulp.src(globs.images)
.pipe(imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{removeUnknownsAndDefaults: false}, {cleanupIDs: false}]
}))
.pipe(gulp.dest(path.dist + 'images'))
.pipe(browserSync.stream());
});
// ### JSHint
// `gulp jshint` - Lints configuration JSON and project JS.
gulp.task('jshint', function() {
return gulp.src([
'bower.json', 'gulpfile.js'
].concat(project.js))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
});
// ### Clean
// `gulp clean` - Deletes the build folder entirely.
gulp.task('clean', require('del').bind(null, [path.dist]));
// ### Watch
// `gulp watch` - Use BrowserSync to proxy your dev server and synchronize code
// changes across devices. Specify the hostname of your dev server at
// `manifest.config.devUrl`. When a modification is made to an asset, run the
// build step for that asset and inject the changes into the page.
// See: http://www.browsersync.io
gulp.task('watch', function() {
browserSync.init({
files: ['{lib,templates}/**/*.php', '*.php'],
proxy: config.devUrl,
snippetOptions: {
whitelist: ['/wp-admin/admin-ajax.php'],
blacklist: ['/wp-admin/**']
}
});
gulp.watch([path.source + 'styles/**/*'], ['styles']);
gulp.watch([path.source + 'scripts/**/*'], ['jshint', 'scripts']);
gulp.watch([path.source + 'fonts/**/*'], ['fonts']);
gulp.watch([path.source + 'images/**/*'], ['images']);
gulp.watch(['bower.json', 'assets/manifest.json'], ['build']);
});
// ### Build
// `gulp build` - Run all the build tasks but don't clean up beforehand.
// Generally you should be running `gulp` instead of `gulp build`.
gulp.task('build', function(callback) {
runSequence('styles',
'scripts',
['fonts', 'images'],
callback);
});
// ### Wiredep
// `gulp wiredep` - Automatically inject Less and Sass Bower dependencies. See
// https://github.com/taptapship/wiredep
gulp.task('wiredep', function() {
var wiredep = require('wiredep').stream;
return gulp.src(project.css)
.pipe(wiredep())
.pipe(changed(path.source + 'styles', {
hasChanged: changed.compareSha1Digest
}))
.pipe(gulp.dest(path.source + 'styles'));
});
// ### Gulp
// `gulp` - Run a complete build. To compile for production run `gulp --production`.
gulp.task('default', ['clean'], function() {
gulp.start('build');
});
I will update this for anybody that may fall here in the future.
I couldn't locate the exact reason why gulp failed, my guess is that it's a spacing/tab bug. I deleted node_modules from the sage theme and reran npm install, and the error corrected itself.

Protractor - Exception of Cannot read property 'headerPrinter' of undefined appears after every run

I really don't know what is the reason for this exception and what are the side effects of it.
But It appears every time even all test cases are passed.
Please help to figure out how to solve this -
[launcher] Error: TypeError: Cannot read property 'headerPrinter' of undefined
at printHeader (C:\automation\tests\node_modules\protractor-console\dist\protractor-console.js:81:8)
at C:\automation\tests\node_modules\protractor-console\dist\protractor-console.js:56:19
at [object Object].promise.ControlFlow.runInFrame_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
at [object Object].promise.Callback_.goog.defineClass.notify (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2464:25)
at [object Object].promise.Promise.notify_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:563:12)
at Array.forEach (native)
at Object.goog.array.forEach (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\array\array.js:203:43)
at [object Object].promise.Promise.notifyAll_ (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:552:16)
at goog.async.run.processWorkQueue (C:\automation\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
at runMicrotasksCallback (node.js:337:7)
[launcher] Process exited with error code 100
Protractor conf:
//-=-=-=- Packages -=-=-=-
var path = require('path');
var fs = require('fs');
var HtmlReporter = require('protractor-html-screenshot-reporter');
var util = require('util');
var Imap = require('imap'),
inspect = require('util').inspect;
var MailParser = require("mailparser").MailParser;
var MailListener = require("mail-listener2");
var log_file = fs.createWriteStream('c:\\automation\\tests\\' + '/emailResponses.log', {flags : 'w'});
// var log_exceptions = fs.createWriteStream('c:\\automation\\tests\\' + '/exceptions.log', {flags : 'w'});
var log_stdout = process.stdout;
//-=-=-=- Helpers -=-=-=-
captureScreen = function(name){
browser.driver.takeScreenshot().then(function(data){
var base64Data = data.replace(/^data:image\/png;base64,/,"");
return browser.getCapabilities().then(function (cap) {
var fname = name + "-" + cap.caps_.browserName + ".png";
fs.writeFile(fname, base64Data, 'base64', function(err) {
if(err) console.log(err);
});
});
});
}
waitPageToLoad = function(){
browser.driver.manage().timeouts().pageLoadTimeout(60000);
}
getLastEmail = function() {
var deferred = protractor.promise.defer();
console.log("Waiting for an email...");
mailListener.on("mail", function(mail){
deferred.fulfill(mail);
});
return deferred.promise;
};
// -=-=- Testing configuration -=-=-
exports.config = {
chromeDriver: 'npm/node_modules/protractor/selenium/chromedriver',
chromeOnly: false,
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['c:\\automation\\tests\\frontageSanity.js'],
plugins: [{
package: 'C:\\automation\\tests\\node_modules\\protractor-console',
logLevels: ['severe', 'warning']
}],
// directConnect: true,
multiCapabilities: [{
// 'browserName': 'firefox',
// 'cli': {
// 'args': ['webdriver.firefox.useExisting=default', '-jsconsole', '-jsdebugger']
// }},
// }
// {
'browserName': 'chrome',
'chromeOptions': {
args: ['--no-sandbox','--test-type','--memory-metrics','--console','--crash-on-failure'],
// '--load-extension=' + 'C:\\Users\\idan\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\idgpnmonknjnojddfkpgkljpfnnfcklj\\1.2.4_0'
prefs: {
download: {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': 'C:\\automation\\tests\\downloaded\\'
}
}
}
// },
// {
// 'browserName': 'internet explorer',
// 'platform': 'ANY',
// 'version': '12'
}],
framework: "jasmine",
allScriptsTimeout: 90000,
getPageTimeout: 90000,
// -=-=- Pre-Conditions & Tools configurations -=-=-
onPrepare: function() {
//-=-=-=- HTML Reporter -=-=-=-
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'C:\\automation\\tests\\HtmlReporter\\',
// takeScreenShotsOnlyForFailedSpecs: true,
docTitle: 'vCita - Sanity testing report',
docName: 'Test_Report.html',
cssOverrideFile: 'htmlReporter_css.css'
}));
browser.driver.manage().window().maximize();
browser.getCapabilities().then(function (cap) {
browser.browserName = cap.caps_.browserName;
});
// -=-=-=- Email Listener - here goes your email connection configuration -=-=-=-
var mailListener = new MailListener({
username: "username",
password: "password",
host: "imap.gmail.com",
port: 993, // imap port
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
// searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
markSeen: true, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
// mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
// attachments: true, // download attachments as they are encountered to the project directory
// attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start();
mailListener.on("server:connected", function(){
console.log("imapConnected");
});
mailListener.on("server:disconnected", function(){
console.log("imapDisconnected");
});
mailListener.on("mail", function(){
// console.log("GO IT!");
});
mailListener.on("mail", function(mail, seqno, attributes){
// do something with mail object including attachments
console.log("emailParsed", mail)
// mail processing code goes here
log_file.write(util.format(mail));
log_stdout.write(util.format(mail));
});
global.mailListener = mailListener;
},
// onCleanUp: function () {
// mailListener.stop();
// },
// -=-=- Node JS Settings -=-=-
jasmineNodeOpts: {
onComplete: function () {
mailListener.stop();
},
// If true, display spec names.
isVerbose: true,
// If true, print colors to the terminal.
showColors: true,
// If true, include stack traces in failures.
includeStackTrace: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 9999999
}
}
The package you needed to install and use is called protractor-console-plugin, not protractor-console.

How to use Winston in several modules?

I have several modules - let's say server.js, module1.js,...,moduleN.js.
I would like define the log file in my server.js:
winston.add(winston.transports.File, { filename: 'mylogfile.log' });
and then use it in all my modules.
What is the best way to do that? I could exports.winston=winston; in each module and then set it in the server.js, but is there any better solution?
Thank you in advance!
The default logger concept handles this nicely.
Winston defines a default logger that any straight require (and subsequent require) to winston will retrieve. Thus you simply configure this default logger once, and it's available for subsequent module use via vanilla require('winston') in its glorious tweaked multi-transport mode.
e.g. here is my complete logging setup that defines 3 transports. I swap Loggly for MongoDB sometimes.
server.js
var logger=require('./log.js');
// requires winston and configures transports for winstons default logger- see code below.
all other .js files
var logger=require('winston'); // this retrieves default logger which was configured in log.js
logger.info("the default logger with my tricked out transports is rockin this module");
log.js - this is a one time configuration of the DEFAULT logger
var logger = require('winston');
var Loggly = require('winston-loggly').Loggly;
var loggly_options={ subdomain: "mysubdomain", inputToken: "efake000-000d-000e-a000-xfakee000a00" }
logger.add(Loggly, loggly_options);
logger.add(winston.transports.File, { filename: "../logs/production.log" });
logger.info('Chill Winston, the logs are being captured 3 ways- console, file, and Loggly');
module.exports=logger;
Alternatively for more complex scenarios you can use winston containers and retrieve the logger from a named container in other modules. I haven't used this.
My only issue with this was a missing logs directories on my deployment host which was easily fixed.
What I do ( which may not be the best way ) is use a 'global' module where I export all the stuff that I use through my applications.
For instance:
//Define your winston instance
winston.add(winston.transports.File, { filename: 'mylogfile.log' });
exports.logger = winston;
exports.otherGlobals = ....
Now just require this globally used module from your other modules
var Global = require(/path/to/global.js);
Because the file is cached after the first time it is loaded (which you can verify by including a log statement in your global; it will only log once), there's very little overhead in including it again. Putting it all into one file is also easier than requiring ALL your globally used modules on every page.
I wanted to use custom colours and levels.
So I removed the default console-transport and set a colorized one
here is my logger.js
var logger = require('winston');
logger.setLevels({
debug:0,
info: 1,
silly:2,
warn: 3,
error:4,
});
logger.addColors({
debug: 'green',
info: 'cyan',
silly: 'magenta',
warn: 'yellow',
error: 'red'
});
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { level: 'debug', colorize:true });
module.exports = logger;
Loading from app.js:
var logger = require('./lib/log.js');
Loading from other modules:
var logger = require('winston');
Slightly off topic (as the OP asks about Winston), but I like the 'child-logger' approach by Bunyan:
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});
app.use(function(req, res, next) {
req.log = log.child({reqId: uuid()});
next();
});
app.get('/', function(req, res) {
req.log.info({user: ...});
});
It solves the OP's problem as the logger is available through the req object (hence no need for 'require(log)' in each module). Additionally, all log entries belonging to a particular request will have a unique ID that connects them together.
{"name":"myapp","hostname":"pwony-2","pid":14837,"level":30,"reqId":"XXXX-XX-XXXX","user":"...#gmail.com","time":"2014-05-26T18:27:43.530Z","v":0}
I'm not sure if Winston supports this as well.
I am working on Winston 3.0.0 right now.
And it seems the way to configure the default logger has changed a little bit.
The way that works for me is folloing:
log.js// the setting for global logger
const winston= require('winston');
winston.configure({
level:"debug",
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
transports: [
new winston.transports.Console()
]
});
The other part is the same.
In the beginning of you application, require('log.js'), and also require ('winston'),
While in all other files, simply require('winston')
.
I'm creating a new Winston logger.
log.js
'use strict';
const winston = require('winston');
module.exports = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
level: 'info'
})
]
});
a.js
const log = require('./log');
log.info("from a.js");
b.js
const log = require('./log');
log.info("from b.js");
Here is my logger configuration with winston version is 3.2.1.
It storing logs in application.log file and for error stack trace I am using errors({ stack: true }) and small trick in printf function to print stack trace in error case.
Configuration
const {format, transports} = require('winston');
const { timestamp, colorize, printf, errors } = format;
const { Console, File } = transports;
LoggerConfig = {
level: process.env.LOGGER_LEVEL || 'debug',
transports: [
new Console(),
new File({filename: 'application.log'})
],
format: format.combine(
errors({ stack: true }),
timestamp(),
colorize(),
printf(({ level, message, timestamp, stack }) => {
if (stack) {
// print log trace
return `${timestamp} ${level}: ${message} - ${stack}`;
}
return `${timestamp} ${level}: ${message}`;
}),
),
expressFormat: true, // Use the default Express/morgan request formatting
colorize: false, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: function (req, res) {
return false;
} // optional: allows to skip some log messages based on request and/or response
}
Declare
I am using this same configuration in express-winston and for general log also. I declared __logger object globally so that you don't need to import every time in every file. Generally in node js all the global variable prefix with 2 time underscore(__) so it will be good to follow this.
Server.js
const winston = require('winston');
const expressWinston = require('express-winston');
/**
* winston.Logger
* logger for specified log message like console.log
*/
global.__logger = winston.createLogger(LoggerConfig);
/**
* logger for every HTTP request comes to app
*/
app.use(expressWinston.logger(LoggerConfig));
Use
__logger is global so you can use it any place, for example:
blog.controller.js
function save(req, res) {
try {
__logger.debug('Blog add operation');
.
.
return res.send(blog);
} catch (error) {
__logger.error(error);
return res.status(500).send(error);
}
}
Hope this will help !
I use a factory function and pass in the module name so it can be added to the meta data:
logger-factory.js
const path = require('path');
const { createLogger, format, transports } = require('winston');
const { combine, errors, timestamp } = format;
const baseFormat = combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
errors({ stack: true }),
format((info) => {
info.level = info.level.toUpperCase();
return info;
})(),
);
const splunkFormat = combine(
baseFormat,
format.json(),
);
const prettyFormat = combine(
baseFormat,
format.prettyPrint(),
);
const createCustomLogger = (moduleName) => createLogger({
level: process.env.LOG_LEVEL,
format: process.env.PRETTY_LOGS ? prettyFormat : splunkFormat,
defaultMeta: { module: path.basename(moduleName) },
transports: [
new transports.Console(),
],
});
module.exports = createCustomLogger;
app-harness.js (so I can run the exported index module)
const index = require('./index');
// https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
const sampleEvent = {
"Records": [
{
"eventVersion": "2.1",
"eventSource": "aws:s3",
"awsRegion": "us-east-2",
"eventTime": "2019-09-03T19:37:27.192Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:AIDAINPONIXQXHT3IKHL2"
},
"requestParameters": {
"sourceIPAddress": "205.255.255.255"
},
"responseElements": {
"x-amz-request-id": "D82B88E5F771F645",
"x-amz-id-2": "vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo="
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "828aa6fc-f7b5-4305-8584-487c791949c1",
"bucket": {
"name": "lambda-artifacts-deafc19498e3f2df",
"ownerIdentity": {
"principalId": "A3I5XTEXAMAI3E"
},
"arn": "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df"
},
"object": {
"key": "b21b84d653bb07b05b1e6b33684dc11b",
"size": 1305107,
"eTag": "b21b84d653bb07b05b1e6b33684dc11b",
"sequencer": "0C0F6F405D6ED209E1"
}
}
}
]
};
index.handler(sampleEvent)
.then(() => console.log('SUCCESS'))
.catch((_) => console.log('FAILURE'));
index.js
const logger = require('./logger-factory')(__filename);
const app = require('./app');
exports.handler = async function (event) {
try {
logger.debug('lambda triggered with event', { event });
await app.run(event);
logger.debug(`lambda finished`);
} catch(error) {
logger.error('lambda failed: ', error);
// rethrow the error up to AWS
throw error;
}
}
app.js
const logger = require('./logger-factory')(__filename);
const run = async (event) => {
logger.info('processing S3 event', event);
try {
logger.info('reading s3 file')
// throws because I used "Record" instead of "Records"
const s3 = event.Record[0].s3;
// use s3 to read the file
} catch (error) {
logger.error('failed to read from S3: ', error);
throw error;
}
};
module.exports = { run };
when I run the application locally at WARN level:
~/repos/ghe/lambda-logging (master * u=)> LOG_LEVEL=warn node -r dotenv/config ./src/app-harness.js
{
module: 'app.js',
level: 'ERROR',
message: "failed to read from S3: Cannot read property '0' of undefined",
stack: "TypeError: Cannot read property '0' of undefined\n" +
' at Object.run (/Users/jason.berk/repos/ghe/lambda-logging/src/app.js:8:28)\n' +
' at Object.exports.handler (/Users/jason.berk/repos/ghe/lambda-logging/src/index.js:7:15)\n' +
' at Object.<anonymous> (/Users/jason.berk/repos/ghe/lambda-logging/src/test-harness.js:44:7)\n' +
' at Module._compile (internal/modules/cjs/loader.js:1158:30)\n' +
' at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\n' +
' at Module.load (internal/modules/cjs/loader.js:1002:32)\n' +
' at Function.Module._load (internal/modules/cjs/loader.js:901:14)\n' +
' at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\n' +
' at internal/main/run_main_module.js:18:47',
timestamp: '2020-05-11 17:34:06'
}
{
module: 'index.js',
level: 'ERROR',
message: "lambda failed: Cannot read property '0' of undefined",
stack: "TypeError: Cannot read property '0' of undefined\n" +
' at Object.run (/Users/jason.berk/repos/ghe/lambda-logging/src/app.js:8:28)\n' +
' at Object.exports.handler (/Users/jason.berk/repos/ghe/lambda-logging/src/index.js:7:15)\n' +
' at Object.<anonymous> (/Users/jason.berk/repos/ghe/lambda-logging/src/test-harness.js:44:7)\n' +
' at Module._compile (internal/modules/cjs/loader.js:1158:30)\n' +
' at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\n' +
' at Module.load (internal/modules/cjs/loader.js:1002:32)\n' +
' at Function.Module._load (internal/modules/cjs/loader.js:901:14)\n' +
' at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\n' +
' at internal/main/run_main_module.js:18:47',
timestamp: '2020-05-11 17:34:06'
}
when I run at DEBUG level:
~/repos/ghe/lambda-logging (master * u=)> LOG_LEVEL=debug node -r dotenv/config ./src/test-harness.js
{
module: 'index.js',
event: {
Records: [
{
eventVersion: '2.1',
eventSource: 'aws:s3',
awsRegion: 'us-east-2',
eventTime: '2019-09-03T19:37:27.192Z',
eventName: 'ObjectCreated:Put',
userIdentity: { principalId: 'AWS:AIDAINPONIXQXHT3IKHL2' },
requestParameters: { sourceIPAddress: '205.255.255.255' },
responseElements: {
'x-amz-request-id': 'D82B88E5F771F645',
'x-amz-id-2': 'vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo='
},
s3: {
s3SchemaVersion: '1.0',
configurationId: '828aa6fc-f7b5-4305-8584-487c791949c1',
bucket: {
name: 'lambda-artifacts-deafc19498e3f2df',
ownerIdentity: { principalId: 'A3I5XTEXAMAI3E' },
arn: 'arn:aws:s3:::lambda-artifacts-deafc19498e3f2df'
},
object: {
key: 'b21b84d653bb07b05b1e6b33684dc11b',
size: 1305107,
eTag: 'b21b84d653bb07b05b1e6b33684dc11b',
sequencer: '0C0F6F405D6ED209E1'
}
}
}
]
},
level: 'DEBUG',
message: 'lambda triggered with event',
timestamp: '2020-05-11 17:38:21'
}
{
module: 'app.js',
Records: [
{
eventVersion: '2.1',
eventSource: 'aws:s3',
awsRegion: 'us-east-2',
eventTime: '2019-09-03T19:37:27.192Z',
eventName: 'ObjectCreated:Put',
userIdentity: { principalId: 'AWS:AIDAINPONIXQXHT3IKHL2' },
requestParameters: { sourceIPAddress: '205.255.255.255' },
responseElements: {
'x-amz-request-id': 'D82B88E5F771F645',
'x-amz-id-2': 'vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo='
},
s3: {
s3SchemaVersion: '1.0',
configurationId: '828aa6fc-f7b5-4305-8584-487c791949c1',
bucket: {
name: 'lambda-artifacts-deafc19498e3f2df',
ownerIdentity: { principalId: 'A3I5XTEXAMAI3E' },
arn: 'arn:aws:s3:::lambda-artifacts-deafc19498e3f2df'
},
object: {
key: 'b21b84d653bb07b05b1e6b33684dc11b',
size: 1305107,
eTag: 'b21b84d653bb07b05b1e6b33684dc11b',
sequencer: '0C0F6F405D6ED209E1'
}
}
}
],
level: 'INFO',
message: 'processing S3 event',
timestamp: '2020-05-11 17:38:21'
}
{
message: 'reading s3 file',
level: 'INFO',
module: 'app.js',
timestamp: '2020-05-11 17:38:21'
}
{
module: 'app.js',
level: 'ERROR',
message: "failed to read from S3: Cannot read property '0' of undefined",
stack: "TypeError: Cannot read property '0' of undefined\n" +
' at Object.run (/Users/jason.berk/repos/ghe/lambda-logging/src/app.js:8:28)\n' +
' at Object.exports.handler (/Users/jason.berk/repos/ghe/lambda-logging/src/index.js:7:15)\n' +
' at Object.<anonymous> (/Users/jason.berk/repos/ghe/lambda-logging/src/test-harness.js:44:7)\n' +
' at Module._compile (internal/modules/cjs/loader.js:1158:30)\n' +
' at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\n' +
' at Module.load (internal/modules/cjs/loader.js:1002:32)\n' +
' at Function.Module._load (internal/modules/cjs/loader.js:901:14)\n' +
' at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\n' +
' at internal/main/run_main_module.js:18:47',
timestamp: '2020-05-11 17:38:21'
}
{
module: 'index.js',
level: 'ERROR',
message: "lambda failed: Cannot read property '0' of undefined",
stack: "TypeError: Cannot read property '0' of undefined\n" +
' at Object.run (/Users/jason.berk/repos/ghe/lambda-logging/src/app.js:8:28)\n' +
' at Object.exports.handler (/Users/jason.berk/repos/ghe/lambda-logging/src/index.js:7:15)\n' +
' at Object.<anonymous> (/Users/jason.berk/repos/ghe/lambda-logging/src/test-harness.js:44:7)\n' +
' at Module._compile (internal/modules/cjs/loader.js:1158:30)\n' +
' at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\n' +
' at Module.load (internal/modules/cjs/loader.js:1002:32)\n' +
' at Function.Module._load (internal/modules/cjs/loader.js:901:14)\n' +
' at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\n' +
' at internal/main/run_main_module.js:18:47',
timestamp: '2020-05-11 17:38:21'
}
if you want to make the logger a global variable- you have to do specifically by assign it to the global variable like so
logger.js
var winston = require('winston')
var winston = winston.createLogger({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({
filename: './logs/logger.log'
})
]
});
module.exports=winston;
app.js
let logger = require('./logger')
global.__logger = logger
someController.js
__logger.info('created log successfully')
Note: it's good practice to assign a prefix for every global variable so you will know that is a global one. i'm using __ as prefix (double low dash)
Just create logger.js and put
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
transports: [
new winston.transports.Console()
]
});
module.exports = logger
Then you can require and use it anywhere, since logger is now singleton.
const logger = require('./utils/logger');
logger.info('Hello!');
This even gives you an option to swap logging library if needed. The accepted answer is totally wrong and one step closer to spaghetti code.
In my team we have created a private npm package with all default configs (as you've shown in previous answers)
I've just one question: would it be a good practice to declare the logger object as a global in order to avoid import in each and every module?

Resources