Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cucumberjs: finding if a step Result is failed

Task:

  • Perform an automated acceptance test using selenium, webdriver, cucumberjs.
  • Find a failing acceptance test scenario and take a screenshot of the page
  • Save that as an artefact in the go pipeline.

That was my task for the day. I have done all of that with writing the scenario test, selenium setup, go pipelines, and everything else needed.

The only problem is - I could not get a way to figure out how can I find a failing scenario step and take a screen shot of the page.

Problem details: I have the following code placed in my step definitions, which is run for every scenario step

//file: features/setpdefinitions/common.step.js

var commonWrapper = function commonWrapper() {

    this.World = require('../support/world').World;

    this.Before(function (next) {
        this.initBrowser(next);
    });

    this.After(function (next) {
        this.browser.quit(next);
    });

    this.StepResult(function (event, callback) {
        var stepResult = event.getPayloadItem('stepResult');
        console.log(stepResult.isFailed());
        callback();
    });
};

module.exports = commonWrapper;

the World contains the browser initiation methods.

and, this is a sample feature scenario that I am testing

Feature: Forgot Password
     As a user of Booking My account
     I want to reset my password
     So that I can login to my account when I forget my password

 Scenario: On unsuccessful entering invalid email id
    Given I am on forgot password page
    When I enter invalid email "invalidemail-someDomain.com"
        And click submit button
    Then I should see validation message "Please enter a valid email."

the problem is with the context data. I somehow could not get the scenario passed to the after/before method as the first argument. I tried the code given in the cucumberjs source but could not succeed. So, I moved onto adding the stepResult method, which run every time a step is completed. A relatively similar approach.

As per documentation, the isFailed() method returns a boolean based on the step result. but, I always get a false no matter the step failed or passed. I tried its alter-ego isSuccessful() which returns a true no matter what.

so,

  1. what could I be possibly doing wrong here?
  2. how do I actually pass the scenario to the after() method?

I'm relatively new to TDD, but hey it been a great experience so far.

like image 728
Sanjeev Avatar asked Feb 12 '23 15:02

Sanjeev


1 Answers

What you need is an after hook

Create a file features/support/after_hooks.js

module.exports = function() {
    this.After(function (scenario, callback) {
        if (scenario.isFailed()) {
            // Do your after stuff here
        }
        callback();
    });
};

Note that this is only executed after every feature

like image 59
Dominik Ehrenberg Avatar answered Feb 15 '23 12:02

Dominik Ehrenberg