1

I new to protractor, and trying to implement hooks. But the hooks are not getting executed. Following is my code: Hook.js:

const {
    Before
} = require('cucumber');

const {AfterAll} = require('cucumber');

module.exports = function () {
    
   Before(function () {
        browser.manage().window().maximize();
       /// await browser.manage().deleteAllCookies();
        
    });

    AfterAll(async function () {
        return driver.quit();
    });
}

I have also added the path in config.js: enter image description here

Do I need to give its path in spec.js as well?

3
  • Compare what you've written to github.com/cucumber/cucumber-js/blob/master/docs/support_files/…. Just requiring your hook file doesn't do anything, because the function isn't executed. Also you can't have two require keys in one object, the value is an array so you can have multiple entries there.
    – jonrsharpe
    Jun 22, 2020 at 6:52
  • The issue is now resolved. After removing module.exports = function() from hooks.js,it worked fine. Jun 22, 2020 at 7:34
  • 1
    I'd suggest deleting this then
    – jonrsharpe
    Jun 22, 2020 at 7:44

1 Answer 1

1

Try to understand what the code does:

const {
    Before
} = require('cucumber');

const {AfterAll} = require('cucumber');

module.exports = function () {
    
   Before(function () {
        browser.manage().window().maximize();
       /// await browser.manage().deleteAllCookies();
        
    });

    AfterAll(async function () {
        return driver.quit();
    });
}

Here you creates a function but never calls the function.

you can make it work in three ways:

First Approach: Call the function and pass the object to module.exports

const {
    Before
} = require('cucumber');

const {AfterAll} = require('cucumber');

//store function to a variable
let hooks = function () {
    
   Before(function () {
        browser.manage().window().maximize();
       /// await browser.manage().deleteAllCookies();
        
    });

    AfterAll(async function () {
        return driver.quit();
    });
}

//call the function and pass it to module.exports
module.exports = hooks();

Second Approach: Create self-invoking function

const {
    Before
} = require('cucumber');

const {AfterAll} = require('cucumber');

module.exports = (function () {
    
   Before(function () {
        browser.manage().window().maximize();
       /// await browser.manage().deleteAllCookies();
        
    });

    AfterAll(async function () {
        return driver.quit();
    });
})();

Third Approach: create an object using object literal instead of creating a function and calling it ( This what you have done as the fix )

const {
    Before
} = require('cucumber');

const {AfterAll} = require('cucumber');

module.exports = {
    
   Before(function () {
        browser.manage().window().maximize();
       /// await browser.manage().deleteAllCookies();
        
    });

    AfterAll(async function () {
        return driver.quit();
    });
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.