TransWikia.com

Hooks not getting executed in protractor cucumber

Software Quality Assurance & Testing Asked by Saloni Singhal on November 28, 2021

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?

One Answer

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();
    });
};

Answered by PDHide on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP