Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

any way to monkey patch cucumber reporting? #806

Closed
laurelnaiad opened this issue May 15, 2014 · 1 comment
Closed

any way to monkey patch cucumber reporting? #806

laurelnaiad opened this issue May 15, 2014 · 1 comment

Comments

@laurelnaiad
Copy link

Cucumber run through protractor is emitting giant useless stacktraces. For something that is supposed to be good for having business involvement in test development, this is a no go, so I'm trying to clean up the console.

I'm running protractor through the runner, FYI. I'm having trouble following the chain of command can't figure out who owns the console when protractor is running cucumber, but it seems it isn't me! When I completely disable console.log, console.error and console.stack it has no effect on protractor or cucumber.

Has anyone figured out how or where to monkey patch the console for a scenario like this? I am kind of stuck because it's making protractor a difficult sell when each assertion error results in pages of useless information, which effectively buries the test results in garbage.

What I'd like to do is just sniff out any lines that are stack traces (/^[ ]*at / would be a reasonable expression for starters) and swallow them or accumulate them and emit the first 7 or so. Pages of useless stacks are killing the love and initial attempts to figure out who owns the process that has the console is driving me in circles! :)

I realize this problem starts (or probably starts) with cucumber, but it seems like it's being compounded because cucumber is behind protractor and that's keeping me from monkey patching the right console? I'm really just guessing node-debugiing my way through the process, I'm not seeing any child processes being spawned, but I may be stepping over the key line of code...

@laurelnaiad
Copy link
Author

I figured out why strange things were happening -- webdriver is (repeatedly -- at least under cucumber) parsing and rebuilding stack traces which makes it a bit "different" in where and how you need to patch it.

For anyone trying to deal with cucumber and protractor, or really anyone who wants to customize the traces that come out of webdriver, this might help: cucumber/cucumber-js#157 (comment) by way of explanation. Since this is an issue for a protractor crowd, I'll provide a little more code... my World module is where I set this up. Obviously my World and Page objects are just getting off the ground and don't do much, yet, but it was a convenient place to do the patching. I''ll be nulling out my patch after protractor has run so that other parts of my build process don't become mute for stack traces, but I haven't gotten to that yet, and it's late :):

// this file adds global variables -- not generally a good idea but for cucumber tests it's handy
var path = require('path');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
// webdriver promises do not derive from Object.prototype, thus `should`
// cannot influence them
// chai.should();
global.expect = chai.expect;

global.ptor = require('protractor').getInstance();

var util = require('util');

function PageBase () {}

PageBase.prototype.go = function () {
  return ptor.get(this.url);
};

var pages = {};

function World (callback) {
  this.pages = pages;
  this.currentPage = null;
  callback(this);
}

function addPage (name, Page) {
  util.inherits(Page, PageBase);
  pages[name] = new Page();
}
World.addPage = addPage;

Object.defineProperty(World.prototype, 'pageTitle', {
  get: function () {
    var title = ptor.getTitle();
    return title;
  }
});

// monkey-patch stack trace more-or-less compatible with webdriver
var isMyCode = new RegExp(
  path.join(
    __dirname,
    '../..',
    '[^node_modules/]'
  )
      .replace(/\//g, '\\/')
);


Error.prepareStackTrace = function (err, stack) {
  var i;
  var frame;
  var results = [];
  for (i = 0; i < stack.length; i++) {
    frame = stack[i].toString();
    if (isMyCode.test(frame)) {
      results.push('    at ' + frame);
    }
  }
  return results.length ? '\n' + results.join('\n') : '';
};

global.World = World;

This does get me wondering about whether I could also patch the WD promises to support chai's should...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant