NickTomlin / protractor-flake

Rerun potentially flakey protractor tests before failing.
MIT License
80 stars 51 forks source link

Issues with parsing cucumber #68

Closed HaydenMeloche closed 7 years ago

HaydenMeloche commented 7 years ago

Hi! First of all, thank you for this plugin. I have the plugin working I'm just having problems parsing cucumber output log so it runs every test case if 1 fails. Any thoughts on what I could do to fix this?

Windows 7 Node version = 6.9.1 Protractor version = 5.1.2

Command I am running: protractor-flake --parser cucumber --max-attempts=2 -- protractor.conf.js

Output I get:

[14:21:36] I/launcher - Running 1 instances of WebDriver
[14:21:36] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
Feature: Example Test Cases

  Scenario: Passing Script
  √ Given I am on the DI login page
  √ Then I login
  √ When I am on the home page

  Scenario: Failing Script
  √ Given I am on the DI login page
  √ Then I login
  × Then I intenionally fail

Failures:

1) Scenario: Failing Script - features\arithmetic.feature:8
   Step: Then I intenionally fail - features\arithmetic.feature:11
   Step Definition: Testing\features\steps\DI_login_steps.js:27
   Message:
     AssertionError: expected 'Home - RBC Direct Investing' to include 'pop'

2 scenarios (1 failed, 1 passed)
6 steps (1 failed, 5 passed)
0m16.993s
[14:21:55] I/launcher - 0 instance(s) of WebDriver still running
[14:21:55] I/launcher - chrome #01 failed 1 test(s)
[14:21:55] I/launcher - overall: 1 failed spec(s)
[14:21:55] E/launcher - Process exited with error code 1

Using cucumber to parse output
Re-running tests: test attempt 2

Tests failed but no specs were found. All specs will be run again.

[14:21:57] I/launcher - Running 1 instances of WebDriver
[14:21:57] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
Feature: Example Test Cases

  Scenario: Passing Script
  √ Given I am on the DI login page
  √ Then I login
  √ When I am on the home page

  Scenario: Failing Script
  √ Given I am on the DI login page
  √ Then I login
  × Then I intenionally fail

Failures:

1) Scenario: Failing Script - features\arithmetic.feature:8
   Step: Then I intenionally fail - features\arithmetic.feature:11
   Step Definition: Testing\features\steps\DI_login_steps.js:27
   Message:
     AssertionError: expected 'Home - RBC Direct Investing' to include 'pop'

2 scenarios (1 failed, 1 passed)
6 steps (1 failed, 5 passed)
0m16.227s
[14:22:17] I/launcher - 0 instance(s) of WebDriver still running
[14:22:17] I/launcher - chrome #01 failed 1 test(s)
[14:22:17] I/launcher - overall: 1 failed spec(s)
[14:22:17] E/launcher - Process exited with error code 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! protractor-cucumber-hayden@1.0.0 test: `protractor-flake --parser cucumber --max-attempts=2 -- protractor.conf.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the protractor-cucumber-hayden@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
wswebcreation commented 7 years ago

Hi @HaydenMeloche

Tnx for the compliments. Which version of CucumberJS are you using?

When I look at the docs here you are using a parser that only works for version < 0.9.0 of CucumberJS. I would advice using the cucumberMulti

I also used you logging against the parser that should work with versions higher then 0.9.0 and that also doesn't work, so it would be nice to know your version.

A workaround (I'm using this in my current project) is to add a hook like described here. This is only for CucumberJS 1, for CucumberJS 2 you can use this piece of code

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
  After(function (scenarioResult, callback) {
    if (scenarioResult.status === 'failed') {
            // Log the spec to the console for protractor-flake to be able to rerun the failed specs
            console.log('Specs:', scenarioResult.scenario.uri);
    }
    callback();
  });
});

With this hook you are version independent.

Hope it helps

Grtz

wswebcreation

HaydenMeloche commented 7 years ago

@wswebcreation Thanks for the response.

I am using "cucumber": "^1.3.3".

I tried adding a hook for version 1. mention in the docs but it seems like the parser tries to read it and it errors. LOG: `> protractor-cucumber-hayden@1.0.0 test C:\OBIS\Protractor Testing

protractor-flake --parser cucumberMulti --max-attempts=2 -- protractor.conf.js

[09:21:11] I/launcher - Running 1 instances of WebDriver
[09:21:11] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[09:21:13] E/launcher - Error: CompositeParserException: Parser errors:
(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'var {defineSupportCode} = require('cucumber');'
(3:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'defineSupportCode(function({After}) {'
(4:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'var afterHook = function () {'
(5:9): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'this.After(function (scenario, callback) {'
(6:15): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'if (scenario.isFailed()) {'
(7:19): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'console.log('Specs:', scenario.getUri());'
(8:15): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '}'
(9:15): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'callback();'
(10:9): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '});'
(11:7): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '};'
(12:7): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'module.exports = afterHook;'
path: C:\OBIS\Protractor Testing\afterHooks.js
    at Function.Errors.CompositeParserException.create (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\errors.js:27:13)
    at addError (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\parser.js:88:45)
    at matchTokenAt_0 (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\parser.js:261:5)
    at matchToken (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\parser.js:149:14)
    at Parser.parse (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\parser.js:72:15)
    at C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\parser.js:18:36
    at Array.forEach (native)
    at Object.parse (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\parser.js:12:22)
    at Object.getFeatures (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\runtime.js:39:35)
    at Object.start (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\runtime.js:11:27)
[09:21:13] E/launcher - Process exited with error code 100

Using cucumberMulti to parse output
Re-running tests: test attempt 2

Tests failed but no specs were found. All specs will be run again.

AfterHook.js

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
    var afterHook = function () {
        this.After(function (scenario, callback) {
              if (scenario.isFailed()) {
                  console.log('Specs:', scenario.getUri());
              }
              callback();
        });
      };
      module.exports = afterHook;
}

protractor.conf.js

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',

  framework: 'custom',
  frameworkPath: require.resolve('protractor-cucumber-framework'),

  specs: [
    'afterHooks.js',  
    'features/*.feature'
  ],
  cucumberOpts: {
    require: 'features/steps/*_steps.js',
    format: 'pretty'
  }
}

Any ideas?

wswebcreation commented 7 years ago

You added the wrong hook, the one you added was for CucumberJS 2

..A workaround (I'm using this in my current project) is to add a hook like described here. This is only for CucumberJS 1, for CucumberJS 2 you can use this piece of code...

So take this code ;-)

HaydenMeloche commented 7 years ago

@wswebcreation I must be doing something else wrong. Still getting same error :(

[10:31:09] I/launcher - Running 1 instances of WebDriver
[10:31:09] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[10:31:35] E/launcher - Error: CompositeParserException: Parser errors:
(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'var afterHook = function () {'
(2:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'this.After(function (scenario, callback) {'
(3:11): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'if (scenario.isFailed()) {'
(4:15): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'console.log('Specs:', scenario.getUri());'
(5:11): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '}'
(6:11): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'callback();'
(7:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '});'
(8:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '};'
(10:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'module.exports = afterHook;'
path: C:\OBIS\Protractor Testing\afterHooks.js
    at Function.Errors.CompositeParserException.create (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\errors.js:27:13)
    at Parser.parse (C:\OBIS\Protractor Testing\node_modules\gherkin\lib\gherkin\parser.js:79:45)
    at C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\parser.js:18:36
    at Array.forEach (native)
    at Object.parse (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\parser.js:12:22)
    at Object.getFeatures (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\runtime.js:39:35)
    at Object.start (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\runtime.js:11:27)
    at Object.run (C:\OBIS\Protractor Testing\node_modules\cucumber\lib\cucumber\cli.js:75:15)
    at C:\OBIS\Protractor Testing\node_modules\protractor-cucumber-framework\index.js:38:36
    at Function.promise (C:\OBIS\Protractor Testing\node_modules\q\q.js:682:9)
[10:31:35] E/launcher - Process exited with error code 100

Using cucumberMulti to parse output
Re-running tests: test attempt 2

Tests failed but no specs were found. All specs will be run again. 
wswebcreation commented 7 years ago

Can you create a simple git project and give me rights, then I'll fix it for you tonight or tomorrow.

Grtz wswebcreation

HaydenMeloche commented 7 years ago

@wswebcreation Sorry for wasting your time. I figured out what it was. I had require in the wrong section in protractor.conf.js................

Thanks for your help anyway!

NickTomlin commented 7 years ago

@HaydenMeloche glad you figured it out.

@wswebcreation thanks for helping debug! I wonder if we could consolidate the cucumber parsing since cucumberMulti supports both multi and non multi specs. We could work to deprecate or just drop it and make a major version bump.

wswebcreation commented 7 years ago

@HaydenMeloche

No problem

@NickTomlin

Looks like you can read minds. I first want to check what CucumberJS 3 does, was just released this week. Protractor-cucumber-framework was fixed today. Hope to get my boilerplate up and running this weekend and then will create a PR for protractor-flake to remove the CucumberJS parser so you will need to make a major.

By the way, the hook that logs a line in the console, that's also in the docs, is a more stable way to use to read the logs. Can't we also create something for Jasmine and Mocha, that will save a lot of debugging 🤔😁

NickTomlin commented 7 years ago

Yeah I think that'd be great if we could find a way to insert logging into the frameworks and not have to worry as much about parsing